java - 嵌入式 jetty 。 java.lang.IllegalStateException : ! 已停止

标签 java jetty servlet-3.0

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

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

所以,你应该删除 block :

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

关于java - 嵌入式 jetty 。 java.lang.IllegalStateException : ! 已停止,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38940330/

相关文章:

java - 实现并通过方法向 ArrayList 添加项目

java - 在 SWT 浏览器中运行 JQuery 脚本

java - Servlet 3.0环境下的web.xml文件在哪里?

java - 异步 Servlet 异常

java - spring-boot 如何提供特定的 url?

java - eclipse 中的 Tomcat WTP : route all requests to same Servlet

java - esper 根据事件开始时间固定窗口

eclipse - 为什么我无法通过安装新软件下载任何插件?

java - HttpClientTransportOverHTTP2 在哪里?

java - 当我运行 Spring 这个错误时常来?是这个版本的jar吗?