Last active
November 13, 2019 14:50
-
-
Save 0xR/091849b8bb8d935d7343416f52d72a3e to your computer and use it in GitHub Desktop.
Vert.X streams with Reactor
This file contains hidden or 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
fun <Input, Output> processWithReactor( | |
vertx: Vertx, | |
inputStream: ReadStream<Input>, | |
outputStream: WriteStream<Output>, | |
processor: (Flux<Input>) -> Flux<Output> | |
) { | |
val publisher = ReactiveWriteStream.writeStream<Input>(vertx) | |
inputStream.pipeTo(publisher) | |
val writeStreamFlux = Flux.from(publisher) | |
val processedFlux = processor(writeStreamFlux) | |
val subscriber = ReactiveReadStream.readStream<Output>() | |
processedFlux.subscribe(subscriber) | |
subscriber.pipeTo(outputStream) | |
} | |
// Use the helper in a Vert.X handler | |
processWithReactor( | |
vertx, | |
inputStream = websocketStream, | |
outputStream = eventBusPublisher | |
) { socketFlux -> | |
socketFlux | |
.flatMap { /* do things*/ } | |
.buffer(10) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment