如何优雅地关闭嵌入式码头 [英] How to gracefully shutdown embeded jetty

查看:207
本文介绍了如何优雅地关闭嵌入式码头的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个应用程序在嵌入式jetty服务器上运行。现在我想启动/停止服务器作为服务。
我使用脚本启动服务器。

I have an application runs on an embedded jetty server. Now i want to start/stop the server as a service. I use a script to start the server.

java $JAVA_OPTS -DREQ_JAVA_VERSION=$JAVA_VERSION -jar myjetty.jar

主类

Server server = new Server();
SelectChannelConnector connector = new SelectChannelConnector();
connector.setPort(PORT);
server.addConnector(connector);
HandlerCollection handlers = new HandlerCollection();
NCSARequestLog requestLog = new NCSARequestLog();
requestLog.setFilename(home + "/logs/access_" + logFileDateFormat
            + ".log");
requestLog.setFilenameDateFormat(logFileDateFormat);
requestLog.setRetainDays(10);
requestLog.setAppend(true);
requestLog.setExtended(false);
requestLog.setLogCookies(false);
requestLog.setLogTimeZone(TimeZone.getDefault().getID());
RequestLogHandler requestLogHandler = new RequestLogHandler();
requestLogHandler.setRequestLog(requestLog);
handlers.addHandler(requestLogHandler);
server.setHandler(handlers);
server.start();
server.join();

这将启动服务器。通过网络电话停止和/或重新启动嵌入式Jetty实例可以用来停止服务器但是,
如何停止脚本中的服务器?我应该做什么改变来喊出主类中的服务器。

This starts the server.Stopping and/or Restarting an embedded Jetty instance via web call can be used to stop server but, How to stop the server from the script? and what changes should i make to shout down server in the main class.

推荐答案

没有预定义的解决方案来关闭Jetty服务器。关闭Jetty服务器的唯一有序方法是在正在运行的服务器实例上调用方法 stop()。您必须实现自己调用此方法的方式。

There is no predefined solution to shut-down the Jetty server. The only ordered way to shut-down the Jetty server is to call the method stop() on the running server instance. You must implement the way how this method is called yourself.

您可以通过...实现此目的(例如)...

You could achieve this (for example) by...


  • 实现RMI服务器线程并从RMI客户端调用该方法

  • 实现JMX MBean,并从客户端调用该MBean上的方法

  • 实现自己发布的链接中描述的自定义处理程序

如果你只想要找到一种不依赖于其他工具的方法,比如 curl ,你可以解决它,例如下面的代码(这是你自己的代码,修改很少)

If you only want to find a way which does not depend on additional tools like curl, than you could solve it for example like below (it's your own code with small modifications)

public class MyJetty {

    public static void main(String[] args) throws Exception {
        int PORT = 9103;
        String home = System.getProperty("user.home");
        String logFileDateFormat = "yyyy_MM_dd";

        // execute a request to http://localhost:9103/stop
        // instead of `curl -v http://localhost:9103/stop`
        if (args.length == 1 && "stop".equalsIgnoreCase(args[0])) {
            URL url = new URL("http", "localhost", PORT, "/stop");
            try (InputStream in = url.openStream()) {
                int r;
                while ((r = in.read()) != -1) {
                    System.out.write(r);
                }
                return;
            } catch (IOException ex) {
                System.err.println("stop Jetty failed: " + ex.getMessage());
            }
        }

        Server server = new Server();
        SelectChannelConnector connector = new SelectChannelConnector();
        connector.setPort(PORT);
        server.addConnector(connector);
        HandlerCollection handlers = new HandlerCollection();
        NCSARequestLog requestLog = new NCSARequestLog();
        requestLog.setFilename(home + "/logs/access_" + logFileDateFormat + ".log");
        requestLog.setFilenameDateFormat(logFileDateFormat);
        requestLog.setRetainDays(10);
        requestLog.setAppend(true);
        requestLog.setExtended(false);
        requestLog.setLogCookies(false);
        requestLog.setLogTimeZone(TimeZone.getDefault().getID());
        RequestLogHandler requestLogHandler = new RequestLogHandler();
        requestLogHandler.setRequestLog(requestLog);
        handlers.addHandler(requestLogHandler);

        // the class YourHandler is the one from your link
        handlers.addHandler(new YourHandler(server));

        server.setHandler(handlers);
        server.start();
        server.join();
    }
}




  • 启动服务器 java MyJetty

  • 使用停止服务器java MyJetty stop

    • start the server with java MyJetty
    • stop the server with java MyJetty stop
    • 这篇关于如何优雅地关闭嵌入式码头的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