服务器之家:专注于服务器技术及软件下载分享
分类导航

PHP教程|ASP.NET教程|Java教程|ASP教程|编程技术|正则表达式|C/C++|IOS|C#|Swift|Android|VB|R语言|JavaScript|易语言|vb.net|

服务器之家 - 编程语言 - Java教程 - Spring 中使用Quartz实现任务调度

Spring 中使用Quartz实现任务调度

2020-08-12 10:50FlyHeLanMan Java教程

这篇文章主要介绍了Spring 中使用Quartz实现任务调度,Spring中使用Quartz 有两种方式,感兴趣的小伙伴们可以参考一下。

前言:Spring中使用Quartz 有两种方式,一种是继承特定的基类:org.springframework.scheduling.quartz.QuartzJobBean,另一种则不需要,(推荐使用第二种)。下面分别介绍。

1、作业类继承 org.springframework.scheduling.quartz.QuartzJobBean

第一步:定义作业类

java代码

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
import java.text.SimpleDateFormat;
import java.util.Date;
 
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
import org.springframework.scheduling.quartz.QuartzJobBean;
 
public class Job1 extends QuartzJobBean{
  
  //这个参数值由xml配置传过来
  private int timeout;
  
  public void setTimeout(int timeout) {
    this.timeout = timeout;
  }
 
  @Override
  protected void executeInternal(JobExecutionContext content) throws JobExecutionException {
    SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
    System.out.println(sdf.format(new Date()) + "job1执行" + "这是xml里给timeout赋值" + timeout);
  }
}

第二步spring中配置JobDetailBean

spring.xml配置代码

?
1
2
3
4
5
6
7
8
9
10
<bean id = "job1" class="org.springframework.scheduling.quartz.JobDetailBean">
   <!-- 这里指向写好的作业类 -->
   <property name="jobClass" value="com.ccg.job.Job1" />
   <property name="jobDataAsMap">
     <map>
       <!-- 这里写参数可以传到作业类中定义的参数 -->
       <entry key="timeout" value="10"></entry>
     </map>
   </property>
 </bean>

 第三步配置触发方式

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

org.springframework.scheduling.quartz.SimpleTriggerBean  ,按照一定频率执行任务

org.springframework.scheduling.quartz.CronTriggerBean ,支持cron表达式,可以指定时间执行,也可以按照频率执行

第一种 SimpleTriggerBean,比如每两秒执行一次,xml配置如下:

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

第二种 CronTriggerBean,比如每天12点执行,xml配置如下:

?
1
2
3
4
<bean id="cronTrigger" class="org.springframework.scheduling.quartz.CronTriggerBean">
   <property name="jobDetail" ref="job1" />
   <property name="cronExpression" value="0 0 12 * * ?" /> <!-- 每天12天执行任务 -->
 </bean>

Cron表达式格式最后面介绍。

第四步配置调度工厂

spring.xml配置代码如下:

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

第五步启动应用,查看任务调度执行情况。

2、作业类不需要继承,只是普通的java类

主要的类是org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean ,下面是代码:

第一步作业类

?
1
2
3
4
5
6
7
8
9
10
import java.text.SimpleDateFormat;
import java.util.Date;
 
public class Job2 {
  
  public void run(){
    SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
    System.out.println(sdf.format(new Date()) + "这里是job2的执行");
  }
}

第二步在spring.xml中配置job2

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

targetObject 执行作业类 targetMethod指向作业类中要执行的方法

第三步配置触发方式,同样是有两种一种SimpleTrggerBean,一种CronTrggerBean

第一种配置xml如下:(每2秒执行一次)

?
1
2
3
4
5
<bean id="simpleTrigger" class="org.springframework.scheduling.quartz.SimpleTriggerBean">
   <property name="jobDetail" ref="job2" />
   <property name="startDelay" value="10000" />
   <property name="repeatInterval" value="2000" />
 </bean>

第二种配置xml如下:(每天12点执行)

?
1
2
3
4
<bean id="cronTrigger" class="org.springframework.scheduling.quartz.CronTriggerBean">
   <property name="jobDetail" ref="job2" />
   <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="simpleTrigger"/>
    </list>
  </property>
</bean>

如果使用CronTriggerBean 需要把simpleTrigger 换成simpleTrigger

最后启动服务,查看任务调度执行情况。

附:Cron表达式

Cron表达式是一个字符串,字符串以5或6个空格隔开,分为6或7个域,每一个域代表一个含义,Cron有如下两种语法格式:

?
1
2
Seconds Minutes Hours DayofMonth Month DayofWeek Year//或
Seconds Minutes Hours DayofMonth Month DayofWeek

每一个域可出现的字符如下:

  • Seconds:可出现", - * /"四个字符,有效范围为0-59的整数
  • Minutes:可出现", - * /"四个字符,有效范围为0-59的整数
  • Hours:可出现", - * /"四个字符,有效范围为0-23的整数
  • DayofMonth:可出现", - * / ? L W C"八个字符,有效范围为0-31的整数
  • Month:可出现", - * /"四个字符,有效范围为1-12的整数或JAN-DEc
  • DayofWeek:可出现", - * / ? L C #"四个字符,有效范围为1-7的整数或SUN-SAT两个范围。1表示星期天,2表示星期一, 依次类推
  • Year:可出现", - * /"四个字符,有效范围为1970-2099年

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持服务器之家。

原文链接:http://www.cnblogs.com/FlyHeLanMan/p/6401776.html

延伸 · 阅读

精彩推荐