Spring集成Quartz定时器实现定时任务(非注解方式)
程序员文章站
2022-06-09 11:22:44
...
Job 是什么?
Job是一个定时任务,它会每隔多久执行你的方法.你并不需要在意它多久执行一次,你只需把你service方法写好,让你的Job调用就行了。
Job可以理解为工作任务,代码就是实现了org.quartz.Job 或 org.quartz.StatefulJob 接口的java类,当Scheduler决定运行Job时,execute()方法就会被执行。
Job具体可以干啥昵?
①每天定时发送系统邮件
②在指定时候发送短信给用户
第一种方式使用 -->Spring集成Quartz定时器实现定时任务
pom.xml需要的jar包
<properties>
<springframework.version>4.0.6.RELEASE</springframework.version><!-- Spring的版本 -->
<quartz.version>2.2.1</quartz.version><!-- Quartz的版本 -->
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<spring.version>4.1.7.RELEASE</spring.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-web</artifactId>
<version>${springframework.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
<version>${springframework.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context-support</artifactId>
<version>${springframework.version}</version>
</dependency>
<!-- Spring事务的依赖 -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-tx</artifactId>
<version>${springframework.version}</version>
</dependency>
<!-- 定时任务框架Quartz的依赖-->
<dependency>
<groupId>org.quartz-scheduler</groupId>
<artifactId>quartz</artifactId>
<version>${quartz.version}</version>
</dependency>
<!-- 添加Spring支持 -->
<!-- 核心包 -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
<version>${spring.version}</version>
</dependency>
<!-- pring IOC的基础实现,包含访问配置文件、创建和管理bean等 -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-beans</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context-support</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-web</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-tx</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-aop</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-aspects</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jdbc</artifactId>
<version>${spring.version}</version>
</dependency>
</dependencies>
web.xml文件的配置
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>
classpath*:applicationContext-*.xml
</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
resources文件夹要添加下面需要添加2个文件:
applicationContext-jobs.xml
<util:list id="triggers" list-class="java.util.ArrayList">
<ref bean="messageJobTrigger"></ref>
</util:list>
<!--
p:jobClass : 执行job的那个类名包括路径
p:cronExpression : job多久执行一次的配置
-->
<bean id="messageJob"
class="org.springframework.scheduling.quartz.JobDetailFactoryBean"
p:jobClass="com.hp.quartz.MessageDeferJob"
p:name="MessageJob"
/>
<!-- Message Defer Job -->
<bean id="messageJobTrigger"
class="org.springframework.scheduling.quartz.CronTriggerFactoryBean"
p:jobDetail-ref="messageJob"
p:cronExpression="0/5 * * * * ? "
p:name="MessageJobTrigger"
p:priority="5"
/>
applicationContext-quartz.xml
<!--
autoStartup : 是否自动启动 默认为false
startupDelay : 延迟秒数
jobFactory-ref : 将他配置到spring容器中
triggers-ref : 关联触发器的id
-->
<bean id="scheduler"
class="org.springframework.scheduling.quartz.SchedulerFactoryBean"
p:autoStartup="true"
p:startupDelay="10"
p:jobFactory-ref="jobFactory"
p:triggers-ref="triggers"
/>
<bean id="jobFactory"
class="org.springframework.scheduling.quartz.SpringBeanJobFactory"
/>
创建测试的类 MessageDeferJob.java
接下来测试一下效果
上一篇: MFC Timer定时器使用
下一篇: STM32普通定时器TIM6精确延时函数