Spring Integrated scheduled task scheduling framework quartz Combat

Source: Internet
Author: User

Timed task handling is fairly common in program development, and it is not always convenient to use the JDK's Timer class library to do task scheduling, because it does not specify the time of year, month, day, hour, and minute as the Cron service, and we can only convert the time into microseconds and then pass it to it. In quartz we only need to set the cronexpression to complete the timed periodic method call. The core of quartz is the Task Scheduler Scheduler, which manages Job,trigger and Calendar, and each job is a Java class that needs to perform tasks, which executes when the Schelduler dispatches the task, which is the execute of the class ( ) method, and we can also pass data to the job through Jobdatamap. Trigger's role is to decide when to execute a Job,trigger type a total of three, respectively, is simpletrigger, it can achieve basic timing trigger function, such as the execution of a fixed time interval n job. The second type of trigger is Crontrigger, which uses cronexpression to set the time for task execution, such as "0/5 * * * *?" Indicates that it departs every 5 seconds. The third is Nthicludeddaytrigger, a type of trigger used to set the day of a certain interval to perform a task. In this article, we mainly introduce the use of quartz through the second kind of flip-flop. First look at the configuration file:

<?xml version= "1.0" encoding= "UTF-8"? ><! DOCTYPE beans Public "-//spring//dtd bean//en" "Http://www.springframework.org/dtd/spring-beans.dtd" ><beans ><!--Configuration Scheduler Quartz, where Jobdetail is configured in two ways--><!--way one: Using Jobdetailbean, the task class must implement the job interface--><bean id= "Myjob "Class=" Org.springframework.scheduling.quartz.JobDetailBean "><property name=" name "value=" Examplejob "> </property><property name= "Jobclass" value= "Com.yangchangshou.SpringQtz" ></property>< Property Name= "Jobdataasmap" ><map><entry key= "service" ><value>simple </entry></map></property></bean><!--Run, please comment out the way! --><!--Way Two: Using Methodinvokingjobdetailfactorybean, the task class can not implement the job interface, through Targetmethod to specify the calling method--><!-- Define methods in target beans and beans--><bean id= "springqtzjob" class= "Com.yangchangshou.SpringQtz" ></bean><bean ID = "Springqtzjobmethod" class= "Org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBeaN "><property name=" TargetObject "><ref bean=" springqtzjob "/></property><property name=" Targetmethod "> <!--the method name to execute--><value>execute</value></property></bean><!--= ======================= Dispatch Trigger ========================--><bean id= "Crontriggerbean" class= " Org.springframework.scheduling.quartz.CronTriggerBean "><property name=" Jobdetail "ref=" Springqtzjobmethod " ></property><property name= "cronexpression" value= "0/5 * * * *?" ></property></bean><!--======================== Dispatch factory ========================--><bean id= "Springjobschedulerfactorybean" class= "Org.springframework.scheduling.quartz.SchedulerFactoryBean" >< Property name= "Triggers" ><list><ref bean= "Crontriggerbean"/></list></property></  Bean></beans>

In This configuration file we define a job class in which we rewrite the Execute method and execute the job class when scheduler triggers the job class based on the interval set by Trrigger. Here we will demonstrate a simple demo bar, to see my job class:

public class Springqtz {private static int counter = 0;public void execute ()  {counter++; System.out.println ("First" + Counter + "times, Hello");}}

The Execute () method is executed every 5 seconds based on the set interval, resulting in the following results:


The execution results in 15 seconds are as shown. In the actual project according to the actual demand, business implementation will be more complex, but their principle is the same, regardless of is more code just. It is hoped that such a simple small example would be helpful to those who initially learn the quartz framework.

Spring Integrated scheduled task scheduling framework quartz Combat

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.