简体   繁体   中英

Shutdown Embedded Jetty Server programmatically in Eclipse RCP

I have embedded Jetty into my Eclipse RCP application successfully. In my RCP application, when user click some button, a browser will be opened and some servlet page shown. The jsp files are in a separated directory, it is a angulajs web application.

I am trying to shutdown embedded Jetty server from Eclipse UI plugin when user closes the RCP.The server is started in a class named Workshop which is part of web project, so I dont have access to Server instance to call, server.stop() from Eclipse UI Plugin.I have tried below code, but in vein. 1>Configure ShutdownHook to Workshop class of web project

server = new Server();
server.setConnectors(new Connector[] { connector });
server.setHandler(handlers);
server.start();
handlers.addHandler(new ShutdownHandler(server, "abc"));
server.setStopAtShutdown(true);
server.setGracefulShutdown(7_000);
ShutdownThread.getInstance().run();

2> In my Eclipse UI Plugin, I have added

Runtime.getRuntime().addShutdownHook(new Thread() {
            @Override
            public void run() {
               try {
            URL url = new URL("http://localhost:" + resultPortNo + "/shutdown?token=" + shutdownCookie);
            HttpURLConnection connection = (HttpURLConnection)url.openConnection();
            connection.setRequestMethod("POST");
            connection.getResponseCode();
           logger.info("Shutting down " + url + ": " + connection.getResponseMessage());
        } catch (SocketException e) {
           // logger.debug("Not running");
            // Okay - the server is not running
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
            }
        }); 

HtppUrlConnection throws 404-NotFound response code.So Jetty server is still running. How do we handle embedded Jetty shutdown from Eclipse UI Plugin.

I did read lot of articles, but cannot find answer to my question.

Any help will be appreciated.Thank you.

Problem : Every time jettyServer.stop was called, Interrupt Exception was thrown and jetty server continued to run.

Solution : (1) Added Executor Service with daemon thread in the Servlet code to stop the Jetty server

JettyShutdownServlet.js
-----------------------
    ExecutorService pool = Executors.newSingleThreadExecutor(new ThreadFactory() {
                @Override
                public Thread newThread(Runnable runnable) {
                    Thread thread = Executors.defaultThreadFactory().newThread(runnable);
                    thread.setDaemon(true);
                    return thread;
                }
            });

            pool.execute(new Runnable() {
                @Override
                public void run() {
                    if (null != jettyServer) {
                        try {
                            jettyServer.stop();
                            jettyServer.join();
                        } catch (Exception e) {
                            logger.info("Error when stopping Jetty: " + e.getMessage());
                        }
                    }

                }
            });

(2) Adding the servlet inside the Jetty startup code.

servletContextHandler.addServlet(new ServletHolder(new JettyShutdownServlet(server)), "/shutdown");

(3) Adding shutdownhook to Eclipse UI class

Runtime.getRuntime().addShutdownHook(new Thread() {
            @Override
            public void run() {
            try {
            String shutdownurl = "http://localhost:" + resultPortNo + "/api/shutdown";
            URL url = new URL(shutdownurl);
            HttpURLConnection connection =(HttpURLConnection)url.openConnection();
            connection.setRequestMethod("GET");
            try {
               connection.getResponseCode();
               connection.disconnect();
            } catch (SocketException e) {
               // logger.debug("Not running");
               // Okay - the server is not running
               connection.disconnect();
          }
        } catch (Exception x) {
        // exception during shutdown
        Activator.error("Unable to shutdown Jetty Server.\n" + x.getMessage());
                }
            }
        });

This solved my problem and hope it will be of some help to others.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM