编程语言
首页 > 编程语言> > 嵌入式码头. java.lang.IllegalStateException:!STOPPED

嵌入式码头. java.lang.IllegalStateException:!STOPPED

作者:互联网

我试图在嵌入式jetty容器中制作简单的servlet.
这是我的码头配置:

public class Application {
  public static void main(String[] args) throws Exception {
    //-Dport=8188
    int port = 8188;
    if(System.getProperty("port") != null) {
      port = Integer.valueOf(System.getProperty("port"));
    }

    //-Dhost="127.0.0.1"
    String host = System.getProperty("host");
    if(host == null || host.isEmpty()) {
      host = "127.0.0.1";
    }

    ServletContextHandler context = new ServletContextHandler(ServletContextHandler.SESSIONS);
    context.setContextPath("/");

    InetAddress address = InetAddress.getByName(host);
    InetSocketAddress socketAddress = new InetSocketAddress(address, port);

    Server jettyServer = new Server(socketAddress);
    jettyServer.setHandler(context);

    ServletHolder jerseyServlet = context.addServlet(org.glassfish.jersey.servlet.ServletContainer.class, "/*");
    jerseyServlet.setInitOrder(0);

    // Initializing servlet
    jerseyServlet.setInitParameter(
      "jersey.config.server.provider.classnames",
      Proxy.class.getCanonicalName()
    );

    try {
      jettyServer.start();
      jettyServer.join();
    } finally {
      jettyServer.destroy();
    }
  }
}

我添加了一个最简单的servlet,它什么都不做.刚写了一些字符串来回复:

public class Proxy extends HttpServlet {
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
        throws ServletException, IOException {
        doRequest(request, response);
    }

    protected void doPost(HttpServletRequest request, HttpServletResponse response)
        throws ServletException, IOException {
        doRequest(request, response);
    }

    private void doRequest(HttpServletRequest request, HttpServletResponse response) throws IOException {
       PrintWriter writer = response.getWriter();
        writer.println("GOT IT!");
    }
}

当我启动它时,我收到一个错误:

2016-08-14 10:53:42.338:INFO::main: Logging initialized @110ms
2016-08-14 10:53:47.404:INFO:oejs.Server:main: jetty-9.3.z-SNAPSHOT
2016-08-14 10:53:47.923:INFO:oejsh.ContextHandler:main: Started o.e.j.s.ServletContextHandler@8458f04{/,null,AVAILABLE}
Exception in thread "main" java.lang.IllegalStateException: !STOPPED
    at org.eclipse.jetty.server.handler.HandlerWrapper.destroy(HandlerWrapper.java:135)
    at ru.gridr.Application.main(Application.java:50)

哪里出错了?
可能有人可以展示一些简单的例子吗?

解决方法:

当你执行jettyServer.destroy()时,你请求销毁

the managed Destroyable beans in the reverse order they were added

一旦服务器完成启动,就会执行此代码:

 jettyServer.join();

所以,你应该删除块:

 finally {
      jettyServer.destroy();
    }

因为我想你希望你的服务器在启动后继续工作.

我猜这个消息

Exception in thread “main” java.lang.IllegalStateException: !STOPPED

你应该在停止之后使用jettyServer.destroy(),我想在两种情况下:

>如果在服务器启动期间发生任何异常.
>如果你故意停止服务器.

如果在服务器启动期间发生任何异常,您可以尝试这样的事情:

  try {
      jettyServer.start();
      jettyServer.join();
    } catch (Exception e){
        logger.errror("error during server starting",e)
        jettyServer.stop();
        jettyServer.destroy();
    }

标签:servlet-3-0,java,jetty
来源: https://codeday.me/bug/20190727/1554253.html