You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@tomcat.apache.org by Jerry Malcolm <2n...@gmail.com> on 2012/02/28 03:05:41 UTC

Servlet Timer error

I have several tasks that needs to run periodically as part of an overall
web application solution... some every few minutes and some once a day.  I
know it's technically not a requirement that it run in a web container.
But it needs all off the resources and database connections, etc just like
the rest of the web app.  I currently have a servlet that starts a timer in
the servlet init:

aTimer = new Timer( true );
aTimer.scheduleAtFixedRate( new MyTimerTask(), getFirstTime(), getPeriod()
);

The timer task has a run method that (as far as I know) runs and exits.

This has all worked fine pre TC7.  But now I'm getting this error message:

A web application appears to have started a TimerThread named [Timer-114]
via the java.util.Timer API but has failed to stop it. To prevent a memory
leak, the timer (and hence the associated thread) has been forcibly
cancelled.

So.... is what I'm doing forbidden in official web container architecture?
(And now TC 7 has started enforcing the rules?)  Or is what I'm doing ok
and just somehow my timer task is getting caught in a loop a few times and
TC is just stopping it for that reason?

What are my options?  Is there a 'right' way to have a servlet run a task
periodically in a web app? (sample code anywhere?) I know I can set up a
standalone command line java app to do this one little task.  But then I'd
be stuck with a separate distribution file requiring setup, configuration,
and management outside of a WAR file for what in actuality is one
self-contained application.  I will forego J2EE purity at this point if
that's what's required and even risk the memory leak.  I just need to get
this back running again. If it's just a case of TC 7 now enforcing a rule,
is there any way to tell TC 7 to NOT do me these favors of stopping all
timer threads?

Please advise....

Thanks.

Jerry

Re: Servlet Timer error

Posted by Felix Schumacher <fe...@internetallee.de>.

Jerry Malcolm <2n...@gmail.com> schrieb:

>I have several tasks that needs to run periodically as part of an
>overall
>web application solution... some every few minutes and some once a day.
> I
>know it's technically not a requirement that it run in a web container.
>But it needs all off the resources and database connections, etc just
>like
>the rest of the web app.  I currently have a servlet that starts a
>timer in
>the servlet init:
>
>aTimer = new Timer( true );
>aTimer.scheduleAtFixedRate( new MyTimerTask(), getFirstTime(),
>getPeriod()
>);
>
>The timer task has a run method that (as far as I know) runs and exits.
>
>This has all worked fine pre TC7.  But now I'm getting this error
>message:
>
>A web application appears to have started a TimerThread named
>[Timer-114]
>via the java.util.Timer API but has failed to stop it. To prevent a
>memory
>leak, the timer (and hence the associated thread) has been forcibly
>cancelled.
>
>So.... is what I'm doing forbidden in official web container
>architecture?
>(And now TC 7 has started enforcing the rules?)  Or is what I'm doing
>ok
>and just somehow my timer task is getting caught in a loop a few times
>and
>TC is just stopping it for that reason?

 You are not stopping your thread when the webapp gets stopped.

 So, when you start the thread in the init method, just stop it in the destroy method. 

>
>What are my options?  Is there a 'right' way to have a servlet run a
>task
>periodically in a web app? (sample code anywhere?) I know I can set up

You could use a ServletContextListener instead of tying it to one special servlet.

If I remember correctly TaskTimer has problems with uncaught Exeptions. Maybe have a look at ScheduledExecutor. You will have to stop it yourself of course :)

If you implement yourself something like a cron framework, have a look at quartz. 

Regards
Felix
>a
>standalone command line java app to do this one little task.  But then
>I'd
>be stuck with a separate distribution file requiring setup,
>configuration,
>and management outside of a WAR file for what in actuality is one
>self-contained application.  I will forego J2EE purity at this point if
>that's what's required and even risk the memory leak.  I just need to
>get
>this back running again. If it's just a case of TC 7 now enforcing a
>rule,
>is there any way to tell TC 7 to NOT do me these favors of stopping all
>timer threads?
>
>Please advise....
>
>Thanks.
>
>Jerry




---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@tomcat.apache.org
For additional commands, e-mail: users-help@tomcat.apache.org