Win a copy of The Java Performance Companion this week in the Performance forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

How to Start a service automatically when the tomcat starts?

 
Muralidharan Raju
Greenhorn
Posts: 25
Eclipse IDE Firefox Browser Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hey folks,

i need to run an application that can run automatically that when the tomcat starts..?
any suggestions...?
 
Kumar Raja
Ranch Hand
Posts: 547
2
Hibernate Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Muralidharan Raju wrote:Hey folks,

i need to run an application that can run automatically that when the tomcat starts..?
any suggestions...?


Hi Muralidharan,

What do you mean by an application here? Since you posted in webservices forum, I assume that you are referring to a webservice. As long as the service is deployed properly in webcontainer, when you start the tomcat, it would automatically starts the service just like any other web application. Please be aware that webservice deployed in webcontainer is nothing but a service wrapped in a servlet which looks for SOAP request over HTTP just like any other servlet.
 
Ivan Krizsan
Ranch Hand
Posts: 2198
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi!
One basic approach is to configure a servlet to be eagerly loaded by using the <load-on-startup> element in a <servlet> element in the web.xml deployment descriptor or, if you are using Tomcat 7 (or any other container implementing the servlet 3 specification), the loadOnStartup element in the @WebServlet annotation.
In the servlet that is eagerly loaded, you also need an initialization method - this method will be called when the servlet is loaded, which will be when Tomcat is started.
The following is an example of such a servlet using the new servlet 3 annotations:

Best wishes!
 
Kumar Raja
Ranch Hand
Posts: 547
2
Hibernate Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Ivan,

I'm just curious, if we need to setup load-on-startup explicitly. If the application is deployed, and when the tomcat starts, would this not make the app available implicitly.
 
Ivan Krizsan
Ranch Hand
Posts: 2198
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi!
If an application is deployed and enabled, the it will automatically be available when Tomcat has started up.
The load-on-startup property facilitates eager loading of servlets. If the servlet is not eagerly loaded, then it will still be loaded when a request that matches its URL patter arrives.
Eager loading enables us to eagerly load a servlet which may require long time to, for instance, acquire resources. This to avoid waiting time when the first request arrives.
Best wishes!
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic