Updated and add tests and services

This commit is contained in:
Sambo Chea 2020-07-11 21:02:26 +07:00
parent 67dbe86095
commit bc4005a1a3
4 changed files with 65 additions and 13 deletions

View File

@ -0,0 +1,63 @@
package com.cubetiqs.wsserver
import org.springframework.boot.autoconfigure.SpringBootApplication
import org.springframework.boot.runApplication
import org.springframework.http.MediaType
import org.springframework.messaging.handler.annotation.MessageMapping
import org.springframework.messaging.rsocket.RSocketRequester
import org.springframework.stereotype.Controller
import org.springframework.stereotype.Service
import org.springframework.web.bind.annotation.GetMapping
import org.springframework.web.bind.annotation.PathVariable
import org.springframework.web.bind.annotation.RestController
import reactor.core.publisher.Flux
import java.time.Duration
import java.time.LocalDateTime
import java.util.concurrent.ThreadLocalRandom
@SpringBootApplication
class WsServerApplication
fun main(args: Array<String>) {
runApplication<WsServerApplication>(*args)
}
@RestController
class RestController (val priceService: PriceService) {
@GetMapping(
value = ["/stocks/{symbol}"],
produces = [MediaType.TEXT_EVENT_STREAM_VALUE]
)
fun prices(
@PathVariable symbol: String
): Flux<StockPrice> {
return priceService.generate(symbol)
}
}
@Controller
class RSocketController(val priceService: PriceService) {
@MessageMapping("stockPrices")
fun prices(symbol: String): Flux<StockPrice> {
return priceService.generate(symbol)
}
}
@Service
class PriceService {
fun generate(symbol: String): Flux<StockPrice> {
return Flux
.interval(Duration.ofSeconds(1))
.map { StockPrice(symbol, randomStockPrice(), LocalDateTime.now()) }
}
private fun randomStockPrice(): Double {
return ThreadLocalRandom.current().nextDouble(100.0)
}
}
data class StockPrice (
val symbol: String,
val price: Double,
val time: LocalDateTime
)

View File

@ -1,11 +0,0 @@
package com.cubetiqs.wsserver
import org.springframework.boot.autoconfigure.SpringBootApplication
import org.springframework.boot.runApplication
@SpringBootApplication
class WsserverApplication
fun main(args: Array<String>) {
runApplication<WsserverApplication>(*args)
}

View File

@ -1 +1 @@
spring.rsocket.server.port=7000

View File

@ -4,7 +4,7 @@ import org.junit.jupiter.api.Test
import org.springframework.boot.test.context.SpringBootTest
@SpringBootTest
class WsserverApplicationTests {
class WsServerApplicationTests {
@Test
fun contextLoads() {