温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

Spring 中使用Quartz实现任务调度

发布时间:2020-08-20 13:57:30 来源:脚本之家 阅读:107 作者:FlyHeLanMan 栏目:编程语言

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

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

第一步:定义作业类

java代码

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配置代码

<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配置如下:

<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配置如下:

<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配置代码如下:

<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 ,下面是代码:

第一步作业类

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

<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秒执行一次)

<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点执行)

<bean id="cronTrigger" class="org.springframework.scheduling.quartz.CronTriggerBean"> 
   <property name="jobDetail" ref="job2" /> 
   <property name="cronExpression" value="0 0 12 * * ?" /> 
 </bean>

第四步配置调度工厂

<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有如下两种语法格式:

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年

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

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI