一、计划任务

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

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. 将PDM文件导出成CHM帮助文件

    实际开发中常常将维护数据库字段用 powerdesigner 维护起来,而实际要查阅 数据库用了什么字段是,常常又不方便去打开PDM 文件去找. 下面分享一个小工具,效果看下图: 有这个小工具, PD ...

  2. LoRaWAN移植笔记(一)__RTC闹钟链表的实现

    近日在阅读semtech的Lora-net/LoRaMac-node.此代码是LoRaWAN MAC层的node段的代码. 此代码中构建了一个定时器链表,此链表构建得非常的巧妙,现在和大家分享. 此定 ...

  3. 【转载】uclibc和glibc的差别

    转载自:http://blog.163.com/huangnan0727@126/blog/static/30626184201042022011225/ CC的标准库,就是glibc这个库,里面有G ...

  4. LeetCode——Find Median from Data Stream

    Median is the middle value in an ordered integer list. If the size of the list is even, there is no ...

  5. node log4js包

    http://blog.csdn.net/heiantianshi1/article/details/43984601

  6. 3 Servlet监听器

    作者:禅楼望月(http://www.cnblogs.com/yaoyinglong) 1. ServletConfig和ServletContext 1.1 ServletConfig和Servle ...

  7. [Bootstrap]7天深入Bootstrap(5)JavaScript插件

    在bs3.X中,提供了12种JavaScript插件,分别是:动画过渡(Transition).模态弹窗(Modal).下拉菜单(Dropdown).滚动侦测(Scrollspy).选项卡(Tab). ...

  8. 2015 Multi-University Training Contest 1 - 1001 OO’s Sequence

    OO’s Sequence Problem's Link: http://acm.hdu.edu.cn/showproblem.php?pid=5288 Mean: 给定一个数列,让你求所有区间上满足 ...

  9. 统一者管理员指南(Unifier Administration Guide)中文

    统一者管理员指南 Unifier Administration Guide 2014年6月 发布 2014年11月翻译 10.0版本 10.0.1译 关于译者 翻译者QQ:77811970 Email ...

  10. 重构第20天 提取子类(Extact SubClass)

    理解:提取子类就是把基类中,不是所有子类或者只有少数子类用到的方法,提取出来,调整到子类中去. 详解:下面的代码中我们用到一个单一的类Registration,来处理学生选课信息. public cl ...