gpt4 book ai didi

grizzly http 服务器应该继续运行

转载 作者:行者123 更新时间:2023-12-04 01:35:17 25 4
gpt4 key购买 nike

下面是启动 Grizzly Http Server 的代码。如果我按任何键,服务器就会停止。有什么办法让它活着。

Jetty 有 join() 方法,不会退出主程序。灰熊中是否也有类似的东西。

public static void main(String args){



ResourceConfig rc = new PackagesResourceConfig("com.test.resources");

HttpServer httpServer = GrizzlyServerFactory.createHttpServer(BASE_URI, rc);
logger.info(String.format("Jersey app started with WADL available at "
+ "%sapplication.wadl\nTry out %shelloworld\nHit enter to stop it...",
BASE_URI, BASE_URI));

System.in.read();
httpServer.stop();

}

根据上面的代码,如果您按下任何键,服务器就会停止。我想让它继续运行。当我真正想停止服务器时,我会终止该进程。 main 方法不应终止。

谢谢

最佳答案

我使用关机钩子(Hook)。这是一个代码示例:

public class ExampleServer {
private static final Logger logger = LoggerFactory
.getLogger(ExampleServer.class);

public static void main(String[] args) throws IOException {
new Server().doMain(args);
}

public void doMain(String[] args) throws IOException {
logger.info("Initiliazing Grizzly server..");
// set REST services packages
ResourceConfig resourceConfig = new PackagesResourceConfig(
"pt.lighthouselabs.services");

// instantiate server
final HttpServer server = GrizzlyServerFactory.createHttpServer(
"http://localhost:8080", resourceConfig);

// register shutdown hook
Runtime.getRuntime().addShutdownHook(new Thread(new Runnable() {
@Override
public void run() {
logger.info("Stopping server..");
server.stop();
}
}, "shutdownHook"));

// run
try {
server.start();
logger.info("Press CTRL^C to exit..");
Thread.currentThread().join();
} catch (Exception e) {
logger.error(
"There was an error while starting Grizzly HTTP server.", e);
}
}

}

关于grizzly http 服务器应该继续运行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14558079/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com