Question

I'm trying to get a Java (1.7) repeating TimerTask to work e.g.

TokenJanitor janitor = new TokenJanitor( TokenManager.getInstance() );
try {
    new Timer().schedule(janitor, System.currentTimeMillis()+15000, 15000);
    LOG.info("TokenJanitor is running...");
    ...

And then the TokenJanitor is a TimerTask e.g.

public class TokenJanitor extends TimerTask {

    @Override
    public void run() {
        LOG.info("Running TokenJanitor cleanup now...");
        System.out.println("Hello..."); // just for good measure

It doesn't even run once. I see the log message after the schedule function is called, but I can't see any log messages or print output from the TimerTask. I'm using log4j but I'm using it exactly as I do in every other class and it works fine. For what it's worth, I'm using Jetty to run a web server, so the schedule function above is called in the main method of a class that launches the server.

Was it helpful?

Solution

Look at the method Timer#schedule(TimerTask task,long delay,long period) that you have used of Timer class.

task - task to be scheduled.
delay - delay in milliseconds before task is to be executed.
period - time in milliseconds between successive task executions. 

you have specified System.currentTimeMillis()+15000 as delay that is very long time to run the task.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top