use of reactor.netty.DisposableServer in project reactor-netty by reactor.
the class ServerTransport method bindUntilJavaShutdown.
/**
* Start the server in a fully blocking fashion, not only waiting for it to initialize
* but also blocking during the full lifecycle of the server. Since most
* servers will be long-lived, this is more adapted to running a server out of a main
* method, only allowing shutdown of the servers through {@code sigkill}.
* <p>
* Note: {@link Runtime#addShutdownHook(Thread) JVM shutdown hook} is added by
* this method in order to properly disconnect the server upon receiving a
* {@code sigkill} signal.</p>
*
* @param timeout a timeout for server shutdown
* @param onStart an optional callback on server start
*/
public final void bindUntilJavaShutdown(Duration timeout, @Nullable Consumer<DisposableServer> onStart) {
Objects.requireNonNull(timeout, "timeout");
DisposableServer facade = Objects.requireNonNull(bindNow(), "facade");
if (onStart != null) {
onStart.accept(facade);
}
Runtime.getRuntime().addShutdownHook(new Thread(() -> facade.disposeNow(timeout)));
facade.onDispose().block();
}
use of reactor.netty.DisposableServer in project spring-boot by spring-projects.
the class GracefulShutdown method shutDownGracefully.
void shutDownGracefully(GracefulShutdownCallback callback) {
DisposableServer server = this.disposableServer.get();
if (server == null) {
return;
}
logger.info("Commencing graceful shutdown. Waiting for active requests to complete");
this.shutdownThread = new Thread(() -> doShutdown(callback, server), "netty-shutdown");
this.shutdownThread.start();
}
use of reactor.netty.DisposableServer in project spring-framework by spring-projects.
the class ReactorHttpServer method startInternal.
@Override
protected void startInternal() {
DisposableServer server = this.reactorServer.handle(this.reactorHandler).bind().block();
setPort(((InetSocketAddress) server.address()).getPort());
this.serverRef.set(server);
}
use of reactor.netty.DisposableServer in project reactor-netty by reactor.
the class ITTracingHttpServerDecoratorTest method init.
@Override
protected void init() {
HttpServerRoutes routes = HttpServerRoutes.newRoutes().options("/", (req, res) -> res.send()).get("/foo", (req, res) -> res.sendString(Mono.just("bar"))).get("/baggage", (req, res) -> res.sendString(Mono.just(req.requestHeaders().getAsString(BAGGAGE_FIELD_KEY)))).get("/exception", (req, res) -> Mono.error(NOT_READY_ISE)).get("/badrequest", (req, res) -> res.status(400).send()).get("/async", (req, res) -> res.sendString(Mono.just("body").publishOn(Schedulers.boundedElastic()))).get("/exceptionAsync", (req, res) -> Mono.error(NOT_READY_ISE).publishOn(Schedulers.boundedElastic()).then()).get("/items/{itemId}", (req, res) -> res.sendString(Mono.justOrEmpty(req.param("itemId")))).get("/async_items/{itemId}", (req, res) -> res.sendString(Mono.justOrEmpty(req.param("itemId")).publishOn(Schedulers.boundedElastic()))).get("/nested/items/{itemId}", (req, res) -> res.sendString(Mono.justOrEmpty(req.param("itemId")))).get("/child", (req, res) -> {
httpTracing.tracing().tracer().nextSpan().name("child").start().finish();
return res.send();
});
ReactorNettyHttpTracing reactorNettyHttpTracing = ReactorNettyHttpTracing.create(httpTracing, s -> {
if ("/foo/bark".equals(s)) {
return "not_found";
}
int ind = s.lastIndexOf('/');
if (s.length() > 1 && ind > -1) {
return s.substring(0, ind);
}
return s;
});
group = new DefaultChannelGroup(executor);
disposableServer = reactorNettyHttpTracing.decorateHttpServer(HttpServer.create().port(0).wiretap(true).forwarded(true).channelGroup(group).handle(routes)).bindNow();
}
use of reactor.netty.DisposableServer in project reactor-netty by reactor.
the class HttpServerTests method testNormalConnectionCloseForWebSocketClient.
@Test
void testNormalConnectionCloseForWebSocketClient() throws Exception {
Flux<String> flux = Flux.range(0, 100).map(n -> String.format("%010d", n));
AtomicReference<List<String>> receiver = new AtomicReference<>(new ArrayList<>());
AtomicReference<WebSocketCloseStatus> statusServer = new AtomicReference<>();
AtomicReference<WebSocketCloseStatus> statusClient = new AtomicReference<>();
CountDownLatch latch = new CountDownLatch(3);
List<String> test = flux.collectList().block();
assertThat(test).isNotNull();
disposableServer = createServer().handle((req, resp) -> resp.sendWebsocket((in, out) -> out.sendString(flux).then(out.sendClose(4404, "test")).then(in.receiveCloseStatus().doOnNext(o -> {
statusServer.set(o);
latch.countDown();
}).then()))).bindNow();
createClient(disposableServer.port()).websocket().uri("/").handle((in, out) -> {
in.receiveCloseStatus().doOnNext(o -> {
statusClient.set(o);
latch.countDown();
}).subscribe();
return in.receive().asString().doOnNext(s -> receiver.get().add(s)).doFinally(sig -> latch.countDown()).then(Mono.delay(Duration.ofMillis(500)));
}).blockLast();
assertThat(latch.await(30, TimeUnit.SECONDS)).isTrue();
assertThat(receiver.get()).containsAll(test);
assertThat(statusClient.get()).isNotNull().isEqualTo(new WebSocketCloseStatus(4404, "test"));
assertThat(statusServer.get()).isNotNull().isEqualTo(new WebSocketCloseStatus(4404, "test"));
}
Aggregations