📜 ⬆️ ⬇️

Work with the scheduler in Java (Spring)

Recently, in the process of work, I was faced with the task of managing shedulers in a running application. We have a server application, and in the Spring configuration files we specified which tasks to run on a timer. However, the next task appeared: remove these tasks from the list of tasks or change the cron-timer without slowing down the application.
In the process of googling and reading, I found how to do it. Everything turned out to be much simpler than I thought. But in order to understand this I had to read a little.
Probably, this article will be useful for beginners, but, perhaps, mastodonts will learn something new for themselves.

Terms used:

Scheduler - manages task start timers

Job - a specific task run on a timer
')
Trigger - the condition of the task execution - sets the time frame for launching tasks and their execution

Accordingly, the sheduler contains descriptions of all tasks and triggers to them.

The system uses the scheduler, which is described in the system configuration file (shedulers.xml):
<beans:bean class="org.springframework.scheduling.quartz.SchedulerFactoryBean"> <beans:property name="jobDetails"> <beans:list> <beans:ref bean="reportCheckServiceJob" /> <beans:ref bean="vacationApprovalAutoProcessServiceJob" /> <beans:ref bean="plannedVacationServiceJob" /> <beans:ref bean="employeeLdapServiceJob" /> <beans:ref bean="oqProjectSyncServiceJob" /> </beans:list> </beans:property> <beans:property name="triggers"> <beans:list> <beans:ref bean="reportCheckServiceCronTrigger" /> <beans:ref bean="vacationApprovalAutoProcessServiceCronTrigger" /> <beans:ref bean="plannedVacationServiceCronTrigger" /> <beans:ref bean="employeeLdapServiceCronTrigger" /> <beans:ref bean="oqProjectSyncServiceCronTrigger" /> </beans:list> </beans:property> </beans:bean> 


An example of a job (and a service whose method it calls):
 <beans:bean id="plannedVacationService" class="com.aplana.timesheet.service.PlannedVacationService" /> <beans:bean id="plannedVacationServiceJob" class="org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean"> <beans:property name="targetObject" ref="plannedVacationService" /> <beans:property name="targetMethod" value="service" /> </beans:bean> 


Where targetObject specifies the class-service (bin), which contains the method that you want to run on a timer. In the targetMethod specify the name of the method.

Trigger example:
 <beans:bean id="plannedVacationServiceCronTrigger" class="org.springframework.scheduling.quartz.CronTriggerBean"> <beans:property name="jobDetail" ref="plannedVacationServiceJob" /> <beans:property name="cronExpression" value="${scheduler.plannedvacationcheck}" /> </beans:bean> 


In this trigger, we specify a job, which we will perform on the trigger (in this case, the plannedVacationServiceJob, also a bin), as well as in the cron format, we specify the mode and frequency of the task launch.

Now about how to manage the scheduler.

To control this scheduler, you need to connect this bin to the controller (service) (in our case, it is SchedulerFactoryBean).

This factory has a specific shear used, which can be obtained using the getScheduler () method. And already through it to manage jobs, triggers and the tasks themselves.

Consider the methods of sheduler for stopping and running Jobs:

pauseJob (String name, String Group) - stop the execution of the sheduler task in the specified job group. Stopping occurs by stopping the corresponding trigger (see pauseTrigger)

resumeJob (String name, String Group) - resume the execution of the sheduler task in the specified job group. Recovery occurs by running the appropriate trigger (see resumeTrigger)

pauseTrigger (String name, String Group) - stops the trigger in the corresponding group

resumeTrigger (String name, String Group) - resumes the trigger in the corresponding group

pauseAll - stops all scheduler tasks (pauseJobs (String group) - only for a specific group)

resumeAll - resumes the start of all tasks of the scheduler (see also resumeJobs)

And also, below in the example, the code is given as you can see all the tasks of the job group, and a list of all available groups in the scheduler. By default, if no job is specified for the jobs, they are assigned the DEFAULT group.

You can inject (@Autowired) directly triggers and manage their cron scheduler. In the example, this is the plannedVacationServiceCronTrigger, in which we change the CronExpression. Alternatively, you can access the trigger through the scheduler by calling the getTrigger method (String name, String group).

  @Autowired @Qualifier("plannedVacationServiceCronTrigger") private CronTriggerBean plannedVacationServiceCronTrigger; @Autowired private SchedulerFactoryBean schedulerFactoryBean; public someMethod(){ logger.info(plannedVacationServiceCronTrigger.getCronExpression()); plannedVacationServiceCronTrigger.setCronExpression("*/1 * * * * ?"); //schedulerFactoryBean.getScheduler().pauseJob("plannedVacationServiceJob", "DEFAULT"); schedulerFactoryBean.getScheduler().pauseTrigger("plannedVacationServiceCronTrigger", "DEFAULT"); for (String str : schedulerFactoryBean.getScheduler().getJobGroupNames()){ logger.info(str); } for (String str : schedulerFactoryBean.getScheduler().getJobNames("DEFAULT")){ logger.info(str); } logger.info(schedulerFactoryBean.getScheduler().getTrigger("plannedVacationServiceJob", "DEFAULT").getCronExpression()); } 



What to read:
www.seostella.com/ru/article/2012/02/12/ispolzovanie-annotacii-autowired-v-spring-3.html
static.springsource.org/spring/docs/3.2.x/spring-framework-reference/html/scheduling.html
quartz-scheduler.org/api/2.1.0/org/quartz/Scheduler.html
ru.wikipedia.org/wiki/Cron

Source: https://habr.com/ru/post/193140/


All Articles