javajettyservlet-3.0

Embedded jetty. java.lang.IllegalStateException: !STOPPED


I trying to make simple servlet in embedded jetty container. This is my jetty configuration:

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

I add a simplest servlet, that does nothing. Just wrote some string to response:

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!");
    }
}

When I start it, I get an error:

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)

Where is the mistake? May be someone can show some simple example?


Solution

  • When you do jettyServer.destroy() , you request to Destroy

    the managed Destroyable beans in the reverse order they were added

    And this code is performed as soon as the server has finished his start :

     jettyServer.join();
    

    So, you should remove the block :

     finally {
          jettyServer.destroy();
        }
    

    because I imagine that you want that your server goes on working after its startup.

    I guess from the message

    Exception in thread "main" java.lang.IllegalStateException: !STOPPED

    that you should usejettyServer.destroy() only after stopping it and I suppose in two cases :

    If any exception occurs during the server startup, you could try something like that :

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