New Scenarios: Many times, we often encounter the need to dynamically add or modify tasks, and spring provides the timing task component can only be modified in the XML trigger configuration to control the time of the scheduled task and the task is enabled or stopped, This gives us the convenience and also loses the flexibility of the dynamic configuration Task. All the configuration is done in xml, including the Cronexpression expression, which is very convenient. But what if my task information is stored in the database, I want to initialize it dynamically, and I don't have a lot of XML configuration when I have more tasks? Or I would like to modify the expression of trigger, so that the original 5 seconds to run the task into 10 seconds to run, then the problem comes, tried in the configuration file does not pass in the parameters such as cronexpression, but at the start of the error, I always change the XML file and then restart the application? , which is obviously inappropriate. ideally, you can add, delete, and modify configurations for dynamic tasks while integrating with Spring.
Scenario Requirements:
1, reduce the spring configuration file, in order to achieve a scheduled task, spring configuration code too Many.
2, users can add, enable, disable a task through the Page.
3. The user can modify a run time expression that is already running the task, Cronexpression.
4, for the convenience of maintenance, simplifying the operation of the task call processing, the task of running the portal is the job implementation of the class is the best only one, the job run class equivalent to the factory class, in the actual invocation of the task related information passed through the parameter, the factory class according to the task information to perform the required operations.
Analysis: The configuration file mode is the configuration in the job, trigger, etc. out of the schedule, the same database storage is removed to assemble the schedule
Steps:
Step one: Spring Configuration file
1 <?XML version= "1.0" encoding= "UTF-8"?>2 <Beansxmlns= "http://www.springframework.org/schema/beans"3 Xmlns:xsi= "http://www.w3.org/2001/XMLSchema-instance"4 xsi:schemalocation= "http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">5 6 <BeanID= "schedulerfactorybean"class= "org.springframework.scheduling.quartz.SchedulerFactoryBean"/>7 </Beans>
Step two: Create a task, Save the task in a database, start, delete, pause, resume, and run the process immediately
Note: tasks are handled not only by tasks but also by triggers, and some operations have restrictions such as deletion, to pause the trigger, remove the trigger, and remove the Task.
Step three: Create a timed task: two ways to create: implement the job interface or inherit the Quartzjobbean abstract class. The name is consistent with the JobName in the entity Bean.
Public class Quartzjob extends quartzjobbean{}
Public class Quartzjob implements Job {}
Attention:
1, Trigger each status description:
The None:trigger is completed and will not be executed, or the trigger cannot be found, or trigger has been removed normal: healthy PAUSED: paused state complete: trigger completed, but the task may still be executing BLOCKED: thread blocking status error: errors occurred
2, Update Task Direct update does not work, to first delete after the new creation;
3, Synchronous and Asynchronous: synchronous and asynchronous in the version of Quartz 2.2 for the consumer, the only difference is whether to add @disallowconcurrentexecution annotations on the job class, note that there is no way to change synchronous and asynchronous when the scheduled task runs
4, cluster, Distributed Architecture implementation: using the message mechanism, through the distribution and reception of information to achieve the management of scheduled tasks in the Cluster.
5, the implementation of a number of problems: Triger in the definition of a default parameter starttime, if not set the default setting is now
Crontrigger trigger = Newtrigger (). withidentity ("trigger1", "group1"). startnow (). withschedule (cronschedule ("0/2 * * * * * ?")). Build ();
Reference:
http://www.dexcoder.com/selfly/article/308
Http://www.dexcoder.com/selfly/article/1822?curPage=83#comment
http://blog.csdn.net/pengpegv5yaya/article/details/37595889
Spring+quartz integration Ii: scheduling management and separation of scheduled tasks