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并且通过特殊字符表示特殊意义,具体为下:
例子:
"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
<?xml version="1.0" encoding="UTF-8"?>
<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 * * * * ?</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定时服务的原理是什么的方法了吗?如果还想学到更多技能或想了解更多相关内容,欢迎关注亿速云行业资讯频道,感谢各位的阅读!
亿速云「云服务器」,即开即用、新一代英特尔至强铂金CPU、三副本存储NVMe SSD云盘,价格低至29元/月。点击查看>>
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。