Spring 3 consolidates Quartz 2 for manual set-up of scheduled tasks: New, modify, delete, pause and resume----every moment you are dissatisfied, there is a past you never tried

Source: Internet
Author: User

absrtact : In the project management function, the management of the scheduled tasks is sometimes very common. However, the general Timing task configuration is done in XML, including the Cronexpression expression, which is very convenient. But if my task information is stored in the database, want to initialize dynamically, and there is a lot of tasks when there is not a large number of XML configuration? Or do I have to modify the trigger expression so that the original 5-second run task becomes 10 seconds to run, or do I have to control the " pause " of the scheduled task? Pause and then "Restart" the scheduled task at some point in time? or " Delete " The scheduled task directly? Do you want to change the trigger time for a certain scheduled task ? This is the problem, tried to not pass in the configuration file cronexpression parameters, but the start of the error, I always modify 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, without restarting the app.

First, let's review the configuration code for using quartz in spring:

<!--use Methodinvokingjobdetailfactorybean, the task class can not implement the job interface, Targetmethod specify the calling method--><bean id= "Taskjob" class= "Com.tyyd.dw.task.DataConversionTask"/><bean id= "Jobdetail" class= " Org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean "> <property name=" group "value=" Job_work "/> <property name=" name "value=" Job_work_name "/> <!--false indicates that a new task is opened after the last task has been executed--<prop Erty name= "Concurrent" value= "false"/> <property name= "TargetObject" > <ref bean= "taskjob"/> &L t;/property> <property name= "Targetmethod" > <value>execute</value> </property>< /bean><!--Dispatch Trigger--><bean id= "Mytrigger" class= " Org.springframework.scheduling.quartz.CronTriggerFactoryBean "> <property name=" name "value=" Work_default_ Name "/> <property name=" group "value=" Work_default "/> <property name=" Jobdetail "> <ref bean = "Jobdetail"/> </property> <property name= "Cronexpression" > &LT;VALUE&GT;0/5 * * * *?</value> </property>< /bean><!--Dispatch Factory--><bean id= "Scheduler" class= "            Org.springframework.scheduling.quartz.SchedulerFactoryBean "> <property name=" triggers "> <list> <ref bean= "Mytrigger"/> </list> </property></bean>

All the configuration is done in XML, including the Cronexpression expression, which is very convenient. But if you have a lot of scheduled tasks and need to change manually, you have to have a whole bunch of XML configurations that are not easy to manage.

So at design time I think of the following points

1, reduce the spring configuration file.

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, that is, 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.

As shown in the following example:

1. You can view the task details directly on the page.

2, can be added, modified

3, can be executed immediately, and view the execution details

In the above thinking down to carry out our development bar.

First, the database script used for initialization
SET foreign_key_checks=0;--------------------------------Table structure for task_detail-------------------------- ----DROP TABLE IF EXISTS ' task_detail '; CREATE TABLE ' task_detail ' (' job_id ' int () NOT null auto_increment, ' job_name ' varchar ($) ' DEFAULT NULL COMMENT ' task Name ', ' job_group ' varchar (+) default NULL COMMENT ' task group ', ' job_status ' varchar (5) Default null COMMENT ' task status 0 disable 1 Enable 2 delete Except ', ' cron_expression ' varchar ($) Default null COMMENT ' task run time expression ', ' bean_class ' varchar ' default null COMMENT ' task Row class ', ' execute_method ' varchar (+) default null COMMENT ' task execution method ', ' create_time ' date DEFAULT null COMMENT ' Task creation time ', ' up Date_time ' date default null COMMENT ' Task update time ', ' job_desc ' varchar ($) Default NULL COMMENT ' task description ', PRIMARY KEY (' job_id ') Engine=innodb auto_increment=11 DEFAULT Charset=utf8;--------------------------------Records of task_detail----- -------------------------INSERT into ' task_detail ' VALUES (' 9 ', ' Test manual set Task ', ' testquartztask ', NULL, ' 0 0 1 * *? ',' Com.zealer.cps.task.executor.TestQuartzTask ', ' Printoneword ', ' 2017-06-22 ', ' 2017-06-22 ', ' Print a word '); 

Then create the corresponding entity class Schedulejob.java

View CodeTwo, spring configuration file Spring.xml
<bean id= "Schedulerfactorybean" class= "Org.springframework.scheduling.quartz.SchedulerFactoryBean"/>

Then join the Web. xml

<context-param>    <param-name>contextConfigLocation</param-name>    <param-value> Classpath:spring.xml</param-value></context-param>
Third, write the task controller Taskcontroller.javaView CodeIv. writing the mission to run the portal, i.e. Jobmethod.javaView CodeFive, write the business layer class Quartzjobservice.javaView CodeVI. Writing SQL mapping XML file Quartzjob.xmlView CodeVii. Modifications and List pages

updatejob.jsp

View Code

tasklist.jsp

View Code

Here, our Spring3 Integration Quartz 2 of the timing task function is finally over, to some of the commonly used functions to achieve, I believe can meet the needs of general projects.

Spring 3 consolidates Quartz 2 for manual set-up of scheduled tasks: New, modify, delete, pause and resume----every moment you are dissatisfied, there is a past you never tried

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.