Spring(十)Spring任务调度
一、计划任务
需要定时执行一些计划(定时更新等),这样的计划称之为计划任务
Spring抽象封装了Java提供的Timer与TimerTask类
也可以使用拥有更多任务计划功能的Quartz
二、TimerTask
2.1、继承TimerTask类重写run方法


实现类
package com.pb.task.timertask; import java.util.Iterator;
import java.util.List;
import java.util.TimerTask; public class BatchUpdate extends TimerTask {
//存放SQL
private List commons;
@Override
public void run() {
//输出语句
for(Iterator it=commons.iterator();it.hasNext();){
System.out.println(it.next());
}
System.out.println("timertask批量更新完毕");
}
public void setCommons(List commons) {
this.commons = commons;
} }
配置文件:
<?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:p="http://www.springframework.org/schema/p"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd"> <bean id="batch_update" class="com.pb.task.timertask.BatchUpdate">
<property name="commons">
<list>
<value>update personn set onduty="出勤" where date=${ontime}</value>
<value>update personn set onduty="缺勤" where date=${miss}</value>
<value>update personn set onduty="迟到" where date=${late}</value>
<value>update personn set onduty="早退" where date=${early}</value>
</list>
</property>
</bean> <!-- 配置任务调度的类 -->
<bean id="update_schuleTask" class="org.springframework.scheduling.timer.ScheduledTimerTask">
<property name="timerTask" ref="batch_update"/>
<!--启动任务后多久开始执行 -->
<property name="delay">
<value>1000</value>
</property>
<!--第一次记动后后多久执行一次 2秒重复一次-->
<property name="period">
<value>2000</value>
</property>
</bean>
<!--启动任务 -->
<bean id="timerFactory" class="org.springframework.scheduling.timer.TimerFactoryBean">
<!--任务调度类 -->
<property name="scheduledTimerTasks">
<list>
<ref local="update_schuleTask"/>
</list>
</property>
</bean>
</beans>
测试类只需要调用
ApplicationContext context=new ClassPathXmlApplicationContext("applicationContext.xml");
2.2、使用Spring提供的MethodInvokingTimerTaskFactoryBean定义计划任务


package com.pb.task.timertask; import java.util.Iterator;
import java.util.List;
import java.util.TimerTask; public class MethodInvokingBatchUpdate {
//存放SQL
private List commons; public void run() {
//输出语句
for(Iterator it=commons.iterator();it.hasNext();){
System.out.println(it.next());
}
System.out.println("MethodInvoking批量更新完毕");
}
public void setCommons(List commons) {
this.commons = commons;
} }
配置文件
<?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:p="http://www.springframework.org/schema/p"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd"> <bean id="batch_update" class="com.pb.task.timertask.MethodInvokingBatchUpdate">
<property name="commons">
<list>
<value>update personn set onduty="出勤" where date=${ontime}</value>
<value>update personn set onduty="缺勤" where date=${miss}</value>
<value>update personn set onduty="迟到" where date=${late}</value>
<value>update personn set onduty="早退" where date=${early}</value>
</list>
</property>
</bean>
<!--配置方法的调用类 -->
<bean id="methodInvoking" class="org.springframework.scheduling.timer.MethodInvokingTimerTaskFactoryBean">
<property name="targetObject" ref="batch_update"></property>
<property name="targetMethod" value="run"/> </bean> <!-- 配置任务调度的类 -->
<bean id="update_schuleTask" class="org.springframework.scheduling.timer.ScheduledTimerTask">
<property name="timerTask" ref="methodInvoking"/>
<!--启动任务后多久开始执行 -->
<property name="delay">
<value>1000</value>
</property>
<!--第一次记动后后多久执行一次 2秒重复一次-->
<property name="period">
<value>2000</value>
</property>
</bean>
<!--启动任务 -->
<bean id="timerFactory" class="org.springframework.scheduling.timer.TimerFactoryBean">
<!--任务调度类 -->
<property name="scheduledTimerTasks">
<list>
<ref local="update_schuleTask"/>
</list>
</property>
</bean>
</beans>
三、QuartzJobBean实现




