Quartz of Spring Task Scheduling

1. In terms of the inheritance method of Quartz job classes, it can be divided into two categories:

  1. The job class needs to inherit from a specific job class base class. For example, in Quartz, it needs to inherit from org.springframework.scheduling.quartz.QuartzJobBean; in java.util.Timer, it needs to inherit from java.util.TimerTask.
  2. Job classes are ordinary java classes that do not need to inherit from any base class.

Note: It is recommended to use the second method, because all the classes are ordinary classes and do not need to be treated differently in advance.

  • From the trigger timing of task scheduling, here are mainly the triggers used for jobs, there are mainly the following two:
  1. It is triggered every specified time. The corresponding trigger in Quartz is: org.springframework.scheduling.quartz.SimpleTriggerBean
  2. It is triggered once every specified time. The corresponding scheduler in Quartz is: org.springframework.scheduling.quartz.CronTriggerBean

Note: Not every task can use these two triggers, such as java.util.TimerTask task can only use the first one. Both Quartz and spring tasks can support both trigger conditions.


 

Second, the first, the job class inherits from a specific base class: org.springframework.scheduling.quartz.QuartzJobBean

   Step 1: Define the Job Class

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
import  org.quartz.JobExecutionContext; 
import  org.quartz.JobExecutionException; 
import  org.springframework.scheduling.quartz.QuartzJobBean; 
public  class  Job1  extends  QuartzJobBean { 
   
private  int  timeout; 
private  static  int  i =  0
//调度工厂实例化后,经过timeout时间开始执行调度 
public  void  setTimeout( int  timeout) { 
this .timeout = timeout; 
   
/**
* 要调度的具体任务
*/ 
@Override 
protected  void  executeInternal(JobExecutionContext context) 
throws  JobExecutionException { 
   System.out.println( "定时任务执行中…" ); 

  Step 2: Configure the job class JobDetailBean in the spring configuration file 

1
2
3
4
5
6
7
8
<bean name= "job1"  class = "org.springframework.scheduling.quartz.JobDetailBean"
<property name= "jobClass"  value= "com.gy.Job1"  /> 
<property name= "jobDataAsMap"
    <map> 
        <entry key= "timeout"  value= "0"  /> 
    </map> 
</property> 
</bean> 

 Description: org.springframework.scheduling.quartz.JobDetailBean has two properties, the jobClass property is the task class we defined in the java code, and the jobDataAsMap property is the property value that needs to be injected in the task class. 

   Step 3: Configure the trigger method (trigger) of job scheduling

   There are two kinds of job triggers in Quartz, they are

            org.springframework.scheduling.quartz.SimpleTriggerBean

            org.springframework.scheduling.quartz.CronTriggerBean

 

            The first SimpleTriggerBean only supports calling tasks at a certain frequency, such as running once every 30 minutes. The configuration is as follows:

1
2
3
4
5
<bean id= "simpleTrigger"  class = "org.springframework.scheduling.quartz.SimpleTriggerBean"
<property name= "jobDetail"  ref= "job1"  /> 
<property name= "startDelay"  value= "0"  /><!– 调度工厂实例化后,经过 0 秒开始执行调度 –> 
<property name= "repeatInterval"  value= "2000"  /><!– 每 2 秒调度一次 –> 
</bean> 

           The second type of CronTriggerBean supports running once at a specified time, such as running once a day at 12:00. The configuration is as follows:

1
2
3
4
5
<bean id= "cronTrigger"  class = "org.springframework.scheduling.quartz.CronTriggerBean"
<property name= "jobDetail"  ref= "job1"  /> 
<!—每天 12 : 00 运行一次 —> 
<property name= "cronExpression"  value= "0 0 12 * * ?"  /> 
</bean> 

 

Step 4: Configure the scheduling factory 

1
2
3
4
5
6
7
<bean  class = "org.springframework.scheduling.quartz.SchedulerFactoryBean"
<property name= "triggers"
<list> 
<ref bean= "cronTrigger"  /> 
</list> 
</property> 
</bean> 

 Description: This parameter specifies the name of the previously configured trigger.

  Step 5: Start your application, that is, deploy the project to tomcat or other containers.


 

Third, the second, the job class does not inherit a specific base class.

    Spring supports this approach thanks to two classes:

         org.springframework.scheduling.timer.MethodInvokingTimerTaskFactoryBean

        org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean

这两个类分别对应spring支持的两种实现任务调度的方式,即前文提到到java自带的timer task方式和Quartz方式。这里我只写MethodInvokingJobDetailFactoryBean的用法,使用该类的好处是,我们的任务类不再需要继承自任何类,而是普通的pojo。

第一步:编写任务类

1
2
3
4
5
public  class  Job2 { 
public  void  doJob2() { 
System.out.println( "不继承QuartzJobBean方式-调度进行中…" ); 
}

说明:可以看出,这就是一个普通的类,并且有一个方法。

第二步:配置作业类

1
2
3
4
5
6
7
8
<bean id= "job2" 
class = "org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean"
<property name= "targetObject"
<bean  class = "com.gy.Job2"  /> 
</property> 
<property name= "targetMethod"  value= "doJob2"  /> 
<property name= "concurrent"  value= "false"  /><!– 作业不并发调度 –> 
</bean> 

 说明:这一步是关键步骤,声明一个MethodInvokingJobDetailFactoryBean,有两个关键属性:targetObject指定任务类,targetMethod指定运行的方法。往下的步骤就与方法一相同了,为了完整,同样贴出。

第三步:配置作业调度的触发方式(触发器)

   Quartz的作业触发器有两种,分别是

             org.springframework.scheduling.quartz.SimpleTriggerBean

             org.springframework.scheduling.quartz.CronTriggerBean

 

 第一种SimpleTriggerBean,只支持按照一定频度调用任务,如每隔30分钟运行一次。配置方式如下:

1
2
3
4
5
<bean id= "simpleTrigger"  class = "org.springframework.scheduling.quartz.SimpleTriggerBean"
<property name= "jobDetail"  ref= "job2"  /> 
<property name= "startDelay"  value= "0"  /><!– 调度工厂实例化后,经过 0 秒开始执行调度 –> 
<property name= "repeatInterval"  value= "2000"  /><!– 每 2 秒调度一次 –> 
</bean> 

 第二种CronTriggerBean,支持到指定时间运行一次,如每天12:00运行一次等。配置方式如下:

1
2
3
4
5
ronTriggerBean"> 
<property name= "jobDetail"  ref= "job2"  /> 
<!—每天 12 : 00 运行一次 —> 
<property name= "cronExpression"  value= "0 0 12 * * ?"  /> 
</bean> 

以上两种调度方式根据实际情况,任选一种即可。

 

第四步:配置调度工厂 

1
2
3
4
5
6
7
<bean  class = "org.springframework.scheduling.quartz.SchedulerFactoryBean"
<property name= "triggers"
<list> 
<ref bean= "cronTrigger"  /> 
</list> 
</property> 
</bean> 

说明:该参数指定的就是之前配置的触发器的名字。

第五步:启动你的应用即可,即将工程部署至tomcat或其他容器。  

 

到此,spring中Quartz的基本配置就介绍完了,当然了,使用之前,要导入相应的spring的包与Quartz的包,这些就不消多说了。

其实可以看出Quartz的配置看上去还是挺复杂的,没有办法,因为Quartz其实是个重量级的工具,如果我们只是想简单的执行几个简单的定时任务,有没有更简单的工具,有!

 

引用原文:http://www.cnblogs.com/hongwz/p/5642429.html

 

写博客是为了记住自己容易忘记的东西,另外也是对自己工作的总结,文章可以转载,无需版权。希望尽自己的努力,做到更好,大家一起努力进步!

如果有什么问题,欢迎大家一起探讨,代码如有问题,欢迎各位大神指正!

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325097318&siteId=291194637