"SSH Project Combat" national Tax cooperative Platform -34.quartz&crontrigger

Source: Internet
Author: User

The last time we used the Quartz Task Scheduler framework to create a task that ran in the background at intervals, we used the Simpletrigger simple trigger, in order to achieve our end of the month auto-reply function, we want to set the trigger can at some precise time to automatically perform the task, Then using the Simpletrigger simple trigger is not enough, we need to use the Crontrigger task trigger to implement this function.

Our Crontrigger setup steps are as follows:
Task Trigger (Crontrigger)
① Setting Task Details
② Set Execution Time (cronexpression)
Cronexpression: Second-time anniversary (optional)

Let's first write another execution method in the task class:
Package Cn.edu.hpu.tax.complain;import Java.text.simpledateformat;import Java.util.date;public class QuartzTask { public void Dosimpletriggertask () {SimpleDateFormat sdf=new SimpleDateFormat ("Yyyy-mm-dd HH:mm:ss"); System.out.println ("Doing Simpletrigger task ..." +sdf.format (new Date ())); public void Docrontriggertask () {SimpleDateFormat sdf=new SimpleDateFormat ("Yyyy-mm-dd HH:mm:ss"); System.out.println ("Doing Crontrigger task ..." +sdf.format (new Date ()));}}

is the Docrontriggertask () above

Then modify the configuration file for our quartz quartz-spring.xml as follows:
<?xml version= "1.0" encoding= "UTF-8"? ><beans xmlns= "Http://www.springframework.org/schema/beans" xmlns: Xsi= "Http://www.w3.org/2001/XMLSchema-instance" xmlns:p= "http://www.springframework.org/schema/p" xmlns:context= "Http://www.springframework.org/schema/context" xmlns:aop= "HTTP://WWW.SPRINGFRAMEWORK.ORG/SCHEMA/AOP" xmlns:tx= " Http://www.springframework.org/schema/tx "xsi:schemalocation=" Http://www.springframework.org/schema/beans http:/ /www.springframework.org/schema/beans/spring-beans-3.0.xsd HTTP://WWW.SPRINGFRAMEWORK.ORG/SCHEMA/AOP/HTTP Www.springframework.org/schema/aop/spring-aop-3.0.xsd HTTP://WWW.SPRINGFRAMEWORK.ORG/SCHEMA/TX/HTTP Www.springframework.org/schema/tx/spring-tx-3.0.xsdhttp://www.springframework.org/schema/context/HTTP Www.springframework.org/schema/context/spring-context-3.0.xsd "> <!--Register a regular bean--<bean id=" quartz Task "class=" Cn.edu.hpu.tax.complain.QuartzTask "></bean> <!--1. Specify task details--<bean id=" JOBDEtial1 "class=" Org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean "> <!--① Set execution Object-- > <property name= "targetObject" ref= "Quartztask" ></property> <!--② Set the execution method corresponding to the execution object--<    Property Name= "Targetmethod" value= "Dosimpletriggertask" ></property> <!--③ can be executed synchronously (this setting is not synchronized)-- <property name= "Concurrent" value= "false" ></property> </bean> <bean id= "JobDetial2" class= "or G.springframework.scheduling.quartz.methodinvokingjobdetailfactorybean "> <!--① Set Execution Object--<property NA Me= "TargetObject" ref= "Quartztask" ></property> <!--② Set the corresponding execution method in the execution object--<property name= "Targetme Thod "value=" Docrontriggertask "></property> <!--③ can be executed synchronously (this setting is not synchronized)--<property name=" concur Rent "value=" false "></property> </bean> <!--2. Set Task execution time (Task execution trigger)--<bean id=" simple Trigger "class=" Org.springframEwork.scheduling.quartz.SimpleTriggerBean "> <!--① setup task details-<property name=" Jobdetail "ref=" JobDetial1 ></property> <!--② Set task delay execution time (2 seconds delay)-<property name= "Startdelay" value= "></property" > <!--③ set task execution frequency (execution frequency is executed every 2 seconds)-<property name= "Repeatinterval" value= "$" ></property> &  lt;/bean> <bean id= "Crontrigger" class= "Org.springframework.scheduling.quartz.CronTriggerBean" > <!--    Set task Details-<property name= "Jobdetail" ref= "JobDetial2" ></property> <!--set task execution time, cron expression-- <property name= "cronexpression" value= "0/3 * * * *?" ></property> </bean> <!--3. Set up dispatch factory--<bean class= "Org.springframework.scheduling.qu Artz. Schedulerfactorybean "> <property name=" triggers "> <list> <!--First trigger--<ref bean=" Si Mpletrigger "/> <!--second Trigger--<ref bean=" Crontrigger "/> </list> </property> </bean></beans> 

What is received in the Cronexpression expression property in Crontrigger:
In the expression, the time format is separated by a space, each time means: The second minute hour The Sun and the moon anniversary (optional)
The values that can appear and conform to:


The ' * ' character can be used for all fields and set to "*" in the "sub" field to mean "every minute".

The '? ' character can be used in the "Day" and "Week" fields. It is used to specify ' ambiguous values '. This is used when you need to specify one of these two fields instead of the other. In the following example, you can see its meaning.

The '-' character is used to specify a range of values, such as "10-12" in the "Hour" field for "10 point to 12 points".

The ', ' character specifies a number of values. For example, in the "Weeks" field is set to "Mon,wed,fri" means "The days Monday, Wednesday, and Friday".

The '/' character is used to specify the increment of a value. For example, the "Seconds" field is set to "0/15" for "No. 0, 15, 30, and 45 seconds". "5/15" means "5th, 20, 35, and 50". The "*" character in front of '/' is equivalent to the specified starting at 0 seconds. Each field has a series of values that can start or end. For the seconds and minutes fields, the values range from 0 to 59, which is 0 to 23 for the hour field, 0 to 31 for the day field, and 1 to 12 for the month field. The "/" field is simply a value that helps you to start "nth" within the allowable numeric range. So for the "month" field, "7/6" is only meant to be opened in July instead of "every six months", please note the subtle differences.

The ' L ' character can be used in both the "Day" and "Week" fields. It is the abbreviation for "last", but it has different meanings in both fields. For example, "L" in the day field means "the last day of one months"-for January is number 31st for February is 28th (not a leap year). In the "Weeks" field, it simply represents "7" or "SAT", but if it is followed by a number in the "Weeks" field, it means "the last one weeks of the month X"-such as "6L" for "The last Friday of the month". When using the ' L ' option, it is important to specify the list or scope of the determination, otherwise you will be confused by the result.

The ' W ' is available for the day field. Used to specify the most recent weekday (Monday to Friday) for the given date of the calendar. For example, you set the "Day" field to "15W", meaning: "The most recent working day from the month of 15th." So if number 15th is Saturday, the trigger will be called at 14th, or Friday. If number 15th is Sunday, the trigger will be triggered at 16th, or Monday. If number 15th is Tuesday, it will be triggered the same day. However, if you set the "Day" field to "1W" and the number is Saturday, the trigger will be triggered in the next Monday, which is the 3rd number of the month, because it will not cross the range boundary of the value of the month. The ' W ' character can only be used when the value of the day field is a single day instead of a series of values.

' L ' and ' W ' can be combined for the ' Day ' field as ' LW ', meaning ' last working day of the month '.

The ' # ' character can be used in the "Weeks" field. The character denotes "week X of the month", such as "6#3", which represents the third Friday of the month (6 for Friday and the third for the month "#3"). Another example: "2#1" = the first Monday of the month and "4#5" = Fifth Wednesday of the month. Note If you specify "#5" that the month does not have a fifth "week X", the month is not triggered.

The ' C ' character can be used for the "Day" and "Week" fields, which are abbreviations for "calendar". It represents the value, if any, that is computed based on the associated calendar. If there is no associated calendar, it is equivalent to including all calendars. The "Day" field value is "5C" for "the first day of the calendar or 5th and later", and the "Weeks" field value "1C" means "the first day of the calendar or Sunday and beyond."

Valid characters are not case-sensitive for the month field and the weeks field.

Examples of official documents:



What we set in it is "0/3 * * * *?" It means performing a task every 3 seconds every day. We performed with the previous Simpletrigger to test whether our configuration was successful:
Restart the server and find the console output the following information:
...... Info: Server Startup in 16932 msdoing Simpletrigger task ... 2015-12-07 09:09:44doing Crontrigger Task ... 2015-12-07 09:09:45doing Simpletrigger Task ... 2015-12-07 09:09:46doing Crontrigger Task ... 2015-12-07 09:09:48doing Simpletrigger Task ... 2015-12-07 09:09:48doing Simpletrigger Task ... 2015-12-07 09:09:50doing Crontrigger Task ... 2015-12-07 09:09:51doing Simpletrigger Task ... 2015-12-07 09:09:52doing Crontrigger Task ... 2015-12-07 09:09:54doing Simpletrigger Task ... 2015-12-07 09:09:54doing Simpletrigger Task ... 2015-12-07 09:09:56doing Crontrigger Task ... 2015-12-07 09:09:57doing Simpletrigger Task ... 2015-12-07 09:09:58 ...

We can see Simpletrigger and Crontrigger executing at the same time, and Crontrigger is executed every 3 seconds.
This proves that our configuration is successful. You can test other configurations according to the configuration rules given above, no more one by one tests.

And then we can start to meet our needs.
Our demand is the last day of the month to automatically process the non-reply to the speed of the request, the no reply to the complaint set to "expired." You cannot reply to this type of complaint in background management.

Timing: The last day of the month at the end of each cronexpression:10 2 L *? (2:10 A.M. 10 seconds Automatic processing, in order to avoid peak work time)
What to do: automatically handle pending complaints before this month; Change the status of the complaint information to an expired
How to perform:
Pending complaint before this month: SELECT * from complain where complaint time < month 1th 0:0 0 seconds


First add our configuration to the spring configuration file under the package of our "complaints" module:
<?xml version= "1.0" encoding= "UTF-8"? ><beans xmlns= "Http://www.springframework.org/schema/beans" xmlns: Xsi= "Http://www.w3.org/2001/XMLSchema-instance" xmlns:p= "http://www.springframework.org/schema/p" xmlns:context= "Http://www.springframework.org/schema/context" xmlns:aop= "HTTP://WWW.SPRINGFRAMEWORK.ORG/SCHEMA/AOP" xmlns:tx= " Http://www.springframework.org/schema/tx "xsi:schemalocation=" Http://www.springframework.org/schema/beans http:/ /www.springframework.org/schema/beans/spring-beans-3.0.xsd HTTP://WWW.SPRINGFRAMEWORK.ORG/SCHEMA/AOP/HTTP Www.springframework.org/schema/aop/spring-aop-3.0.xsd HTTP://WWW.SPRINGFRAMEWORK.ORG/SCHEMA/TX/HTTP Www.springframework.org/schema/tx/spring-tx-3.0.xsdhttp://www.springframework.org/schema/context/HTTP Www.springframework.org/schema/context/spring-context-3.0.xsd "> <!--inherited the abstract class injected into the Sessionfactory, No re-entry sessionfactory--<bean id= "Complaindao" class= "Cn.edu.hpu.tax.complain.dao.impl.ComplainDaoImpl" Parent= "Xdao" ></bean> <!--Scan Service--<context:component-scan base-package= "Cn.edu.hpu.tax.complain.service.impl ></context:component-scan> <!--1. Specify task details-<bean id= "Complainjobdetail" class= "org.spring Framework.scheduling.quartz.MethodInvokingJobDetailFactoryBean "> <!--① Set Execution Object--<property name=" Targ Etobject "ref=" Complainservice "></property> <!--② Set execution method corresponding to execution object--<property name=" Targetmetho D "value=" Autodeal "></property> <!--whether ③ can be executed synchronously (this setting is not synchronized)--<property name=" concurrent "value=  "False" ></property> </bean> <!--2. Set Task execution time (Task execution trigger)-<bean id= "Complaincrontrigger" class= "Org.springframework.scheduling.quartz.CronTriggerBean" > <!--setup Task details-<property name= "Jobdetai L "ref=" Complainjobdetail ></property> <!--set task execution time, cron expression--<property name= "Cronexpression" V Alue= "Ten 2 L *?" ></property> </bean> <!--3. Set up dispatch factory--<bean class= "Org.springframework.scheduling.quartz . Schedulerfactorybean "> <property name=" triggers "> <list> <!--trigger--and <ref bean=" compl Aincrontrigger "/> </list> </property> </bean></beans>

Then we can see that our task execution class is complainservice, where the task execution method is the Autodetail method, and we are going to write the Autodetail method in the Complainservice class below:
This method is defined in the interface:
Package Cn.edu.hpu.tax.complain.service;import Cn.edu.hpu.tax.complain.entity.complain;import Cn.edu.hpu.tax.core.service.baseservice;public interface Complainservice extends baseservice<complain> {// Automatic handling of complaints public void autodeal ();


The method is implemented in the first class:
Package Cn.edu.hpu.tax.complain.service.impl;import Java.util.calendar;import Java.util.list;import Javax.annotation.resource;import Org.springframework.stereotype.service;import Cn.edu.hpu.tax.complain.dao.complaindao;import Cn.edu.hpu.tax.complain.entity.complain;import Cn.edu.hpu.tax.complain.service.complainservice;import Cn.edu.hpu.tax.core.service.impl.baseserviceimpl;import Cn.edu.hpu.tax.core.util.QueryHelper, @Service ("Complainservice") public class Complainserviceimpl extends baseserviceimpl<complain> Implementscomplainservice {private Complaindao Complaindao; @Resourcepublic void Setcomplaindao (Complaindao Complaindao) {Super.setbasedao (Complaindao); This.complaindao = ComplainDao;} @Overridepublic void Autodeal () {//1, consult the list of complaints pending before this month Queryhelper queryhelper=new queryhelper (Complain.class, "C"); Queryhelper.addcondition ("c.state=", Complain.complain_state_undone); Calendar cal=calendar.getinstance (); Cal.set (Calendar.day_of_month, 1);//Set the current time with a date of 1th number Cal.set (calendar.hour_of_day , 0)///Set the current time to a date of 0 o'clock Cal.set (calendar.minute, 0);//Set the current time to 0 points cal.set (calendar.second, 0);// Set the current time to a date of 0 seconds queryhelper.addcondition ("C.comptime <?", Cal.gettime ()); List<complain> list=findobjects (Queryhelper); if (list! = null && list.size () >0) {//2, The status of the update complaint information is expired for (complain comp:list) {comp.setstate (complain.complain_state_invalid); update (comp);}}}

In this way, we will automatically execute the Autodeal method in Complainservice at the end of each month, 2:10 A.M. 10 seconds, to set all the non-reply complaint messages before this month as "invalid".

Then "invalid" complaint is not able to reply, we add this control in the JSP, so that all invalid complaints "reply" link invalid:
<TD align= "center" >     <s:if test= "state! = 2" >         <a href= "javascript:dodeal (' <s:property value = ' compid '/> ') > Reception </a>     </s:if></td>
Effect:


At this point, our quartz and Simpletrigger, Crontrigger study completed, the end of the automatic reply function has been realized.

Reprint Please specify source: http://blog.csdn.net/acmman/article/details/50205893

"SSH Project Combat" national Tax cooperative Platform -34.quartz&crontrigger

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.