Win a copy of Murach's Python Programming this week in the Jython/Python forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Starting a thread when an application is started  RSS feed

 
Xolani Nkosi
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
After I deploy and start an enterprise application (if it's relevant, in Websphere 7.0) is there a standards compliant way to kick off piece of code that is guaranteed to run, so that I can start some daemon threads etc. I guess I'm looking for something like a public static final void main() method, but one the application server will call when the application starts. Is there something in the deployment descriptor that specifies this? I've thought about adding it as a static {...} block in a class, but that feels a bit messy, plus then I'm left with trying to get the class loader to load that class, which it wouldn't do without some sort of event to trigger it.


 
Gokul Nanthakumar
Ranch Hand
Posts: 56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
create a servlet and keep your logic in the init() of the servlet and in web.xml set +ve value for the load-on-startup
 
Xolani Nkosi
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Gokul Nanthakumar wrote:create a servlet and keep your logic in the init() of the servlet and in web.xml set +ve value for the load-on-startup

So this should do the job? There any risk the container will decide to destroy the servlet early if it detects nothing has used it in a while?
 
Gokul Nanthakumar
Ranch Hand
Posts: 56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
As per your requirement you need to do something when an application starts, so it will solve your problem, after that even if container destroys the servlet, I dont think there is a problem.
 
Pallav Bora
Ranch Hand
Posts: 73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Another option you can try is using a ServletContextListener if your application has a web module.

WAS has ways to specify starup class. You may want to explore more about that.
 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 65826
134
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Gokul Nanthakumar wrote:create a servlet and keep your logic in the init() of the servlet and in web.xml set +ve value for the load-on-startup

That's a poor way of doing it. A context listener, as pointed out in the previous reply is the correct way to initiate startup code.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!