[英]Deploying Java Application As Servlet
I have a java application that up until now was run as a stand alone java application (ie executable jar). 我有一个Java应用程序,到目前为止,它是作为独立的Java应用程序(即可执行jar)运行的。 I now need to deploy it in Tomcat as a servlet.
我现在需要将它作为servlet部署在Tomcat中。 It doesn't need to actually process any HTTP requests though, but it needs to be started using tomcat.
虽然它实际上不需要处理任何HTTP请求,但是需要使用tomcat启动它。
What are the steps required to convert the project so that it can be deployed in Tomcat? 转换项目以便可以在Tomcat中部署需要什么步骤? I'm using maven as a build tool and Java 1.5.
我正在使用maven作为构建工具和Java 1.5。
I understand that you want to run this app on server's startup. 我了解您要在服务器启动时运行此应用程序。 The best way would be implementing
ServletContextListener
and run the app in the contextInitialized()
method. 最好的方法是实现
ServletContextListener
并在contextInitialized()
方法中运行应用程序。 Eg 例如
public class Config implements ServletContextListener {
private YourApp yourApp;
public void contextInitialized(ServletContextEvent event) {
yourApp = new YourApp();
yourApp.start();
}
public void contextDestroyed(ServletContextEvent event) {
yourApp.shutdown();
}
}
Register this in web.xml
as follows: 如下所示在
web.xml
注册:
<listener>
<listener-class>com.example.Config</listener-class>
</listener>
That's it. 而已。 No need to wrap it in flavor of a
HttpServlet
as you aren't going to fire HTTP requests on it. 无需将其包装为
HttpServlet
因为您不会在其上触发HTTP请求。
You however need to ensure that it runs in its own thread, otherwise it would block the startup. 但是,您需要确保它在自己的线程中运行,否则会阻止启动。 If it doesn't, then wrap it in a
Runnable
and execute it using ExecutorService
. 如果不是,则将其包装在
Runnable
,并使用ExecutorService
执行它。
I'm assuming that your app is continuously running and you have an app/web server already (eg Tomcat/Jetty), such that it's making your life easy to deploy into it. 我假设您的应用程序一直在运行,并且您已经有一个应用程序/ Web服务器(例如Tomcat / Jetty),这样一来,您的生活就可以轻松部署到其中。 Given that, you need to:
鉴于此,您需要:
AbstractHttpServlet
class and in particular the init() method. AbstractHttpServlet
类,尤其是init()方法。 This would start your app. web.xml
that references this and sets the load-on-startup attribute to 1 (or at least non-zero) web.xml
并将启动时加载属性设置为1(或至少非零) Step 2 ensures that the init()
method is called upon deployment/server reboot, and so you don't have to respond to HTTP requests (a normal startup trigger for a servlet). 第2步确保在部署/服务器重新启动时调用
init()
方法,因此您不必响应HTTP请求(Servlet的常规启动触发器)。
It may be simpler and more appropriate to use something like javaservicewrapper , and wrap it up to be a Windows service or similar. 使用javaservicewrapper之类的东西,将其包装为Windows服务或类似的东西,可能会更简单,更合适。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.