This week's book giveaway is in the Performance forum.
We're giving away four copies of The Java Performance Companion and have Charlie Hunt, Monica Beckwith, Poonam Parhar, & Bengt Rutisson on-line!
See this thread for details.
Win a copy of The Java Performance Companion this week in the Performance forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Servlets&Threads

 
Marko Debac
Ranch Hand
Posts: 121
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Does anybody knows how could I make servlet who starts with app starting, and he create and start one thread with two different jobs, where each job is running separately for every.. (some defined time)? Maybe somebody have some code example?

Please help your little greenhorn!
[ October 10, 2006: Message edited by: Bear Bibeault ]
 
sven studde
Ranch Hand
Posts: 148
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
servlet who starts with app starting,

A ServletContextListener can be used for that. When your app starts, the container(e.g. Tomcat) creates a ServletContext object. And you can define a class that implements the ServletContextListener interface to detect the creation of the ServletContext object. The class will have two methods:

contextInitialized(ServletContextEvent event)
contextDestroyed(ServletContextEvent event)

If you put the name and location of your listener class in the web.xml file:



then the container will automatically create an object of your class and call the contextInitialized() method when your app starts.

Inside the contextInitialized() method you can write your one thread, two job program(whatever that means? ).

(The experts will most likely post about how that is the wrong way to organize your code and give you some better suggestions.)
[ October 10, 2006: Message edited by: sven studde ]
 
Rahul Bhattacharjee
Ranch Hand
Posts: 2308
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Marko Debac:
Hello you Indians!

Does anybody knows how could I make servlet who starts with app starting, and he create and start one thread with two different jobs, where each job is running separately for every.. (some defined time)? Maybe somebody have some code example?

Please help your little greenhorn!


You can create and start thread from the init method of the servler which runs with the start up of the application.That is enough.Make sure to make the threads as deamon threads.
 
Rashid Mayes
Ranch Hand
Posts: 160
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
As Sven stated, I also think a contextListener is the way to go. And you can place code to gracefully stop the threads in the contextDestroyed method.
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 64967
86
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Rahul Bhattacharjee:

You can create and start thread from the init method of the servler which runs with the start up of the application.


This is no longer a good way to go snce the inception of context listeners as pointed out by sven and Rashid. Before context listeners were introduced, "startup" servlets were the only means to accomplish this, but now there are better ways.
 
Marko Debac
Ranch Hand
Posts: 121
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi to all,

Tnx you all for the advice about ServletContextListiner, that is ok, but more precisely what I need is: user who uses web app put some data input - time_1 for task_1 to be started to run for every time_1, and time_2 for task_2 to be started to run for every time_2 (note: that all is happening while servlet runs those same tasks for other users only with diferent times), and then user klik the button - that same running servlet creates new Thread with those 2 tasks with new times for that particulary user_id, and it runs, and it runs (if the user go logout, servlet still runs Threads jobs, but now it runs one Thread more).
Maybe I need to put every new Thread in something called vector that I could find her latter with user_id so I could stop her or destroy her, but other threads must still running hers jobs. I am doing banghead for very long on this, can somebody call the sherif (or sherifs)!
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 64967
86
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It sounds as if you don't really need threads running in the web app, but perhaps consider a separate daemon application that queue up jobs using a database shared with the web app.
[ October 10, 2006: Message edited by: Bear Bibeault ]
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic