Spring中实现定时调度的几种方法
作者:迈步出de围城 时间:2021-08-29 13:04:44
1,内容简介
所谓的定时调度,是指在无人值守的时候系统可以在某一时刻执行某些特定的功能采用的一种机制,对于传统的开发而言,定时调度的操作分为两种形式:
定时触发:到某一时间点上执行某些处理操作;
间隔触发:每隔几秒后进行某些操作的自动处理。
所有的处理都依赖于计算机系统底层的时钟发生器,在java最初的实现过程里面,真对于定时处理专门提供有两个类:Timer,TimerTask两个类,其中TimerTask主要是定义任务的执行,相当于启动一个线程去执行某些任务。
public class MyTask extends TimerTask{
@Override
public void run() {//定义要执行的任务
// TODO Auto-generated method stub
String currentTime=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS").format(new Date());
System.out.println(currentTime);
}
}
public class MyTaskTest {
public static void main(String[] args) {
Timer timer=new Timer();
timer.schedule(new MyTask(), 1000);//启动任务,延迟1秒后执行。
}
}
但是,如果要求要在每年的某月某时某分某秒执行某个任务,使用Timer和TimerTask就无能为力了。在项目开发中往往会有两种定时控制的选择:
quartz组件:企业及定时调度组件,需要单独配置;
SpringTask:轻量级组件,配置简单,可以利用Annotation实现配置处理。
2,Quartz定义定时调度
使用Quartz组件,我们需要导入quartz的开发包,在pom.xml中添加quartz的开发包。
<dependency>
<groupId>org.quartz-scheduler</groupId>
<artifactId>quartz</artifactId>
<version>2.2.3</version>
</dependency>
引入包后,就可以进行定时调度的开发了。
有两种实现模式:
去继承QuartzJobBean父类;
直接利用配置就可以实现方法的调度控制。
1,继承一个父类实现任务的处理。
public class MyTask2 extends QuartzJobBean{
@Override
protected void executeInternal(JobExecutionContext context) throws JobExecutionException {
// TODO Auto-generated method stub
String currentTime=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS").format(new Date());
System.out.println(currentTime);
System.out.println("具体的任务实现!!!");
}
}
所有的定时调度的启用都要在Spring的控制文件中完成,即,不需要去写一个明确的类来进行定时任务启用。
2,在applicationContext.xml文件中增加定时调度的配置,通过定时调度工厂类实现。
<bean id="taskFactory"
class="org.springframework.scheduling.quartz.JobDetailFactoryBean">
<property name="jobClass" value="cn.wnh.timerSask.MyTask1" />
<property name="jobDataMap">
<map>
<entry key="timeout" value="0" />
</map>
</property>
</bean>
随后配置任务的触发作业,对于作业的配置有两类:
使用间隔触发:若干时间之后重复执行;
工厂类:org.springframework.scheduling.quartz.SimpleTriggerFactoryBean
<bean id="simpleTrigger"
class="org.springframework.scheduling.quartz.SimpleTriggerFactoryBean">
<!-- 定义间隔触发的执行程序类 -->
<property name="jobDetail" ref="taskFactory"></property>
<!-- 设置定时触发延迟时间 -->
<property name="startDelay" value="0"></property>
<!-- 单位是”毫秒“ -->
<property name="repeatInterval" value="2000"></property>
</bean>
设置间隔触发调度器:org.springframework.scheduling.quartz.SchedulerFactoryBean
<bean class="org.springframework.scheduling.quartz.SchedulerFactoryBean">
<property name="triggers">
<list>
<ref bean="simpleTrigger" />
</list>
</property>
</bean>
3,此时所有的间隔触发控制都交由Spring管理了,现在只需要启动Spring容器即可实现间隔触发任务。
使用Cron实现定时触发
Quartz不仅可以实现间隔触发,它还可以结合Cron实现定时触发,这也是它最重要的功能。
一般项目中使用最多的模式:小时触发,月初触发,年初触发。
修改之前的间隔触发配置,使用CronTriggerFactoryBean实现定时触发。
<bean id="taskFactory"
class="org.springframework.scheduling.quartz.JobDetailFactoryBean">
<property name="jobClass" value="cn.wnh.timerSask.MyTask1" />
<property name="jobDataMap">
<map>
<entry key="timeout" value="0" />
</map>
</property>
</bean>
<bean id="cronTrigger"
class="org.springframework.scheduling.quartz.CronTriggerFactoryBean">
<property name="jobDetail" ref="taskFactory" />
<!-- cron表达式,描述每分钟触发一次 -->
<property name="cronExpression" value="0 * * * * ?" />
</bean>
<bean class="org.springframework.scheduling.quartz.SchedulerFactoryBean">
<property name="triggers">
<list>
<ref bean="cronTrigger" />
</list>
</property>
</bean>
启动Spring容器即可实现。
2,不继承任何类实现定时调度
在项目开发中,继承直接会导致单继承的局限控制,所以在这种情况下Spring中提供了一种可以不继承任何类即可实现定时操作的任务处理。
定义一个任务执行类,不继承任何类。
public class MyTask2 {
public void taskSelf(){
String task=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS").format(new java.util.Date());
System.out.println(task);
System.out.println("执行具体任务操作");
}
}
在applicationContext.xml在配置工厂类:org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean
<bean id="taskFactory2"
class="org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean">
<property name="targetObject">
<bean class="cn.wnh.timerSask.MyTask2" />
</property>
<!--配置要执行的方法 -->
<property name="targetMethod" value="taskSelf" />
</bean>
随后在任务调度配置上配置新的程序类
<bean id="cronTrigger"
class="org.springframework.scheduling.quartz.CronTriggerFactoryBean">
<property name="jobDetail" ref="taskFactory2" />
<!-- cron表达式,描述每分钟触发一次 -->
<property name="cronExpression" value="* * * * * ?" />
</bean>
启动容器即可实现定时调度。
这种模式没有类的继承依赖,处理会更加灵活。
Spring Task实现定时调度
在Spring中自己有对定时调度的支持,使用起来感觉比Quartz还要好用。
它有两种实现方式,1,在applicationContext.xml中配置实现;2,使用Annotation实现。
不过使用什么模式,必须先有一个任务处理类。
定义任务处理类。
这里直接使用之前的MyTask2类,不再重复写。
修改applicationContext.xml文件:
需要追加task处理的命名空间定义:
<beans xmlns:task="http://www.springframework.org/schema/task"
http://www.springframework.org/schema/task
http://www.springframework.org/schema/task/spring-task-4.1.xsd >
1配置task操作的配置,实现间隔触发。
<bean id="myTask" class="cn.wnh.timerSask.MyTask2" />
<task:scheduled-tasks>
<task:scheduled ref="myTask" method="taskSelf"
fixed-rate="2000" />
</task:scheduled-tasks>
使用cron实现定时触发
<bean id="myTask" class="cn.wnh.timerSask.MyTask2" />
<task:scheduled-tasks>
<task:scheduled ref="myTask" method="taskSelf" cron="* * * * * ?" />
</task:scheduled-tasks>
可见,SpringTask实现起来更加简单。
来源:http://www.cnblogs.com/jjyz/p/6395781.html
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
深入浅出讲解Java集合之Map接口
![](https://img.aspxhome.com/file/2023/6/62816_0s.png)
基于idea Maven中的redis配置使用详解
Java实现天天酷跑小游戏完整代码(附源码)
![](https://img.aspxhome.com/file/2023/3/60833_0s.png)
LinkedBlockingQueue链式阻塞队列的使用和原理解析
![](https://img.aspxhome.com/file/2023/8/79938_0s.png)
浅谈java中对集合对象list的几种循环访问
Java 网络编程socket编程等详解
解析Android游戏中获取电话状态进行游戏暂停或继续的解决方法
C#操作RabbitMQ的完整实例
![](https://img.aspxhome.com/file/2023/2/111182_0s.png)
Java使用DateFormatter格式化日期时间的方法示例
![](https://img.aspxhome.com/file/2023/2/70322_0s.jpg)
Spring+SpringMVC+JDBC实现登录的示例(附源码)
![](https://img.aspxhome.com/file/2023/7/88587_0s.png)
教你怎么用Java回溯算法解数独
![](https://img.aspxhome.com/file/2023/4/111444_0s.png)
springBoot基于webSocket实现扫码登录
Android导航栏功能项的显示与屏蔽介绍
Java中泛型的示例详解
使用代理模式来进行C#设计模式开发的基础教程
![](https://img.aspxhome.com/file/2023/2/104272_0s.gif)
C#开发Windows服务实例之实现禁止QQ运行
![](https://img.aspxhome.com/file/2023/6/83296_0s.jpg)
C# 添加、修改和删除PDF书签的实例代码
![](https://img.aspxhome.com/file/2023/4/68614_0s.png)
C sharp (#) 数据类型获取方式
![](https://img.aspxhome.com/file/2023/9/126009_0s.jpg)
解决java.util.NoSuchElementException异常的问题
![](https://img.aspxhome.com/file/2023/3/93153_0s.jpg)