Updated and add tests and services
This commit is contained in:
parent
67dbe86095
commit
bc4005a1a3
63
src/main/kotlin/com/cubetiqs/wsserver/WsServerApplication.kt
Normal file
63
src/main/kotlin/com/cubetiqs/wsserver/WsServerApplication.kt
Normal 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
|
||||||
|
)
|
@ -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)
|
|
||||||
}
|
|
@ -1 +1 @@
|
|||||||
|
spring.rsocket.server.port=7000
|
@ -4,7 +4,7 @@ import org.junit.jupiter.api.Test
|
|||||||
import org.springframework.boot.test.context.SpringBootTest
|
import org.springframework.boot.test.context.SpringBootTest
|
||||||
|
|
||||||
@SpringBootTest
|
@SpringBootTest
|
||||||
class WsserverApplicationTests {
|
class WsServerApplicationTests {
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
fun contextLoads() {
|
fun contextLoads() {
|
Loading…
Reference in New Issue
Block a user