Spring 3.0.7 CronTrigger executes task multiple times when triggered - spring

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 * * * *)

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 * * * *

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?

Related

Problem with cron job which has to run every 15 minutes but not at 15,30,45

So i have a cron job that runs and executes fine (hence why no path is shown) at:
*/15 * * * *
but I don't want that as it means it runs at every fifteen minutes past 0 of the hour according to crontab.guru which gives tasks run at 15,30 and 45 minutes.
This is actually what I want:
1/15 * * * *
which according to crontab.guru should begin the 15 minutes at 1 minute past the hour and run the tasks at 16, 31 and 46 minutes.
Now the problem is when I crontab -e to set up the cron job of 1/15 * * * * I get a bad minute error from crontab, yet as i say in my opening lines using */15 * * * * returns no error from crontab and the task is executed. Why is this?.
You could do 1,16,31,46 * * * * to run the jobs at 1, 16, 31 and 46 minutes every hour.

How to set two cron on the same springboot method?

I want to set a timer at 9:00 / 16:30. But one cron can not realize it.
So i need two cron to realize it. The corn is as follows:
cron1: * 30 16 * * *
cron2: * * 9 * * *
I want to set cron by springboot #Schedule like this
#Scheduled(cron = "0 0 2 * * ?")
public void sayHello(){}
But it seems that can not support two #Scheduled label on one method or two cron in one label.
The so low method is as follow, Any better advice?
#Scheduled(cron = "* 30 16 * * ?")
public void sayHello(){}
#Scheduled(cron = "* * 9 * * ?")
public void subSayHello(){sayHello();}
I think it is difficult because of the different min and different hour,
if the cron run in the same hour or min, it will be easy.
Such as:
"* 0,30 9 * * ?"
"* 0 9,16 * * ?"
Or you can see this link Cron expression to run job twice a day at different time?

Cron expression running every 30 mins

I have one spring job to run every 30 minutes:-
Suppose current time is 1:55 pm, now it should run at 2:25 pm, 2:55pm, 3:25 pm & so on.
This cron expression is not working for me:-
"0 0/30 * * * ?"
This timespec would run your job on the 25th and 55th minute of every hour, every day:
25,55 * * * *
#Scheduled(cron="0 0/30 * * * ?") will trigger first when it reaches any *:30 or *:00. So in your case it will trigger at 2:00pm, 2:30pm, 3:00pm and so on.
If you want your job to run every 30minutes. You can use fixedRate or fixedDelay.
#Scheduled(fixedRate = 1800000) will first trigger your job at 1:55pm. If you want to skip first iteration you can add initialDelay=1800000.
25,55 * * * * your command >/dev/null 2>&1
You can easly generate a crontab via this website, even with no experience.
The problem is that you have to know the starting time of your cron job.
http://crontab-generator.org/

cron job for every half hour

i have to run a shell script as cron job
in VERY HALF HOUR but ONLY IN BETWEEN (morning 10.30 A.M to evening 6.30 A.M)
DAILY
means this cron job will be execute every half only within a scpecfic givine time
This will do:
30 10 * * * <your cronjob>
0,30 11-18 * * * <your cronjob>
Note you can also use the following for every 30 min:
*/30 11-18 * * * <your cronjob>

How to set scheduler task in spring to run every 1 minutes

now I have this configuration my my scheduler:
<task:scheduled ref="task" method="run" cron="0 45 22 * * *" />
when this task is executed ? and how I can change it to do this task every minute
This task is executed at 22:45:00 every day, every week, every month.
To execute a task every minute, use
0 * * * * *
The accepted answer is correct for spring. Other than that, one should be careful whether the target system uses 6 or 5-digits cron.
With 5-digits-crons
0 * * * * schedules to be run "at minute 0" (hence every hour).
The correct answer in this case should be either
* * * * *
or
*/1 * * * *
Also see: Spring cron vs normal cron?

Resources