温馨提示×

温馨提示×

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

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

Spring项目中QuartZ定时服务的原理是什么

发布时间:2020-11-20 14:41:26 来源:亿速云 阅读:300 作者:Leah 栏目:开发技术

Spring项目中QuartZ定时服务的原理是什么?相信很多没有经验的人对此束手无策,为此本文总结了问题出现的原因和解决方法,通过这篇文章希望你能解决这个问题。

什么是Quartz?

  Quartz是一个强大的企业级任务调度框架。它允许开发人员灵活地定义触发器的调度时间表,并可对触发器和任务进行关联映射。此外,Quartz提供了调度运行环境的持久化机制,可以保存并会发调度现场,即使系统因故障关闭,任务调度现场数据并不会丢失。Spring中继承并简化了Quartz。

如何使用Quartz?

  对于Quartz,我们使用的时候主要是注重两个方面,一个是定时任务的业务,另一个就是Cron表达式。

  1>Quartz存在两种方式来定义定时执行任务,一种是使用QuartJobBean和JobDetailBean;另一种是使用MethodInvokingJobDetailFactoryBean。

  2>Cron表达式包括下面7个字段并区别顺序:秒0-59,分0-59,小时0-23,月内日期1-31,月1-12或者JAN-DEC,周内日期1-7或者SUN-SAT,年(可选字段)留空或者1970-2099并且通过特殊字符表示特殊意义,具体为下:

  • 斜线(/)字符表示增量值。例如,在秒字段中"5/15"代表从第5秒开始,每15秒一次。
     
  • 问号(?)字符和字母L字符只有在月内日期和周内日期字段中可用。问号表示这个字段不包含具体值。所以,如果指定月内日期,可以在周内日期字段中插入"?",表示周内日期值无关紧要。这里有个很蛋疼的设定,无关Quartz,而是Spring集成Quartz后,它自己加的一个约束,那就是:日期(1-31)和星期(SUN-SAT)两者,必须有一个是问号(?),系统在启动的时候,Spring会检查表达式,如果不符合它的规则,就会抛异常。所以在使用的时候这个地方一定要注意,而这个在Linux上执行Cron是没有这个限制的。
     
  • 字母L字符是last的缩写。放在月内日期字段中,表示安排在当月最后一天执行。在周内日期字段中,如果"L"单独存在,就等于"7",否则代表当月内周内日期的最后一个实例。所以"0L"表示安排在当月的最后一个星期日执行。
     
  • 字母(W)字符把执行安排在最靠近指定值的工作日。把"1W"放在月内日期字段中,表示把执行安排在当月的第一个工作日内。
     
  • 井号(#)字符为给定月份指定具体的工作日实例。把"MON#2"放在周内日期字段中,表示把任务安排在当月的第二个星期一。
     
  • 星号(*)字符是通配字符,表示该字段可以接受任何可能的值、表达式例子。
     

例子:

      "0 0 08 * * ?" 每天上午8点触发
      "0 15 10 ? * *" 每天上午10:15触发
      "0 15 10 * * ?" 每天上午10:15触发
      "0 15 10 ? * 6L 2009-2019" 2009年至2019年的每月的最后一个星期五上午10:15触发
      "0 15 10 ? * 6#3" 每月的第三个星期五上午10:15触发

【示例】

  我们使用Spring定时服务Quartz来实现一个每5秒打印一次当前时间的小例子。

  1:定义接口IPrintInfoService类

package demoinfo.spring.quartz;
public interface IPrintInfoService {
  public void print();
}

  2:实现接口类PrintInfoServiceImpl

package demoinfo.spring.quartz;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

import demoinfo.spring.quartz.IPrintInfoService;

public class PrintInfoServiceImpl implements IPrintInfoService{

  public void print() {
    Calendar now = Calendar.getInstance();
    System.out.println("现在是北京时间:" + this.format(now.getTime()));
  }

  public String format(Date date){
    SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
    return sdf.format(date);
  }

}

  3:基于QuartzJobBean的实现类PrintInfoJob

package demoinfo.spring.quartz;

import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
import org.springframework.scheduling.quartz.QuartzJobBean;

import demoinfo.spring.quartz.IPrintInfoService;

public class PrintInfoJob extends QuartzJobBean{
  
  private IPrintInfoService prinfInfoService = null;
  public IPrintInfoService getPrinfInfoService() {
    return prinfInfoService;
  }
  public void setPrinfInfoService(IPrintInfoService prinfInfoService) {
    this.prinfInfoService = prinfInfoService;
  }
  @Override
  protected void executeInternal(JobExecutionContext arg0)
      throws JobExecutionException {
    this.prinfInfoService.print();
  }
}

  4:Spring配置文件applicationContext.xml

<&#63;xml version="1.0" encoding="UTF-8"&#63;>
<beans xmlns="http://www.springframework.org/schema/beans"
  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xmlns:context="http://www.springframework.org/schema/context"
  xmlns:tx="http://www.springframework.org/schema/tx"
  xsi:schemaLocation="http://www.springframework.org/schema/beans
        http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
        http://www.springframework.org/schema/context
        http://www.springframework.org/schema/context/spring-context-2.5.xsd
        http://www.springframework.org/schema/tx
        http://www.springframework.org/schema/tx/spring-tx-2.5.xsd">


  <bean id="printInfoService" class="demoinfo.spring.quartz.PrintInfoServiceImpl" />
  <!-- 配置一个Job -->
  <bean id="printInfoJob" class="org.springframework.scheduling.quartz.JobDetailBean">
    <property name="jobClass" value="demoinfo.spring.quartz.PrintInfoJob" />
    <property name="jobDataAsMap">
      <map>
        <entry key="prinfInfoService" value-ref="printInfoService"></entry>
      </map>
    </property>
  </bean>

  <!-- 简单的触发器 -->
  <bean id="simplePrintInfoTrigger" class="org.springframework.scheduling.quartz.SimpleTriggerBean">
    <property name="jobDetail">
      <ref bean="printInfoJob" />
    </property>
    <property name="startDelay">
      <value>6000</value>
    </property>
    <property name="repeatInterval">
      <value>6000</value>
    </property>
  </bean>

  <!--复杂的触发器 -->
  <bean id="complexPrintInfoTrigger" class="org.springframework.scheduling.quartz.CronTriggerBean">
    <property name="jobDetail">
      <ref bean="printInfoJob" />
    </property>
    <property name="cronExpression">
      <value>00,05,10,15,20,25,30,35,40,45,50,55 * * * * &#63;</value>
    </property>
  </bean>

  <!-- spring触发工厂 -->
  <bean class="org.springframework.scheduling.quartz.SchedulerFactoryBean">
    <property name="triggers">
      <list>
        <ref bean="complexPrintInfoTrigger" />
      </list>
    </property>
  </bean>
</beans>

  5:测试用例类SpringQuartzDemo

package demoinfo.spring.quartz;

import org.springframework.context.support.ClassPathXmlApplicationContext;

public class SpringQuartzDemo {

  public static void main(String[] args) {
    System.out.println("测试开始......");
    new ClassPathXmlApplicationContext(
        "classpath:demoinfo/spring/quartz/applicationContext.xml");
    System.out.println("测试结束......");
  }

}

  运行测试用例,可以看到控制台每过5秒钟就打印一次时间信息。

看完上述内容,你们掌握Spring项目中QuartZ定时服务的原理是什么的方法了吗?如果还想学到更多技能或想了解更多相关内容,欢迎关注亿速云行业资讯频道,感谢各位的阅读!

向AI问一下细节

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

AI