一、计划任务

需要定时执行一些计划(定时更新等),这样的计划称之为计划任务

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任务调度的更多相关文章

  1. spring中的任务调度Quartz

    Spring 整合 Quartz 任务调度 主要有两种方式. Quartz的官网:http://www.quartz-scheduler.org/ 这两种只是一些配置文件简单配置就OK了,但是根本无法 ...

  2. spring timetask 定时任务调度

    作者:Garry1115 定时任务调度即在设置的特定时间执行特定的任务,不需要人工干预. spring timertask spring 自身所带定时任务类,不需要引入第三方jar包,使用方式如下: ...

  3. Spring Boot + Spring Cloud 实现权限管理系统 后端篇(二十五):Spring Security 版本

    在线演示 演示地址:http://139.196.87.48:9002/kitty 用户名:admin 密码:admin 技术背景 到目前为止,我们使用的权限认证框架是 Shiro,虽然 Shiro ...

  4. 手动创建Spring项目 Spring framework

    之前学习框架一直是看的视频教程,并且在都配套有项目源码,跟着视频敲代码总是很简单,现在想深入了解,自己从官网下载文件手动搭建,就遇到了很多问题记载如下. 首先熟悉一下spring的官方网站:http: ...

  5. 【Spring Boot&&Spring Cloud系列】Spring Boot配置文件

    很多的参数可以配置在application.properties或application.yml文件中 一.BANNER banner.charset=UTF-8 # Banner file enco ...

  6. 重新学习Spring一--Spring在web项目中的启动过程

    1 Spring 在web项目中的启动过程 Spring简介 Spring 最简单的功能就是创建对象和管理这些对象间的依赖关系,实现高内聚.低耦合.(高内聚:相关性很强的代码组成,既单一责任原则:低耦 ...

  7. Spring 框架介绍 [Spring 优点][Spring 应用领域][体系结构][目录结构][基础 jar 包]

    您的"关注"和"点赞",是信任,是认可,是支持,是动力...... 如意见相佐,可留言. 本人必将竭尽全力试图做到准确和全面,终其一生进行修改补充更新. 目录 ...

  8. 深入分析Spring 与 Spring MVC容器

    1 Spring MVC WEB配置 Spring Framework本身没有Web功能,Spring MVC使用WebApplicationContext类扩展ApplicationContext, ...

  9. spring/spring boot/spring cloud开发总结

    背景        针对RPC远程调用,都在使用dubbo.dubbox等,我们也是如此.由于社区暂停维护.应对未来发展,我们准备尝试新技术(或许这时候也不算什么新技术了吧),选择使用了spring ...

随机推荐

  1. (转)linux文件读写的流程

    转自http://hi.baidu.com/_kouu/item/4e9db87580328244ef1e53d0 在<linux内核虚拟文件系统浅析>这篇文章中,我们看到文件是如何被打开 ...

  2. 快乐的JS正则表达式(开篇)

    我不喜欢一开始就去讨论某某有多强大,因为我觉得那样没意思,首先我们的知道它是干什么,对我们有啥用,再去讨论它的强大之处也不迟.那和往常一样我们先来看几个例子. var arr = [1,4,2,5,2 ...

  3. 4.3.3版本之引擎bug

    bug描述: IOS设备上,当使用WWW www = WWW.LoadFromCacheOrDownload(url, verNum); 下载资源时,第一次下载某个资源,www.assetBundle ...

  4. Jellycons – iOS 8 图标下载(PNG, SKETCH)

    Jellycons 这套由 LoveUI.co 设计图标包括30款扁平化,圆滑,丰富多彩的 iOS 8 应用程序图标,可以用于于个人和商业项目的使用.另外,PNG 格式包含11种尺寸(1024px, ...

  5. DomFlags - 给 DOM 添加书签,方便调试

    DomFlags 所以一款 Chrome 浏览器扩展程序提,供了一种新的方式与浏览器开发者工具互动.DomFlags 让你可以给 DOM 元素创建快捷键,就像用于导航 DOM 树的书签.它们可以帮助您 ...

  6. Python函数解析

    对于Python的函数,我们需要记住的是: 1. 函数的默认返回值是None. 2. python是一个自上而下逐行解释并执行的语言.因此,函数的定义必须在函数被调用之前.同名的函数,后定义的会覆盖前 ...

  7. js代码中的闭包

    作为一个后台开发人员了解前端非常重要,尤其是深处学校实验室做项目时前端把写好的代码直接给你,然后你在修改的时候.我经常做的就是修改前端的代码的HTML和后台交互的部分以及js的ajax部分,之后修改之 ...

  8. Qt Style Sheet实践(四):行文本编辑框QLineEdit及自动补全

    导读 行文本输入框在用于界面的文本输入,在WEB登录表单中应用广泛.一般行文本编辑框可定制性较高,既可以当作密码输入框,又可以作为文本过滤器.QLineEdit本身使用方法也很简单,无需过多的设置就能 ...

  9. SQL Server视图复习

    视图的好处: 第一点:使用视图,可以定制用户数据,聚焦特定的数据. 第一点:使用视图,可以定制用户数据,聚焦特定的数据. 在实际过程中,公司有不同角色的工作人员,我们以销售公司为例的话,采购人员,可以 ...

  10. yyyy/M/d h:m:s 转换成 yyyy-MM-dd hh:mm:ss

    var arrTime = (dtime).replace("/", "-").replace("/", "-"); v ...