How to run every 25 seconds in Quartz scheduler?
Asked Answered
W

7

17

I am using the Quartz Scheduling API for Java. Could you help me to run every 25 seconds using cron-expression. It's just a delay. It does not have to start always at second 0. For example, the sequence is like this: 0:00, 0:25, 0:50, 1:15, 1:40, 2:05, etc until minute 5 when the sequence begins again at second 0. Thank you.

Wanettawanfried answered 6/6, 2011 at 23:14 Comment(0)
K
17

I don't think cron expression will allow you to do that, but you can use

SimpleScheduleBuilder.repeatSecondlyForever( 25 )

as 300 (5 minutes) is a multiple of 25 it will repeat automatically.

Kinematograph answered 7/6, 2011 at 18:56 Comment(0)
P
10

If you want a job to trigger at a regular interval then you can use a Quartz SimpleTrigger with a repeatInterval specified.

Phthisic answered 18/3, 2013 at 14:16 Comment(1)
This is the correct answer to this question, chron expressions are not built for this particular task.Oidea
D
9

With Quartz 2.1.5 this will help:

CronTrigger trigger = newTrigger()
    .withIdentity("trigger1", "group1")
    .withSchedule(cronSchedule("0/20 * * * * ?"))
    .build();
Demisec answered 5/2, 2016 at 14:9 Comment(1)
This is every 20 seconds, which works because 60 is a multiple of 20. OP explicitly asked for every 25 seconds, which this will not work for.Yate
Y
7

The only way to do this with a cron trigger is so complicated as to be useless; you're much better off with the SimpleTrigger from other answers. Nevertheless, if it has to be cron, you need to set up five different cron triggers:

 0/25 0/5 * * * *
15/25 1/5 * * * *
 5/25 2/5 * * * *
20/25 3/5 * * * *
10/25 4/5 * * * *

The first trigger fires at 0:00:25, 0:00:50; then the second trigger fires at 0:01:15 and 0:01:40; the third at 0:02:05, 0:02:30, 0:02:55; the fourth at 0:03:20, 0:03:45; and finally the fifth at 0:04:10 and 0:04:35. The first trigger then takes over again at 0:05:00, etc.

This only works because 25 seconds divides evenly into 5 minutes (which in turn goes evenly into an hour). If you wanted it every 23 seconds? Forget about it!

Yate answered 26/2, 2016 at 20:50 Comment(0)
L
2
*/25 * * * * *


- [wiki] http://en.wikipedia.org/wiki/Cron
- [quartz tutorial] http://www.quartz-scheduler.org/docs/tutorial/TutorialLesson06.html

Le answered 6/6, 2011 at 23:29 Comment(2)
Logically this seems like like it should work, but it does not with the quartz version I have (2.0.1). It restarts from 0 seconds every minute just like 0/25.Kinematograph
This is not every 25 secs, but the sequence of the seconds goes like: 0 - 25 - 50 - 0Varix
P
0

You can't have a schedule like that for quartz.

One thing you could do is schedule a wrapper job to run every 5 seconds, and only do any work every fifth execution.

Paez answered 7/6, 2011 at 0:6 Comment(0)
E
0

You could schedule the job to run constantly but throttle the frequency using Camel's Throttler.

<route>
  <from uri="jms:queue:TestQueue"/>
  <!-- throttle 1 messages per 25 sec -->
  <throttle timePeriodMillis="25000">
    <constant>1</constant>
    <to uri="bean:TestBean?method=testMethod"/>
  </throttle>
</route>
Enki answered 1/12, 2016 at 18:0 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.