Skip to content

Instantly share code, notes, and snippets.

@soudmaijer
Created May 2, 2021 09:54
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
Save soudmaijer/6cd8f3e417463fda5b892419f62acc19 to your computer and use it in GitHub Desktop.
package nl.sourcelabs.kotlinflowboot.server
import kotlinx.coroutines.delay
import kotlinx.coroutines.flow.flow
import org.springframework.boot.autoconfigure.SpringBootApplication
import org.springframework.boot.runApplication
import org.springframework.http.MediaType
import org.springframework.web.bind.annotation.GetMapping
import org.springframework.web.bind.annotation.RestController
@SpringBootApplication
@RestController
class NumberStreamApplication {
@GetMapping("/number-stream", produces = [MediaType.APPLICATION_NDJSON_VALUE])
suspend fun numberFlow() = flow {
while (true) {
delay((100..1000).random().toLong())
emit((0..100).random())
}
}
}
fun main(args: Array<String>) {
runApplication<NumberStreamApplication>(*args)
}
package nl.sourcelabs.kotlinflowboot.client
import kotlinx.coroutines.flow.collect
import kotlinx.coroutines.runBlocking
import org.springframework.boot.CommandLineRunner
import org.springframework.boot.WebApplicationType
import org.springframework.boot.autoconfigure.SpringBootApplication
import org.springframework.boot.runApplication
import org.springframework.context.annotation.Bean
import org.springframework.web.reactive.function.client.WebClient
import org.springframework.web.reactive.function.client.bodyToFlow
@SpringBootApplication
class NumberStreamClient {
@Bean
fun run() = CommandLineRunner {
runBlocking {
WebClient.create("http://localhost:8080")
.get()
.uri("/number-stream")
.retrieve()
.bodyToFlow<Int>()
.collect { println(it) }
}
}
}
fun main(args: Array<String>) {
runApplication<NumberStreamClient>(*args) {
webApplicationType = WebApplicationType.NONE
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment