Reputation: 5802
BackGround :
I am using Quartz scheduler with Spring to schedule a cronjob.
Question:
I am configuring the scheduler options in my applicationconfig file. Rather, I want to specify these options programatically in my java class. Any ideas on how to achieve this? My Code is as below,
ApplicationConfig
<!-- Cron Trigger -->
<bean id="SimpleTrigger" class="org.springframework.scheduling.quartz.CronTriggerBean">
<property name="jobDetail" ref="TaskJobDetail" />
<property name="cronExpression" value="0 19 14 * * ?" />
</bean>
<bean class="org.springframework.scheduling.quartz.SchedulerFactoryBean">
<property name="jobDetails">
<list>
<ref bean="TaskJobDetail" />
</list>
</property>
<property name="triggers">
<list>
<ref bean="SimpleTrigger" />
</list>
</property>
</bean>
I want to set the cronExpression programatically in my java class. Any ideas please?
Upvotes: 1
Views: 3692
Reputation: 340733
Of course, you first have to inject the Quartz scheduler to your Java class (Spring bean):
@Autowired
private Scheduler scheduler;
Then simply use Quartz API:
import static org.quartz.TriggerBuilder.*;
import static org.quartz.CronScheduleBuilder.*;
import static org.quartz.DateBuilder.*:
JobDetail job = newJob(SimpleJob.class).build();
CronTrigger trigger = newTrigger()
.withSchedule(cronSchedule("0 19 14 * * ?"))
.build();
schedulder.scheduleJob(job, trigger);
Upvotes: 6