Created
May 2, 2021 09:54
Star
You must be signed in to star a gist
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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