Embedded wharf java. lang.IllegalStateException:! STOPPED

I tried to make a simple servlet in an embedded jetty container

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 added the simplest servlet that does nothing Just wrote some strings to reply:

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

    protected void doPost(HttpServletRequest request,response);
    }

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

When I started it, I received 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)

What went wrong? May someone show some simple examples?

Solution

When you execute jettyserver When destroy(), you request destruction

Once the server has finished booting, this code will be executed:

jettyServer.join();

Therefore, you should delete the block:

finally {
      jettyServer.destroy();
    }

Because I think you want your server to continue working after it starts

I guess the news

You should use jettyserver after stopping Destroy (), I think in two cases:

>If any exception occurs during server startup. > If you deliberately stop the server

If any exception occurs during server startup, you can try something like this:

try {
      jettyServer.start();
      jettyServer.join();
    } catch (Exception e){
        logger.errror("error during server starting",e)
        jettyServer.stop();
        jettyServer.destroy();
    }
The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>