Win a copy of Rust Web Development this week in the Other Languages forum!
  • 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:
  • Tim Cooke
  • Campbell Ritchie
  • Ron McLeod
  • Liutauras Vilda
  • Jeanne Boyarsky
Sheriffs:
  • Junilu Lacar
  • Rob Spoor
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Tim Moores
  • Jesse Silverman
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Piet Souris
  • Frits Walraven

Persistent Servlet

 
author
Posts: 4281
34
jQuery Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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?
 
Sheriff
Posts: 13411
Firefox Browser VI Editor Redhat
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Posts: 4281
34
jQuery Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 VI Editor Redhat
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I can move this to our EJB forum for you.
There is nothing in the servlet spec.
 
Sheriff
Posts: 67618
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
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.
 
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 VI Editor Redhat
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 ]
 
Trailboss
Posts: 23418
IntelliJ IDE Firefox Browser Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Does this help: http://www.javaranch.com/monitoring/
 
You showed up just in time for the waffles! And this tiny ad:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
reply
    Bookmark Topic Watch Topic
  • New Topic