传统QuartzJob
需要jta.jar 和quartz-all-1.6.0.jar2个jar包
package com.pb.quartz.job; import java.util.Date; import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
import org.springframework.scheduling.quartz.QuartzJobBean; /**
* 传统的使用Quartz
*
*/
public class QuartzUpdate extends QuartzJobBean {
private String command; @Override
protected void executeInternal(JobExecutionContext context)
throws JobExecutionException {
System.out.println(new Date()+":"+"传统Quartz任务被高度");
for (int i = 1; i <= 10; i++) {
System.out.println("命令:"+command+"第"+i+"次被谳用"); }
System.out.println(new Date()+"传统Quartz调度完成");
}
public void setCommand(String command) {
this.command = command;
} }
配置文件
<?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:p="http://www.springframework.org/schema/p"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.1.xsd"> <!--配置传统方式创建Quartz -->
<bean id="t_quartz" class="org.springframework.scheduling.quartz.JobDetailBean">
<!--配置哪个类 -->
<property name="jobClass">
<!--关联 -->
<value>com.pb.quartz.job.QuartzUpdate</value>
</property> <!-- 赋值-->
<property name="jobDataAsMap">
<map>
<entry key="command">
<value>更新</value>
</entry>
</map>
</property>
</bean> <!--配置触发器简单触发器 -->
<bean id="simpleTrigger" class="org.springframework.scheduling.quartz.SimpleTriggerBean">
<property name="jobDetail" ref="t_quartz"/>
<!--启动时间 -->
<property name="startDelay">
<value>1000</value>
</property>
<!--间隔 -->
<property name="repeatInterval">
<value>2000</value>
</property>
</bean>
<!--启动任务 -->
<bean id="quartzFactory" class="org.springframework.scheduling.quartz.SchedulerFactoryBean">
<property name="triggers">
<list>
<ref local="simpleTrigger"/>
</list>
</property>
</bean>
</beans>
使用MethodInvoking方式
package com.pb.quartz.methodinvoking.update;
public class UpdateQuartzJob {
private String command;
public void show(){
System.out.println("MethodInvoking方式调度开始"+command);
for (int i = 1; i <=10; i++) {
System.out.println("命令:"+command+"第"+i+"次调用");
}
System.out.println("MethodInvoking方式调度结束"+command);
}
public String getCommand() {
return command;
}
public void setCommand(String command) {
this.command = command;
}
}
配置文件
<?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:p="http://www.springframework.org/schema/p"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd">
<!-- 执行的Bean -->
<bean id="updateQuartzJob" class="com.pb.quartz.methodinvoking.update.UpdateQuartzJob">
<property name="command">
<value>Spring新型更新或者插入</value>
</property>
</bean>
<!-- 新的通过方式调用的 -->
<bean id="methodInvoking" class=" org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean">
<!--哪个类要被调用 -->
<property name="targetObject" ref="updateQuartzJob"/>
<!--哪个方法要执行 -->
<property name="targetMethod" value="show"/>
</bean>
<!--配置触发器定时触发器 -->
<bean id="cronTrigger" class="org.springframework.scheduling.quartz.CronTriggerBean">
<property name="jobDetail" ref="methodInvoking"/>
<!--表达式 -->
<property name="cronExpression">
<!-- 每天23:17:01秒调用 -->
<value>02 17 23 * * ?</value>
</property>
</bean> <!--启动任 -->
<bean id="quartzFactory" class="org.springframework.scheduling.quartz.SchedulerFactoryBean">
<!--触发器名字 -->
<property name="triggers">
<list>
<ref local="cronTrigger" />
</list>
</property>
</bean> </beans>
Spring(十)Spring任务调度的更多相关文章
- spring中的任务调度Quartz
Spring 整合 Quartz 任务调度 主要有两种方式. Quartz的官网:http://www.quartz-scheduler.org/ 这两种只是一些配置文件简单配置就OK了,但是根本无法 ...
- spring timetask 定时任务调度
作者:Garry1115 定时任务调度即在设置的特定时间执行特定的任务,不需要人工干预. spring timertask spring 自身所带定时任务类,不需要引入第三方jar包,使用方式如下: ...
- Spring Boot + Spring Cloud 实现权限管理系统 后端篇(二十五):Spring Security 版本
在线演示 演示地址:http://139.196.87.48:9002/kitty 用户名:admin 密码:admin 技术背景 到目前为止,我们使用的权限认证框架是 Shiro,虽然 Shiro ...
- 手动创建Spring项目 Spring framework
之前学习框架一直是看的视频教程,并且在都配套有项目源码,跟着视频敲代码总是很简单,现在想深入了解,自己从官网下载文件手动搭建,就遇到了很多问题记载如下. 首先熟悉一下spring的官方网站:http: ...
- 【Spring Boot&&Spring Cloud系列】Spring Boot配置文件
很多的参数可以配置在application.properties或application.yml文件中 一.BANNER banner.charset=UTF-8 # Banner file enco ...
- 重新学习Spring一--Spring在web项目中的启动过程
1 Spring 在web项目中的启动过程 Spring简介 Spring 最简单的功能就是创建对象和管理这些对象间的依赖关系,实现高内聚.低耦合.(高内聚:相关性很强的代码组成,既单一责任原则:低耦 ...
- Spring 框架介绍 [Spring 优点][Spring 应用领域][体系结构][目录结构][基础 jar 包]
您的"关注"和"点赞",是信任,是认可,是支持,是动力...... 如意见相佐,可留言. 本人必将竭尽全力试图做到准确和全面,终其一生进行修改补充更新. 目录 ...
- 深入分析Spring 与 Spring MVC容器
1 Spring MVC WEB配置 Spring Framework本身没有Web功能,Spring MVC使用WebApplicationContext类扩展ApplicationContext, ...
- spring/spring boot/spring cloud开发总结
背景 针对RPC远程调用,都在使用dubbo.dubbox等,我们也是如此.由于社区暂停维护.应对未来发展,我们准备尝试新技术(或许这时候也不算什么新技术了吧),选择使用了spring ...
随机推荐
- Oracle实例和服务知识点
shutdown是对实例而言 service是启动的,根本不代表instance就是启动的. 启动数据库基本可分为三个过程: 1,nomount(即只启动instance,而不加载数据库) 2,mo ...
- Sylius – 100% 免费和开源的电子商务解决方案
Sylius 项目提供了一个完整的电子商务解决方案.您将学习如何掌握它,帮助你在下一个项目中能够更快速的开发.Sylius 提供了一个完整的在线商店演示:demo.sylius.com. 您可能感兴趣 ...
- vim编辑器的基本使用
VIM的操作模式 Command Mode 命令模式 Insert Mode 输入模式 Last Line Mode 底行模式 vim abc 如果文件存在 ...
- Java对象的复制
Java中对象的赋值分为浅拷贝和深拷贝 1.对象浅拷贝 public class CloneTest{ static class Emp{ String name; int age; Date h ...
- 五分钟,运用cocoaui库,搭建主流iOS app中我的界面
本项目基于天天团购项目,在上一篇中有说到! 首先介绍一些cocoaui,是国内的一名程序员做的开源的开源系统,目的是为了简化ios布局!官网地址:www.cocoaui.com,github地址:ht ...
- 【第三课】ANR和OOM——贪快和贪多的后果(下)
Out of Mana,法力耗尽. 内存就像法力,耗尽了就什么都不能做了.有时候一个应用程序占用了太大的内存,超过了Android系统为你规定的限制,那么系统就会干掉你,以保证其他app有足够的内存. ...
- 搜索 --- 数独求解 POJ 2676 Sudoku
Sudoku Problem's Link: http://poj.org/problem?id=2676 Mean: 略 analyse: 记录所有空位置,判断当前空位置是否可以填某个数,然后直 ...
- Windows 8.1 去掉库的方法
Windows 8.1[这台电脑], 里面又多了[文件夹]分类,真是各种不习惯 删除方法: 打开注册表, 找到 [HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Wind ...
- zTree的使用
一.节点模糊搜索功能:搜索成功后,自动高亮显示并定位.展开搜索到的节点. 二.节点异步加载:1.点击展开时加载数据:2.选中节点时加载数据. 前台代码如下: <script type=" ...
- C#如何获取CPU处理器核心数量
有几条不同的处理器信息,您可以获得有关的信息:物理处理器数量.核心数量和逻辑处理器数量,这些可以不同.两颗双核超线程(启用)处理器的机器情况下有:2个物理处理器.4个核心和8个逻辑处理器. 逻辑处理器 ...