Skip to Main Content

Java EE (Java Enterprise Edition) General Discussion

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

scheduling a JSP/servlet

843836Feb 7 2005 — edited Feb 9 2005
hello friends,

I have a job which I want to run once in a day. It just fetch some records from the database, make come check and send mail to clients.
I am using mysql and tomcat.
One method which i found, is to start a thread in the init/jspinit method, and make it to sleep for 24 hrs. this thread will run forever and serve my purpose, but drawback is that i wont have any control on the thread.
To slove this prob i have stored the object of the class(which is performing the back end operation) in the servletContext like this:
public void jspInit()
{
testThread th=new testThread();
getServletConfig().getServletContext().setAttribute("testThread",th);
new Thread(th).start();
testThread is the class performing the operation.
}

now in the code snippet of the page i write
if(request.getParameter("stop")!=null){
out.println("Stoping thread...");
testThread th1=(testThread)getServletConfig().getServletContext().getAttribute("testThread");
th1.stopThread();
}

so that when second time i will have to stop the thread i pass the parameter stop and till will stop the thread.

run() method of my thread c lass looks like this:
public void run(){
while(flag){
//// do the back end operation
try {
Thread.sleep(10000);
} catch (InterruptedException e) {
writeLog.write(cl,e.toString());
}
if(!flag) return;
}
}


code is working fine but...............
if any one can c some issues with this approach then please tell me. I have write this code but i dont the what can be its consicounces............ pl let me know if u find somthing....
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Mar 9 2005
Added on Feb 7 2005
3 comments
275 views