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 ...
随机推荐
- 设置函数环境——setfenv
当我们在全局环境中定义变量时经常会有命名冲突,尤其是在使用一些库的时候,变量声明可能会发生覆盖,这时候就需要一个非全局的环境来解决这问题.setfenv函数可以满足我们的需求. setfenv(f, ...
- 爬虫技术 -- 基础学习(五)解决页面编码识别(附c#代码)
实现从Web网页提取文本之前,首先要识别网页的编码,有时候还需要进一步识别网页所使用的语言.因为同一种编码可能对应多种语言,例如UTF-8编码可能对应英文或中文等语言. 识别编码整体流程如下: (1) ...
- Understanding G1 GC Logs--转载
原文地址:https://blogs.oracle.com/poonam/entry/understanding_g1_gc_logs Understanding G1 GC Logs By Poon ...
- ASP.NET Core学习零散记录
赶着潮流听着歌,学着.net玩着Core 竹子学Core,目前主要看老A(http://www.cnblogs.com/artech/)和tom大叔的博客(http://www.cnblogs.com ...
- Win10安装.net framework 4.0失败提示已是操作系统一部分如何解决
有位用户因为工作需求,所以想在win10系统电脑中安装microsoft .net framework 4.0.可是在安装过程中却失败了,还遇到提示"Microsoft.net framew ...
- 关于DOM树的常见增删操作
//具体关于DOM的内容可参考我的另外一篇文章"文本对象模型(Document Object Model)". 案例要点: 1.创建并增加元素节点 2.判断是否存在 ...
- JS+JQ手风琴效果
最新在学习JS写一些实用的小玩意——手风琴 CSS样式: <style type="text/css"> * { margin: 0px; border: 0px; p ...
- 【C#进阶系列】09 关于参数的故事
可选参数和命名参数 不多说,上代码,自然懂 class Program { static void Main(string[] args) { var troy = new Troy(); troy. ...
- 【jQuery基础学习】11 jQuery性能简单优化
关于性能优化 合适的选择器 $("#id")会直接调用底层方法,所以这是最快的.如果这样不能直接找到,也可以用find方法继续查找 $("p")标签选择器也是直 ...
- C语言回滚(三)-指针
#include <stdio.h>#include <stdlib.h> //& 地址运算符 //* 间接运算符 // *的作用 当*后面跟一个指针名或地址的时候, ...