How can I set Quartz cron trigger for run at one specifc date and time for once?
Eg: run something at 12.30pm on 2017-06-30 and never run again
If you want to achieve using CronTrigger try like below
<bean id="newTrigger" class="org.springframework.scheduling.quartz.CronTriggerFactoryBean">
<property name="jobDetail" ref="oneTimeJob"/>
<property name="cronExpression" value="0 30 12 30 6 ? 2017"/>
</bean>
Or As #scary wombat mention Use SimpleTrigger
SimpleTrigger trigger = (SimpleTrigger) newTrigger()
.withIdentity("trigger1", "group1")
.startAt(myStartTime) // some Date date 30.06.2017 12:30
.forJob("job1", "group1") // identify job with name, group strings
.build();
Related
I defined a job with Quartz that calls a web service. I have a trigger with Cron expression which run every 50 minutes as 0 0/50 * ? * * * .
I have a requirement that execute the job in startup application and after that every 50 minutes.
the job factory is:
Trigger trigger = newTrigger().withIdentity(name, "our.trigger")
.withSchedule(CronScheduleBuilder.cronSchedule("0
0/50 * ? * * *")).startNow().build();
JobDetail jobDetail = newJob(jobClass).withIdentity(name, "our.job").build();
Set<Trigger> triggers = new HashSet<>();
triggers.add(trigger);
stdScheduler.scheduleJob(jobDetail, triggers, true);
stdScheduler.start();
How do I solve this issue?
I solved the problem with the following code:
Trigger startupTrigger = newTrigger().withIdentity(name+".startup", "trigger")
.withSchedule(SimpleScheduleBuilder.repeatSecondlyForTotalCount(1)).startNow().build();
Thanks for #biiyamn
I have a method that I want to be invoked periodically: every day on 11 am. It is a simple method in Main:
public void loadProduct() {
PropertyConfigurator.configure("log4j.properties");
try {
service.create(product);
logger.info("Creation started");
} catch (Exception e) {
// Log Exception
logger.error(e);
}
}
I have almost figured out how to achieve this with the help of Spring context:
<task:scheduler id="scheduler" pool-size="1"/>
<task:scheduled-tasks scheduler="scheduler">
<task:scheduled ref="productTask" method="loadProduct" cron="0/30 * * * * *"/>
</task:scheduled-tasks>
But how to schedule the task to start every 24 hours on 11 am every day?
Or is there a way to achieve this in Java code?
But how to schedule the task to start every 24 hours on 11 am every day?
This can be achieved by using the cron expression: 0 0 11 * * *.
Or is there a way to achieve this in Java code?
Yes, by using the Scheduled (Spring Framework 5.0.1.RELEASE API) annotation, for example:
#Scheduled(cron = "0 0 11 * * *", zone = "Europe/Moscow")
public void run() {
// ...
}
Additional references:
Integration: 7. Task Execution and Scheduling: 7.4. Annotation Support for Scheduling and Asynchronous Execution, Spring Framework Documentation.
Getting Started ยท Scheduling Tasks.
I run Tomcat with -Duser.timezone=UTC. However Quartz scheduler 2.2.1 seems to run in Europe/Prague which is my OS timezone.
Is there a way to run Quartz in custom timezone or determine which timezone Quartz is using?
If not, is there a way to determine OS timezone programatically?
Quartz by default will use the default system locale and timezone, and it is not programed to pick up the property user.timezone you are providing your app. Remember also that this is only applies to a CronTrigger and not a SimpleTrigger.
If you are using Spring for example:
<bean id="timeZone" class="java.util.TimeZone" factory-method="getTimeZone">
<constructor-arg value="GMT" />
</bean>
<bean id="yourTrigger" class="org.springframework.scheduling.quartz.CronTriggerBean">
<property name="jobDetail" ref="yourJob" />
<property name="cronExpression" value="0 0 0/1 * * ?" />
<property name="timeZone" ref="timeZone" />
</bean>
If you are using plain java:
Trigger yourTrigger = TriggerBuilder
.newTrigger()
.withIdentity("TRIGGER-ID", "TRIGGER-GROUP")
.withSchedule(CronScheduleBuilder
.cronSchedule("0 0 0/1 * * ?")
.inTimeZone(TimeZone.getTimeZone("GMT")))
).build();
If you are using the XML configuration file, e.g. the quartz-config.xml from Example To Run Multiple Jobs In Quartz of mkyong, you can configure the timezone in the element time-zone:
<schedule>
<job>
<name>JobA</name>
<group>GroupDummy</group>
<description>This is Job A</description>
<job-class>com.mkyong.quartz.JobA</job-class>
</job>
<trigger>
<cron>
<name>dummyTriggerNameA</name>
<job-name>JobA</job-name>
<job-group>GroupDummy</job-group>
<!-- It will run every 5 seconds -->
<cron-expression>0/5 * * * * ?</cron-expression>
<time-zone>UTC</time-zone>
</cron>
</trigger>
</schedule>
See also Java's java.util.TimeZone for to see the ID for several timezones.
You can call setTimeZone() to set the time zone of your choosing for anything in Quartz that inherits BaseCalendar.
Java's TimeZone class has a getDefault() which should aid in determining OS timezone programmatically.
I have set a trigger using cronScheduler with misfireInstruction like follows
trigger = newTrigger().withIdentity("autoLockTrigger", "autoLockGroup").startNow() .withSchedule(cronSchedule(croneExpression).withMisfireHandlingInstructionFireAndProceed())
.forJob("autoLockJob","autoLockGroup")
.build();
my quartz.properties is like follows
org.quartz.scheduler.instanceName =MyScheduler
# Configuring ThreadPool
org.quartz.threadPool.class = org.quartz.simpl.SimpleThreadPool
org.quartz.threadPool.threadCount = 1
org.quartz.threadPool.threadPriority = 9
org.quartz.jobStore.class = org.quartz.impl.jdbcjobstore.JobStoreTX
org.quartz.jobStore.driverDelegateClass = org.quartz.impl.jdbcjobstore.StdJDBCDelegate
org.quartz.jobStore.dataSource = myDS
org.quartz.jobStore.tablePrefix = QRTZ_
#org.quartz.dataSource.myDS.jndiURL = jdbc/vikas
org.quartz.dataSource.myDS.driver = com.mysql.jdbc.Driver
org.quartz.dataSource.myDS.URL = jdbc:mysql://staging:3307/facao
org.quartz.dataSource.myDS.user = root
org.quartz.dataSource.myDS.password = toor
org.quartz.dataSource.myDS.maxConnections = 30
#org.quartz.jobStore.nonManagedTXDataSource = myDS
#to store data in string format (name-value pair)
org.quartz.jobStore.useProperties=true
org.quartz.jobStore.misfireThreshold = 60000
In my code if I set some trigger at particular time and if server is in running state then scheduler runs properly but if server is down for the time in which scheduler is suppose to be run and then started after some time then scheduler should run the misfired instruction. But in my case the misfired instruction is not running all the time it runs some time not always so my purpose is not fulfilled. Please give some solution. Thank you in advance.
I am not sure about the cron triggers but for simple triggers yeah,
if the end time of the trigger has been passed then some of the provided misfire instruction
will not work. See the javadoc snippet for more info.
I guess the same would be the case with cron trigger too.
So, it totally depends on what cron expression you use.
I have a simple quartz trigger running in Spring 2.5.6-SEC01.
Trigger definition looks like this:
<bean id="AdvicesCronTrigger" class="org.springframework.scheduling.quartz.CronTriggerBean">
<property name="jobDetail" ref="AdvicesQuartzJob"/>
<property name="cronExpression" value="0 20/15 * * * ?"/>
</bean>
This is my scheduler factory:
<bean class="org.springframework.scheduling.quartz.SchedulerFactoryBean">
<property name="triggers">
<list>
<ref bean="AdvicesCronTrigger"/>
</list>
</property>
</bean>
I've read this documentation about firing CRON triggers from Quartz. This is an excerpt:
CronTrigger Example 1 - an expression
to create a trigger that simply fires
every 5 minutes
"0 0/5 * * * ?"
Today I fired my program at 9:40. This is my execution output:
Edit: Bobby is right in his appreciation. I've updatted my execution log:
2010-02-11 09:50:00,000 INFO - START
2010-02-11 10:20:00,000 INFO - START
2010-02-11 10:35:00,000 INFO - START
2010-02-11 10:50:00,000 INFO - START
2010-02-11 11:20:00,000 INFO - START
2010-02-11 11:35:00,000 INFO - START
I expected that this trigger will be fired at
9:50
10:05
10:20
10:35
...
How to accomplish this? Which CRON expression use?
The 20/15 part of the cron expression means every 15 minutes after the 20'th minute of the hour. This means that it will always start at the 20'th minute.
I have never tested it but maybe an expression like this one would be what you are searching for :
0 */15 * * * ?
Not to give you a non-related answer, but sometimes it makes sense to use some services instead of trying to do it yourself :) Take a look at http://www.cronservice.co.uk/new/, http://scheduler.codeeffects.com, or http://www.webbasedcron.com/