Configuration for Quartz scheduling

In order to periodically submit an Image Resize Request, the Image Resize Request Producer must be able to run at any given interval. For this purpose, CRON expressions are used, which can be specified in a variety of ways (for example, run every Monday at 12.00 a.m., run every day at 2.00 p.m, and so on). The following CRON expression to run every minute is configured as a property in the application.properties file:

images.cron=0 * * * * ?

In the preceding CRON expression 0 represents seconds, first * represents minutes, second * represents hours, third * represents day of month, fourth * represents month, fifth * represents day of week and ? represents the year. CRON expressions can be very simple of very complex so use Oracle Documentation of CRON to learn more. The following configuration is used in the SpringBootAsyncProducerApplication class to enable Quartz scheduling:

@Bean MethodInvokingJobDetailFactoryBean methodInvokingJobDetailFactoryBean(ScheduledImageResizeRequestSubmitter scheduledImageResizeRequestSubmitter) {
MethodInvokingJobDetailFactoryBean methodInvokingJobDetailFactoryBean = new MethodInvokingJobDetailFactoryBean();
methodInvokingJobDetailFactoryBean.setTargetObject(scheduledImageResizeRequestSubmitter);
methodInvokingJobDetailFactoryBean.setTargetMethod("scheduleTaskWithCronExpression");

return methodInvokingJobDetailFactoryBean;
}

@Bean
public CronTriggerFactoryBean trigger(JobDetail job, @Value("${images.cron}") String imagesCron) {
CronTriggerFactoryBean cronTriggerFactoryBean = new CronTriggerFactoryBean();
cronTriggerFactoryBean.setCronExpression(imagesCron);
cronTriggerFactoryBean.setJobDetail(job);
return cronTriggerFactoryBean;
}

The methodInvokingJobDetailFactoryBean bean defines JobDetail, which uses ScheduledImageResizeRequestSubmitter, which are defined earlier, to specify targetObject as well as the targetMethod with the scheduleTaskWithCronExpression method, which needs to run when the CRON expression triggers. 

The trigger bean defines CronTrigger using CronTriggerFactoryBean, which takes in the property images.cron from application.properties to configure it. The JobDetail created inside methodInvokingJobDetailFactoryBean will be invoked when this CRON trigger is fired.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.147.75.203