本文整理了Java中reactor.netty.http.server.HttpServer.bindUntilJavaShutdown()
方法的一些代码示例,展示了HttpServer.bindUntilJavaShutdown()
的具体用法。这些代码示例主要来源于Github
/Stackoverflow
/Maven
等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。HttpServer.bindUntilJavaShutdown()
方法的具体详情如下:
包路径:reactor.netty.http.server.HttpServer
类名称:HttpServer
方法名:bindUntilJavaShutdown
[英]Start a Server in a fully blocking fashion, not only waiting for it to initialize but also blocking during the full lifecycle of the client/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 sigkill.
Note that a Runtime#addShutdownHook(Thread) is added by this method in order to properly disconnect the client/server upon receiving a sigkill signal.
[中]以完全阻塞的方式启动服务器,不仅等待它初始化,而且在客户机/服务器的整个生命周期中进行阻塞。因为大多数服务器都是长寿命的,所以这更适合于在主方法之外运行服务器,只允许通过sigkill关闭服务器。
请注意,此方法添加了运行时#addShutdownHook(线程),以便在接收到sigkill信号时正确断开客户机/服务器的连接。
代码示例来源:origin: spring-cloud/spring-cloud-function
@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();
}
}
代码示例来源:origin: org.springframework.cloud/spring-cloud-function-web
@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();
}
}
代码示例来源:origin: reactor/reactor-netty
@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();
}
代码示例来源:origin: dsyer/spring-boot-micro-apps
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));
}
代码示例来源:origin: dsyer/spring-boot-allocations
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));
}
内容来源于网络,如有侵权,请联系作者删除!