Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7431593
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T09:19:11+00:00 2026-05-29T09:19:11+00:00

I have a WAR file deployed to Tomcat server, one of the class will

  • 0

I have a WAR file deployed to Tomcat server, one of the class will get called at start up time, then the init() method will schedule a timer to fire every 5 hours to perform some tasks.

My init() code looks like this:

public void init()
{
    TimerTask parserTimerTask = new TimerTask() {

        @Override
        public void run() {
            XmlParser.parsePage();
        }
    };

    Timer parserTimer = new Timer();
    parserTimer.scheduleAtFixedRate(parserTimerTask, 0, PERIOD);
}

My application runs without problem, but when I shutdown the Tomcat using /etc/init.d/tomcat7 stop, then I check the log (catalina.out) it has a entry like this:

SEVERE: The web application [/MyApplication] appears to have started a thread named [Timer-0] but has failed to stop it. This is very likely to create a memory leak.

I understand this is caused by me schedule the timer, but my question is:

  1. I didn’t set setDeamon to true, so shouldn’t the timer prevent Tomcat from shutting down, rather than left running?
  2. Can I, in my application, detect Tomcat is going to be shutdown and cancel my timer?
  3. What are the other solutions I can use to take care of this issue?

Thanks!

UPDATE

I changed my code to the following based on some search and DaveHowes’s answer.

Timer parserTimer;
TimerTask parserTimerTask;

public void init()
{
    parserTimerTask = new TimerTask() {

        @Override
        public void run() {
            XmlParser.parsePage();
        }
    };

    parserTimer = new Timer();
    parserTimer.scheduleAtFixedRate(parserTimerTask, 0, PERIOD);
}

@Override
public void contextDestroyed(ServletContextEvent arg0) {
    Logger logger = Logger.getRootLogger();
    logger.info("DETECT TOMCAT SERVER IS GOING TO SHUT DOWN");
    logger.info("CANCEL TIMER TASK AND TIMER");

    otsParserTimerTask.cancel();

    otsParserTimer.cancel();

    logger.info("CANCELING COMPLETE");
}

@Override
public void contextInitialized(ServletContextEvent arg0) {

}

Now my new question:

  1. I cancel TimerTask first then Timer, is this correct?
  2. Are there other thing I should do?

Thanks!

UPDATE

It doesn’t work. I put some logging statement in the contextDestroyed() method, after I shutdown Tomcat, the log file only has the following:

PowderGodAppWebService -> [07 Feb 2012 04:09:46 PM] INFO (PowderGodAppWebService.java:45):: DETECT TOMCAT SERVER IS GOING TO SHUT DOWN
PowderGodAppWebService -> [07 Feb 2012 04:09:46 PM] INFO (PowderGodAppWebService.java:46):: CANCEL TIMER TASK AND TIMER

CANCELING COMPLETE is not there.

I also checked processes that are running (I’m not a Linux expert so I just use Mac’s Activity Monitor.

  • Make sure no java process is running
  • Start Tomcat, note the PID of that java process
  • Stop Tomcat
  • Found the Tomcat process is gone
  • Start Tomcat, note the PID of that java process
  • Deploy my war file
  • Sample the process, see [Timer-0] thread is there
  • Shutdown Tomcat
  • Found that the process is still there
  • Sample the process
  • See [Timer-0] is still there

FIXED

I changed my code to parserTimer = new Timer(true); so that my timer runs as a daemon thread because the contextDestroyed() gets called after Tomcat actually shuts down.

“All servlets and filters will have been destroyed before any ServletContextListeners are notified of context destruction.”

http://docs.oracle.com/javaee/6/api/javax/servlet/ServletContextListener.html

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-29T09:19:11+00:00Added an answer on May 29, 2026 at 9:19 am

    Do not use Timer in an Java EE environment! If the task throws a runtime exception, then the whole Timer is killed and won’t run anymore. You basically needs to restart the whole server to get it to run again. Also, it is sensitive to changes in the system clock.

    Use ScheduledExecutorService instead. It’s not sensitive to exceptions thrown in tasks nor to changes in system clock. You can shutdown it by its shutdownNow() method.

    Here’s an example of how the entire ServletContextListener implementation can look like (note: no registration in web.xml required thanks to the new @WebListener annotation):

    @WebListener
    public class BackgroundJobManager implements ServletContextListener {
    
        private ScheduledExecutorService scheduler;
    
        @Override
        public void contextInitialized(ServletContextEvent event) {
            scheduler = Executors.newSingleThreadScheduledExecutor();
            scheduler.scheduleAtFixedRate(new YourParsingJob(), 0, 5, TimeUnit.HOUR);
        }
    
        @Override
        public void contextDestroyed(ServletContextEvent event) {
            scheduler.shutdownNow();
        }
    
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to build a war file to be deployed to a tomcat server
I have deployed a .war file inside Jetty Server. The server has been started,
I built a war file called myapp.war and deployed it on Tomcat . I've
I have a webapp that is deployed as a WAR file to a Tomcat
I have a WAR file that I have deployed to Tomcat 7.0.23 via the
I have compiled and created .WAR file of my java code. then i started
I have a jsp file in the root of .war file. and then I
I need to create some way to get a local WAR file deployed on
I have deployed an ejb in a war file(not an ear file). I have
I have a problem with egovframework of Korea when I have deployed file war

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.