灰熊HTTP服务器应保持运行状态。

14

以下是启动Grizzly Http服务器的代码。如果我按任意键,服务器将停止。有没有办法让它保持运行状态。

Jetty有join()方法,它不会退出主程序。Grizzly中也有类似的方法吗?

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();

        }

根据上面的代码,如果你按任何键,服务器将停止。 我想让它继续运行。 当我真正想要停止服务器时,我将杀死该进程。主方法不应该终止。

谢谢

4个回答

26

我使用了一个关闭钩子(shutdown 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);
    }
}

}

2
尝试以下操作: ```尝试以下操作:```
    try {
        server.start();
        Thread.currentThread().join();
    } catch (Exception ioe) {
        System.err.println(ioe);
    } finally {
        try {
            server.stop();
        } catch (IOException ioe) {
            System.err.println(ioe);
        }
    }

1
服务器停止是因为您在输入流之后调用了httpServer.stop()方法。当执行到System.in.read();时,它会一直挂起,直到您输入一个字母,然后继续执行服务器停止操作。
您可以注释掉httpServer.stop(),因为该代码示例正是为了在按键时挂起服务器。
但是,如果您想创建Webserver实例,我建议您在main()中运行一个线程,启动Grizzly Webserver的实例。

0

更新 2022/11/23

.stop() 函数已被弃用,现在更安全的做法是使用 .shutdown()

注意

如果您正在使用 WELD 或其他 CDI 容器,则应将其关闭

    final Weld weld = new Weld();
    weld.initialize();

    ResourceConfig resourceConfig = new ResourceConfig();
    resourceConfig.packages("loc.java.my.res.pack");

    final HttpServer server = GrizzlyHttpServerFactory.createHttpServer(URI.create("http://localhost:9000/"),
                resourceConfig);

    Runtime.getRuntime().addShutdownHook(new Thread(new Runnable() {
        @Override
        public void run() {
            logger.info("Stopping server..");
            server.shutdown();//.stop() has been deprecated
            weld.shutdown();//check your CDI container shutdown function
        }
    }, "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);
    }

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接