aspose file tools*
The moose likes Servlets and the fly likes a periodic servlet Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Servlets
Bookmark "a periodic servlet" Watch "a periodic servlet" New topic
Author

a periodic servlet

Ragav Seth
Greenhorn

Joined: Jan 17, 2005
Posts: 16
Hi guys,

Any ideas for creating a servlet executing in background periodically for every 1 min?? This servlet will keep reading a table in the database and will set a variable when a new row is added in the table.

Any ideas?? sorry for a naive question.

--Ragav
David O'Meara
Rancher

Joined: Mar 06, 2001
Posts: 13459

Servlets are supposed to work on a request/response basis. What you are describing is not a Servlet. Better to put it in a separate class and make it usable by the Servlets.
Rajesh Agarwal
Ranch Hand

Joined: Aug 01, 2005
Posts: 79
Seth,
Tho it would be a bad design to have a servlet run in the background every minute, but I think legally it is possible to do it.
Neeraj Dheer
Ranch Hand

Joined: Mar 30, 2005
Posts: 225
Originally posted by Ragav Seth:

Any ideas for creating a servlet executing in background periodically for every 1 min?? This servlet will keep reading a table in the database and will set a variable when a new row is added in the table.

--Ragav


What is the desired end result? What is the variable supposed to do?
I agree with the above posts in that this would not be good Servlet Design.
William Brogden
Author and all-around good cowpoke
Rancher

Joined: Mar 22, 2000
Posts: 12806
    
    5
The basic idea of a servlet is to respond to a request. The Thread that handles the request is created and managed by the servlet container.
If you want to create a Runnable object with its own Thread to do this background task, just do it in the servlet init() method.
Describing this problem as "creating a servlet executing in background periodically for every 1 min?" is a mistake.
Bill
David Bridgewater
author
Ranch Hand

Joined: Apr 29, 2004
Posts: 44
I'm probably way out of line here... but looking at William's point (start your own thread)... don't some web containers resent you starting your own user-defined threads? I can't find anything in the J2EE servlet spec that says you can't do this, mind (I just recall an IBM development tool that identifies this as "bad form").

If there are compelling reasons to place the "every minute" processing in a servlet, I could think of a couple of approaches...

(1) Have a servlet which loads-on-startup, but has no servlet mapping. The every-minute process is called by the init(ServletConfig config) method, which runs a perpetual loop that performs the process and puts its thread to sleep for a minute.

(2) Have the process in a regular servlet. Have a completely separate piece of Java code (an application running in its own JVM). A process in this wakes up every minute, and fires an HTTP request to run the servlet.

Best,

David.
David Bridgewater
author
Ranch Hand

Joined: Apr 29, 2004
Posts: 44
Afterthought (having read the original question more carefully)... what is wanted here is for the database to update information within the web applicaton. If the database supports it, I would add a trigger which fires when a row is added. The trigger program could send an HTTP request to a servlet, probably including data from the newly added row. This saves the bother of the servlet re-reading the table.

Also, you don't then need a separate program which wastefully polls the table when nothing has changed.

Hope that makes sense.

Best,

David.
Ragav Seth
Greenhorn

Joined: Jan 17, 2005
Posts: 16
Hello Everyone,

Thank you very much for all of your replies.Infact I had forgot that i had posted this question.It now pains me to know that Iam late in acknowledging your replies.Sorry about that.

I did solve the problem but I have to review my code to explain what I did since its been a month now.I will do it in the next post.

Thanks
Ragav Seth
Ragav Seth
Greenhorn

Joined: Jan 17, 2005
Posts: 16
The solution Dont know if this is a correct way of doing it).

Iam calling a servlet when i load a jsp page.The servlet calls a thread which executes every minute.The init method of the servlet opens a database connection and gets the number of rows in the table and stores it in a variable.

In the dopost method i call the above mentioned thread.The thread again polls the same table,gets the number of rows and stores in another variable.I keep comparing the two variables.If there is row update i do

out.println("<meta http-equiv='refresh' content='1'/>");
out.println("<html>");
out.println("<body bgcolor='#CCFFFF'>");

if(var1>var2)

out.println("<font color='#CC0033'>");
out.println("You have an Alert");
out.println("</font>");

else
out.println("<font color='#66CC33'>");
out.println("No Alert");
out.println("</font>");

Iam using mysql4.1.18.I guess it does not support triggers.

Any suggestions about my design idea?? Is it ok to call a user defined thread??

Thanks
Ragav
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: a periodic servlet