@Override public void onApplicationEvent(ApplicationEvent event) { ApplicationContext context = ((ContextRefreshedEvent) event) .getApplicationContext(); if (context != this.context) { return; } if (!ClassUtils.isPresent( "org.springframework.http.server.reactive.HttpHandler", null)) { logger.info("No web server classes found so no server to start"); return; } Integer port = Integer.valueOf(context.getEnvironment() .resolvePlaceholders("${server.port:${PORT:8080}}")); String address = context.getEnvironment() .resolvePlaceholders("${server.address:0.0.0.0}"); if (port >= 0) { HttpHandler handler = context.getBean(HttpHandler.class); ReactorHttpHandlerAdapter adapter = new ReactorHttpHandlerAdapter( handler); HttpServer httpServer = HttpServer.create().host(address).port(port) .handle(adapter); Thread thread = new Thread(() -> httpServer .bindUntilJavaShutdown(Duration.ofSeconds(60), this::callback), "server-startup"); thread.setDaemon(false); thread.start(); } }
@Override public void onApplicationEvent(ApplicationEvent event) { ApplicationContext context = ((ContextRefreshedEvent) event) .getApplicationContext(); if (context != this.context) { return; } if (!ClassUtils.isPresent( "org.springframework.http.server.reactive.HttpHandler", null)) { logger.info("No web server classes found so no server to start"); return; } Integer port = Integer.valueOf(context.getEnvironment() .resolvePlaceholders("${server.port:${PORT:8080}}")); String address = context.getEnvironment() .resolvePlaceholders("${server.address:0.0.0.0}"); if (port >= 0) { HttpHandler handler = context.getBean(HttpHandler.class); ReactorHttpHandlerAdapter adapter = new ReactorHttpHandlerAdapter( handler); HttpServer httpServer = HttpServer.create().host(address).port(port) .handle(adapter); Thread thread = new Thread(() -> httpServer .bindUntilJavaShutdown(Duration.ofSeconds(60), this::callback), "server-startup"); thread.setDaemon(false); thread.start(); } }
@Test public void startRouterAndAwait() throws InterruptedException { ExecutorService ex = Executors.newSingleThreadExecutor(); AtomicReference<DisposableServer> ref = new AtomicReference<>(); Future<?> f = ex.submit(() -> HttpServer.create() .port(0) .route(routes -> routes.get("/hello", (req, resp) -> resp.sendString(Mono.just("hello!")))) .wiretap(true) .bindUntilJavaShutdown(Duration.ofSeconds(2), ref::set) ); //if the server cannot be started, a ExecutionException will be thrown instead assertThatExceptionOfType(TimeoutException.class) .isThrownBy(() -> f.get(1, TimeUnit.SECONDS)); //the router is not done and is still blocking the thread assertThat(f.isDone()).isFalse(); assertThat(ref.get()).isNotNull().withFailMessage("Server is not initialized after 1s"); //shutdown the router to unblock the thread ref.get().disposeNow(); Thread.sleep(100); assertThat(f.isDone()).isTrue(); }
public static void start(ConfigurableApplicationContext context, Consumer<DisposableServer> callback) { if (!hasListeners(context)) { ((DefaultListableBeanFactory) context.getBeanFactory()) .registerDisposableBean(SHUTDOWN_LISTENER, new ShutdownApplicationListener()); new BeanCountingApplicationListener().log(context); logger.info(STARTUP); } HttpHandler handler = WebHttpHandlerBuilder.applicationContext(context).build(); ReactorHttpHandlerAdapter adapter = new ReactorHttpHandlerAdapter(handler); HttpServer httpServer = HttpServer.create().host("localhost").port( context.getEnvironment().getProperty("server.port", Integer.class, 8080)) .handle(adapter); httpServer.bindUntilJavaShutdown(Duration.ofSeconds(60), callback(callback)); }
public static void start(ConfigurableApplicationContext context, Consumer<DisposableServer> callback) { if (!hasListeners(context)) { ((DefaultListableBeanFactory) context.getBeanFactory()) .registerDisposableBean(SHUTDOWN_LISTENER, new ShutdownApplicationListener()); new BeanCountingApplicationListener().log(context); logger.info(STARTUP); } HttpHandler handler = WebHttpHandlerBuilder.applicationContext(context).build(); ReactorHttpHandlerAdapter adapter = new ReactorHttpHandlerAdapter(handler); HttpServer httpServer = HttpServer.create().host("localhost").port( context.getEnvironment().getProperty("server.port", Integer.class, 8080)) .handle(adapter); httpServer.bindUntilJavaShutdown(Duration.ofSeconds(60), callback(callback)); }