• Post Reply Bookmark Topic Watch Topic
  • New Topic

Persistent Servlet

 
Scott Selikoff
author
Bartender
Posts: 4033
18
Eclipse IDE Flex Google Web Toolkit
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi All,

I'm trying to construct a servlet as a monitoring device for a set of remote computers. Basically the servlet pings the computers every few minutes and reports an error if a computer cannot be reached.

To accomplish this, I have created the servet with an init() method that contains an infinite while loop, set to sleep periodically. To start the servel, I have added a load-on-startup tag to the web.xml.

Problems: I've noticed when I reploy the application in Geronimo, the servlet fires during the deployment process and basically hangs the deployment. I tested this by making the loop terminate after a couple of minutes (instead of running forever), and as I expected, the deployment did not complete until the servlet finished.

Questions: Am supposed to be using doGet/doPost/service instead of init? Also, anyone have a better idea for a single threaded task in a J2EE environment than using a servlet with infinite loop?
 
Ben Souther
Sheriff
Posts: 13411
Firefox Browser Redhat VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Servlets should really only respond to requests.

The easiest approach for me is to use a cronjob with wget to hit hit a servlet every n seconds/minutes. This can usually be done with one line.

If it must be self contained within the app, I'd create an timer object with java.util.Timer or a library like Quarz and initialize it from a servletContextListener.
 
Scott Selikoff
author
Bartender
Posts: 4033
18
Eclipse IDE Flex Google Web Toolkit
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm trying to make it self-contained for easier deployment, so for now cronjob is out. Is there anything in EJB spec like session beans that can accomplish this type of process?
 
Ben Souther
Sheriff
Posts: 13411
Firefox Browser Redhat VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I can move this to our EJB forum for you.
There is nothing in the servlet spec.
 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 65530
108
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Like Ben, I'd avoid making this part of a web app, but if you insist, I'd spawn off a separate thread and not do this sort of thing in a servlet thread.
 
suri jagadish
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

If you really want to do it using the servlets then why don't you use the context Listener. This listener is called when the context is started or reloaded etc.
 
Ben Souther
Sheriff
Posts: 13411
Firefox Browser Redhat VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by suri jagadish:
Hi,

If you really want to do it using the servlets then why don't you use the context Listener. This listener is called when the context is started or reloaded etc.


If I'm not mistaken, that's been mentioned already in this thread.

[ April 03, 2008: Message edited by: Ben Souther ]
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!