Question

Okay I've looked through everything I can find and haven't found an answer to my issue. A couple items were close but not close enough. I have a Spring 3.0.7 based web app running in Tomcat 7. In the applicationContext.xml I have:

<task:scheduler id="scheduledReportsScheduler" pool-size="1"/> 
<task:scheduled-tasks scheduler="scheduledReportsScheduler"> 
        <task:scheduled ref="scheduledReportsQueuer" method="process" cron="0 */1 * * * *"/> 
</task:scheduled-tasks>
<bean id="scheduledReportsQueuer" class="com.foo.scheduledServices.ScheduledReportsQueuerService"></bean>

This executes my ScheduledReportsQueuerService class once every minute. That works fine. Then inside of that class I check some database tables and try to schedule tasks based on a CronTrigger if they haven't already been scheduled:

if(!_workers.containsKey(schedule.getRptScheduleId())){
    _logger.debug(Thread.currentThread().getName() + " Creating and scheduling ScheduledReportQueuerWorker for scheduled report ID: "+schedule.getRptScheduleId()+"("+schedule.getReportName()+") "+schedule.getCronPattern());
    ScheduledReportQueuerWorker newWorker = new ScheduledReportQueuerWorker(schedule);
    newWorker.setRptRequestDAO(getRptRequestDao());
    newWorker.setRptScheduleDAO(getRptScheduleDao());
    _workers.put(schedule.getRptScheduleId(), newWorker);
    ScheduledFuture<?> newFuture = _scheduler.schedule(newWorker, new CronTrigger(schedule.getCronPattern()));
    _futures.put(schedule.getRptScheduleId(), newFuture);
}

private ConcurrentTaskScheduler _scheduler;

This also seems to work as the new tasks (newWorker above) are executed. The issue is they are getting executed multiple times at the correct trigger time. For instance if a new task has a cron pattern of '* */2 * * * *' it should execute once every 2 minutes. Instead what is happening is the task is getting executed 50-60 times in a row every 2 minutes.

This is driving me nuts any help you can give would be great.

More info Here is some log output, you can see the XML based thread get started and it tells us we are creating a thread for one report to run every two minutes. Then you see that ever two minutes that report runs 60 times in a row.

// here it schedules a single report to be run every 2 minutes
2012-11-19 08:30:02,876 DEBUG - ScheduledReportsQueuerService - scheduledReportsScheduler-1 Creating and scheduling ScheduledReportQueuerWorker for scheduled report ID: 20182(Jasper Test Report 1) * */2 * * * *

// here the worker thread gets kicked off 57 times
2012-11-19 08:30:03,016 INFO  - workers.ScheduledReportQueuerWorker - !!!!!!!!!!!!! 20182 (* */2 * * * *)
...
2012-11-19 08:30:59,012 INFO  - workers.ScheduledReportQueuerWorker - !!!!!!!!!!!!! 20182 (* */2 * * * *)

// two minutes later the worker thread gets kicked off 60 times
2012-11-19 08:32:00,017 INFO  - workers.ScheduledReportQueuerWorker - !!!!!!!!!!!!! 20182 (* */2 * * * *)
...
2012-11-19 08:32:59,003 INFO  - workers.ScheduledReportQueuerWorker - !!!!!!!!!!!!! 20182 (* */2 * * * *)

// two minutes later the worker thread gets kicked off 60 times
2012-11-19 08:34:00,019 INFO  - workers.ScheduledReportQueuerWorker - !!!!!!!!!!!!! 20182 (* */2 * * * *)
...
2012-11-19 08:34:59,014 INFO  - workers.ScheduledReportQueuerWorker - !!!!!!!!!!!!! 20182 (* */2 * * * *)

// two minutes later the worker thread gets kicked off 60 times
2012-11-19 08:36:00,010 INFO  - workers.ScheduledReportQueuerWorker - !!!!!!!!!!!!! 20182 (* */2 * * * *)
...
2012-11-19 08:36:59,006 INFO  - workers.ScheduledReportQueuerWorker - !!!!!!!!!!!!! 20182 (* */2 * * * *)
Was it helpful?

Solution

Got it. Now I feel stupid. Turns out I wasn't understanding the cron pattern. I used

* */2 * * * * 

thinking that should run once every two minutes. what that really does is once every two minutes run it for every second that has passed since the last trigger. The correct pattern is:

0 */2 * * * *

OTHER TIPS

It looks like you are scheduling more tasks as part of your scheduled task?

The original cron trigger in your xml config will run indefinetly... do you really want to be scheduling more tasks?

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