| Author |
Servlets&Threads
|
Marko Debac
Ranch Hand
Joined: Aug 21, 2006
Posts: 121
|
|
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
Joined: Sep 26, 2006
Posts: 148
|
|
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
Joined: Nov 29, 2005
Posts: 2300
|
|
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.
|
Rahul Bhattacharjee
LinkedIn - Blog
|
 |
Rashid Mayes
Ranch Hand
Joined: Jan 11, 2006
Posts: 160
|
|
|
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.
|
Rashid Mayes
http://www.hostj2me.com/ - http://www.worlddeveloper.org/
|
 |
Bear Bibeault
Author and ninkuma
Marshal
Joined: Jan 10, 2002
Posts: 56554
|
|
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.
|
[Smart Questions] [JSP FAQ] [Books by Bear] [Bear's FrontMan] [About Bear]
|
 |
Marko Debac
Ranch Hand
Joined: Aug 21, 2006
Posts: 121
|
|
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
Joined: Jan 10, 2002
Posts: 56554
|
|
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 ]
|
 |
 |
|
|
subject: Servlets&Threads
|
|
|