[英]Shutdown Embedded Jetty Server programmatically in Eclipse RCP
I have embedded Jetty into my Eclipse RCP application successfully. 我已成功将Jetty嵌入到我的Eclipse RCP应用程序中。 In my RCP application, when user click some button, a browser will be opened and some servlet page shown.
在我的RCP应用程序中,当用户单击某个按钮时,将打开一个浏览器并显示一些servlet页面。 The jsp files are in a separated directory, it is a angulajs web application.
jsp文件位于一个单独的目录中,它是一个angulajs Web应用程序。
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. 当用户关闭RCP时,我试图从Eclipse UI插件关闭嵌入式Jetty服务器。服务器在一个名为
Workshop
的类中启动,这是Web项目的一部分,所以我没有访问要调用的Server实例,server.stop()来自Eclipse UI Plugin。我尝试了下面的代码,但确实如此。 1>Configure ShutdownHook to Workshop
class of web project 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> In my Eclipse UI Plugin, I have added 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 throws 404-NotFound response code.So Jetty server is still running. HtppUrlConnection抛出404-NotFound响应代码。所以Jetty服务器仍在运行。 How do we handle embedded Jetty shutdown from Eclipse UI Plugin.
我们如何处理Eclipse UI插件中的嵌入式Jetty关闭。
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. 问题:每次调用jettyServer.stop时,都会引发中断异常,并且jetty服务器继续运行。
Solution : (1) Added Executor Service with daemon thread in the Servlet code to stop the Jetty server 解决方案:(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) Adding the servlet inside the Jetty startup code. (2)在Jetty启动代码中添加servlet。
servletContextHandler.addServlet(new ServletHolder(new JettyShutdownServlet(server)), "/shutdown");
(3) Adding shutdownhook to Eclipse UI class (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());
}
}
});
This solved my problem and hope it will be of some help to others. 这解决了我的问题,并希望它对其他人有所帮助。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.