use of reactor.core.publisher.EmitterProcessor in project reactor-netty by reactor.
the class HttpTests method streamAndPoolExplicitCompression.
@Test
public void streamAndPoolExplicitCompression() throws Exception {
EmitterProcessor<String> ep = EmitterProcessor.create();
NettyContext server = HttpServer.create(opts -> opts.port(0)).newRouter(r -> r.post("/hi", (req, res) -> req.receive().aggregate().asString().log().then(res.sendString(Flux.just("test")).then())).get("/stream", (req, res) -> req.receive().then(res.compression(true).options(op -> op.flushOnEach()).sendString(ep.log()).then()))).block(Duration.ofSeconds(30));
String content = HttpClient.create(opts -> opts.port(server.address().getPort()).compression(true)).post("/hi", req -> req.sendString(Flux.just("1", "2", "3", "4", "5"))).flatMap(res -> res.receive().aggregate().asString().log()).block();
Flux<String> f = HttpClient.create(opts -> opts.port(server.address().getPort()).compression(true)).get("/stream").flatMapMany(res -> res.receive().asString());
System.out.println(content);
StepVerifier.create(f).then(() -> ep.onNext("test1")).expectNext("test1").thenAwait(Duration.ofMillis(300)).then(() -> ep.onNext("test2")).thenAwait(Duration.ofMillis(300)).expectNext("test2").thenAwait(Duration.ofMillis(300)).then(() -> ep.onComplete()).verifyComplete();
content = HttpClient.create(opts -> opts.port(server.address().getPort()).compression(true)).post("/hi", req -> req.sendString(Flux.just("1", "2", "3", "4", "5"))).flatMap(res -> res.receive().aggregate().asString().log()).block();
server.dispose();
}
use of reactor.core.publisher.EmitterProcessor in project reactor-netty by reactor.
the class TcpServerTests method test5.
@Test
@Ignore
public void test5() throws Exception {
// Hot stream of data, could be injected from anywhere
EmitterProcessor<String> broadcaster = EmitterProcessor.create();
// Get a reference to the tail of the operation pipeline (microbatching + partitioning)
final Processor<List<String>, List<String>> processor = WorkQueueProcessor.<List<String>>builder().autoCancel(false).build();
broadcaster.bufferTimeout(10, Duration.ofSeconds(1)).log("broadcaster").subscribe(processor);
// on a server dispatching data on the default shared dispatcher, and serializing/deserializing as string
// Listen for anything exactly hitting the root URI and route the incoming connection request to the callback
NettyContext s = HttpServer.create(0).newRouter(r -> r.get("/", (request, response) -> {
// prepare a response header to be appended first before any reply
response.addHeader("X-CUSTOM", "12345");
// returning a stream of String from each microbatch merged
return response.sendString(Flux.from(processor).flatMap(Flux::fromIterable).take(Duration.ofSeconds(5)).concatWith(Flux.just("end\n")));
})).block(Duration.ofSeconds(30));
for (int i = 0; i < 50; i++) {
Thread.sleep(500);
broadcaster.onNext(System.currentTimeMillis() + "\n");
}
s.dispose();
}
use of reactor.core.publisher.EmitterProcessor in project reactor-netty by reactor.
the class HttpTests method streamAndPoolDefaultCompression.
@Test
public void streamAndPoolDefaultCompression() throws Exception {
EmitterProcessor<String> ep = EmitterProcessor.create();
NettyContext server = HttpServer.create(opts -> opts.port(0).compression(true)).newRouter(r -> r.post("/hi", (req, res) -> req.receive().aggregate().asString().log().then(res.compression(false).sendString(Flux.just("test")).then())).get("/stream", (req, res) -> req.receive().then(res.options(op -> op.flushOnEach()).sendString(ep.log()).then()))).block(Duration.ofSeconds(30));
String content = HttpClient.create(opts -> opts.port(server.address().getPort()).compression(true)).post("/hi", req -> req.sendString(Flux.just("1", "2", "3", "4", "5"))).flatMap(res -> res.receive().aggregate().asString().log()).block();
Flux<String> f = HttpClient.create(opts -> opts.port(server.address().getPort()).compression(true)).get("/stream").flatMapMany(res -> res.receive().asString());
System.out.println(content);
StepVerifier.create(f).then(() -> ep.onNext("test1")).expectNext("test1").thenAwait(Duration.ofMillis(300)).then(() -> ep.onNext("test2")).thenAwait(Duration.ofMillis(300)).expectNext("test2").thenAwait(Duration.ofMillis(300)).then(() -> ep.onComplete()).verifyComplete();
content = HttpClient.create(opts -> opts.port(server.address().getPort()).compression(true)).post("/hi", req -> req.sendString(Flux.just("1", "2", "3", "4", "5"))).flatMap(res -> res.receive().aggregate().asString().log()).block();
server.dispose();
}
use of reactor.core.publisher.EmitterProcessor in project reactor-core by reactor.
the class FluxSpecTests method whenFilterFunctionThrowsFilteredComposableAcceptsError.
@Test
public void whenFilterFunctionThrowsFilteredComposableAcceptsError() {
// "When a filter function throws an exception, the filtered composable accepts the error"
// given: "a source composable with a filter function that throws an error"
EmitterProcessor<Integer> source = EmitterProcessor.create();
Flux<Integer> filtered = source.filter(it -> {
if (it == 1) {
throw new RuntimeException();
} else {
return true;
}
});
LongAdder errors = new LongAdder();
filtered.doOnError(e -> errors.increment()).subscribe();
// when: "the source accepts a value"
source.onNext(1);
// then: "the error is passed on"
assertThat(errors.intValue()).isEqualTo(1);
}
use of reactor.core.publisher.EmitterProcessor in project reactor-core by reactor.
the class FluxSpecTests method whenMappingFunctionThrowsMappedComposableAcceptsError.
@Test
public void whenMappingFunctionThrowsMappedComposableAcceptsError() {
// "When a mapping function throws an exception, the mapped composable accepts the error"
// given: "a source composable with a mapping function that throws an error"
EmitterProcessor<Integer> source = EmitterProcessor.create();
Flux<String> mapped = source.map(it -> {
if (it == 1) {
throw new RuntimeException();
} else {
return "na";
}
});
LongAdder errors = new LongAdder();
mapped.doOnError(e -> errors.increment()).subscribe();
// when: "the source accepts a value"
source.onNext(1);
// then: "the error is passed on"
assertThat(errors.intValue()).isEqualTo(1);
}
Aggregations