• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Servlets&Threads

 
Ranch Hand
Posts: 121
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 ]
 
Ranch Hand
Posts: 148
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 ]
 
Ranch Hand
Posts: 2308
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 160
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Sheriff
Posts: 67746
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Sheriff
Posts: 67746
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 ]
reply
    Bookmark Topic Watch Topic
  • New Topic