Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

TimerTask servlet acting up  RSS feed

 
Zsolt Nagykaldi
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi guys,

I wrote a simple Tomcat TimerTask servlet and referenced it in web.xml:



EmailScheduleServlet basically uses a TimerTask to run a database scan every 24 hours to see if e-mails are scheduled to be sent (handled by an EmailScheduleTask class). The servlet runs fine upon starting Tomcat and it sends out e-mails as it should, but it gives me a weird output in my system logs. Here is the servlet code:



and here is a snippet from catalina.out:


Connection attempt successful in EmailScheduleTask
EmailScheduleTask:---- No scheduled medical encounters today!
Connection attempt successful in EmailScheduleTask
EmailScheduleTask:---- No scheduled medical encounters today!
Connection attempt successful in EmailScheduleTask
EmailScheduleTask:---- No scheduled medical encounters today!
Connection attempt successful in EmailScheduleTask
EmailScheduleTask:---- No scheduled medical encounters today!
Connection attempt successful in EmailScheduleTask
EmailScheduleTask:---- No scheduled medical encounters today!
Connection attempt successful in EmailScheduleTask
EmailScheduleTask:---- No scheduled medical encounters today!

Now the 2 system message lines suppose to come up only ONCE per day, when the TimerTask runs, but after a while it seems that the task will run a bunch of times, then it stops. Is also started to run out of sync (not just every 24 hours). It seems to me that multiple TimerTask instances may be running, but why? I have a Destroy() properly set up which says it shuts down the servlet every time I stop Tomcat. Where should I start to correct this problem? Can it be that the servlet IS shut down, but the TimerTask keeps running? If so, how can I make sure that the TimerTask thread is stopped as well?

Thanks.

[ June 05, 2008: Message edited by: Zsolt Nagykaldi ]
[ June 05, 2008: Message edited by: Zsolt Nagykaldi ]
 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 66207
151
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Please be sure to use UBB code tags when posting code to the forums. Unformatted code is extermely hard to read and many people that might be able to help you will just move along. Please read this for more information.

You can go back and change your post to add code tags by clicking the .
 
Zsolt Nagykaldi
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks, sorry, I added the tags.

This is what I did since: I made the t1 timer declaration reachable for the entire class and added a t1.cancel() statement at server Destroy(). This I hope, will block the timer from running when Tomcat is shut down or a new class is posted (similar effect). I suspect that the problem was there. I will let you know.
 
Zsolt Nagykaldi
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Nope, it did not help a bit. I restarted Tomcat to make sure all related processes are stopped, but a day later I got the same long list of repeats. Huh! This problem is up for grabs then again!

Thanks.
 
Ulf Dittmer
Rancher
Posts: 42972
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The fact that you're passing "24 * 60 * 60000" to the EmailScheduleTask is suspicious - does it possibly start threads on its own?

You could add a logging statement to the EmailScheduleTask constructor to check how often it's called, and from where (print a stack trace).
 
Ben Souther
Sheriff
Posts: 13411
Firefox Browser Redhat VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
A couple suggestions:

1.) If you're running a container that supports servlet spec 2.3 or higher replace this servlet with a context listener. Servlets can be taken out of service and put back into service by the container at will.

Context events are fired when the app starts up and when it closes down.
Before and after any of the servlet events, respectively. This could, potentially give your timer a more stable environment.


2.) See the API for java.util.Timer

By default, the task execution thread does not run as a daemon thread, so it is capable of keeping an application from terminating. If a caller wants to terminate a timer's task execution thread rapidly, the caller should invoke the timer's cancel method.


I don't see a call to cancel in your destroy method.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!