簡體   English   中英

在Eclipse RCP中以編程方式關閉嵌入式Jetty服務器

[英]Shutdown Embedded Jetty Server programmatically in Eclipse RCP

我已成功將Jetty嵌入到我的Eclipse RCP應用程序中。 在我的RCP應用程序中,當用戶單擊某個按鈕時,將打開一個瀏覽器並顯示一些servlet頁面。 jsp文件位於一個單獨的目錄中,它是一個angulajs Web應用程序。

當用戶關閉RCP時,我試圖從Eclipse UI插件關閉嵌入式Jetty服務器。服務器在一個名為Workshop的類中啟動,這是Web項目的一部分,所以我沒有訪問要調用的Server實例,server.stop()來自Eclipse UI Plugin。我嘗試了下面的代碼,但確實如此。 1>將ShutdownHook配置為Web項目的Workshop

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>在我的Eclipse UI插件中,我添加了

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拋出404-NotFound響應代碼。所以Jetty服務器仍在運行。 我們如何處理Eclipse UI插件中的嵌入式Jetty關閉。

我確實閱讀了很多文章,但是找不到我的問題的答案。

任何幫助將不勝感激。謝謝。

問題:每次調用jettyServer.stop時,都會引發中斷異常,並且jetty服務器繼續運行。

解決方案:(1)在Servlet代碼中添加帶有守護程序線程的Executor Service以停止Jetty服務器

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)在Jetty啟動代碼中添加servlet。

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

(3)將shutdownhook添加到Eclipse UI類

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

這解決了我的問題,並希望它對其他人有所幫助。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM