Skip to content

Commit

Permalink
clean codes.
Browse files Browse the repository at this point in the history
  • Loading branch information
hantsy committed Oct 24, 2018
1 parent bbd11df commit dc0c864
Show file tree
Hide file tree
Showing 6 changed files with 24 additions and 23 deletions.
2 changes: 1 addition & 1 deletion kotlin-dsl/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<java.version>1.8</java.version>
<kotlin.version>1.1.61</kotlin.version>
<kotlin.version>1.2.41</kotlin.version>
</properties>

<dependencies>
Expand Down
15 changes: 8 additions & 7 deletions kotlin-dsl/src/main/kotlin/com/example/demo/Application.kt
Original file line number Diff line number Diff line change
Expand Up @@ -4,37 +4,38 @@ import org.springframework.context.support.GenericApplicationContext
import org.springframework.http.server.reactive.HttpHandler
import org.springframework.http.server.reactive.ReactorHttpHandlerAdapter
import org.springframework.web.server.adapter.WebHttpHandlerBuilder
import reactor.ipc.netty.http.server.HttpServer
import reactor.ipc.netty.tcp.BlockingNettyContext
import reactor.netty.DisposableServer
import reactor.netty.http.server.HttpServer


class Application {

private val httpHandler: HttpHandler

private val server: HttpServer

private var nettyContext: BlockingNettyContext? = null
private var nettyContext: DisposableServer? = null

constructor(port: Int = 8080) {
val context = GenericApplicationContext()
beans().initialize(context)
context.refresh()
context.getBean(DataInitializr::class.java).initData()
server = HttpServer.create(port)
server = HttpServer.create().host("localhost").port(port)
httpHandler = WebHttpHandlerBuilder.applicationContext(context)
.build()
}

fun start() {
nettyContext = server.start(ReactorHttpHandlerAdapter(httpHandler))
nettyContext = server.handle(ReactorHttpHandlerAdapter(httpHandler)).bindNow()
}

fun startAndAwait() {
server.startAndAwait(ReactorHttpHandlerAdapter(httpHandler), { nettyContext = it })
server.handle(ReactorHttpHandlerAdapter(httpHandler)).bindNow().onDispose().block()
}

fun stop() {
nettyContext?.shutdown()
nettyContext?.disposeNow()
}
}

Expand Down
12 changes: 6 additions & 6 deletions kotlin-routes/src/main/kotlin/com/example/demo/Application.kt
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ import org.springframework.context.ApplicationContext
import org.springframework.context.annotation.*
import org.springframework.http.server.reactive.ReactorHttpHandlerAdapter
import org.springframework.web.server.adapter.WebHttpHandlerBuilder
import reactor.ipc.netty.NettyContext
import reactor.ipc.netty.http.server.HttpServer
import reactor.netty.DisposableServer
import reactor.netty.http.server.HttpServer

@Configuration
@ComponentScan
Expand All @@ -18,11 +18,11 @@ class Application {

@Profile("default")
@Bean
fun nettyContext(context: ApplicationContext): NettyContext {
fun nettyHttpServer(context: ApplicationContext): DisposableServer {
val handler = WebHttpHandlerBuilder.applicationContext(context).build()
val adapter = ReactorHttpHandlerAdapter(handler)
val httpServer = HttpServer.create("localhost", this.port)
return httpServer.newHandler(adapter).block()
val httpServer = HttpServer.create().host("localhost").port(this.port)
return httpServer.handle(adapter).bindNow()
}
}

Expand All @@ -31,6 +31,6 @@ fun main(args: Array<String>) {
AnnotationConfigApplicationContext {
register(Application::class.java)
refresh()
getBean(NettyContext::class.java).onClose().block()
getBean(DisposableServer::class.java).onDispose().block()
}
}
2 changes: 1 addition & 1 deletion kotlin/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<java.version>1.8</java.version>
<kotlin.version>1.1.61</kotlin.version>
<kotlin.version>1.2.41</kotlin.version>
</properties>

<dependencies>
Expand Down
12 changes: 6 additions & 6 deletions kotlin/src/main/kotlin/com/example/demo/Application.kt
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ import org.springframework.context.ApplicationContext
import org.springframework.context.annotation.*
import org.springframework.http.server.reactive.ReactorHttpHandlerAdapter
import org.springframework.web.server.adapter.WebHttpHandlerBuilder
import reactor.ipc.netty.NettyContext
import reactor.ipc.netty.http.server.HttpServer
import reactor.netty.DisposableServer
import reactor.netty.http.server.HttpServer

@Configuration
@ComponentScan
Expand All @@ -18,11 +18,11 @@ class Application {

@Profile("default")
@Bean
fun nettyContext(context: ApplicationContext): NettyContext {
fun nettyHttpServer(context: ApplicationContext): DisposableServer {
val handler = WebHttpHandlerBuilder.applicationContext(context).build()
val adapter = ReactorHttpHandlerAdapter(handler)
val httpServer = HttpServer.create("localhost", this.port)
return httpServer.newHandler(adapter).block()
val httpServer = HttpServer.create().host("localhost").port(this.port)
return httpServer.handle(adapter).bindNow()
}
}

Expand All @@ -31,6 +31,6 @@ fun main(args: Array<String>) {
AnnotationConfigApplicationContext {
register(Application::class.java)
refresh()
getBean(NettyContext::class.java).onClose().block()
getBean(DisposableServer::class.java).onDispose().block()
}
}
4 changes: 2 additions & 2 deletions kotlin/src/test/kotlin/com/example/demo/ApplicationTests.kt
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,14 @@ import reactor.test.test
@ContextConfiguration(classes = arrayOf(Application::class))
class ApplicationTests {

@Value("#{@nettyContext.address().getPort()}")
@Value("#{@nettyHttpServer.port()}")
var port = 8080

lateinit var client: WebClient

@Before
fun setup() {
client = WebClient.create("http://localhost:8080")
client = WebClient.create("http://localhost:" + this.port)
}

@Test
Expand Down

0 comments on commit dc0c864

Please sign in to comment.