一、计划任务

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

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. JS算法总结

    1.选择排序: var arr = [3,6,7,2,6,4,1,6,8,24,12,53]; function sort(arr){ // 当数组的长度小于1的时候结束递归 if(arr.lengt ...

  2. Lua和C语言的交互——C API

    Lua可作为扩展性语言(Lua可以作为程序库用来扩展应用的功能),同时也是个可扩展的语言(Lua程序中可以注册由其他语言实现的函数). C和Lua交互的部分称为C API.C API是一个C代码与Lu ...

  3. HTML5探索一(那些新增的标签和属性)

    tml5相比html4,添加了部分语义化的标签和属性,现在我们就从这些标签和属性开始,学习html5吧. 首先,认识下HTML5新的文档类型: <!DOCTYPE html> 那些新标签 ...

  4. Communication - 03.RILC

    RIL层的作用大体上就是将上层的命令转换成相应的AT指令,控制modem工作.生产modem的厂家有很多:Qualcomm, STE, Infineon... 不同的厂家都有各自的特点,当然也会有各自 ...

  5. 解决两台centos虚拟机Telnet服务无法联机的问题

    关闭防火墙 [root@localhost ~]# service iptables stopiptables: Flushing firewall rules:                    ...

  6. Appium移动自动化测试(二)--安装Android开发环境

    继续Appium环境的搭建. 第二节  安装Android开发环境 如果你的环境是MAC那么可以直接跳过这一节.就像我们在用Selenium进行web自动化测试的时候一样,我们需要一个浏览器来执行测试 ...

  7. JS&CSS文件请求合并及压缩处理研究(二)

    上篇交待了一些理论方面的东西,并给出了另外一种解决方案的处理流程.本篇将根据该处理流程,开始代码方面的编写工作. 1,打开VS,新建ASP.NET MVC Web项目,项目类型选择空.名称为 Mcmu ...

  8. 开源一个基于nio的java网络程序

    因为最近要从公司离职,害怕用nio写的网络程序没有人能看懂(或许是因为写的不好吧),就调整成了mina(这样大家接触起来非常方便,即使没有socket基础,用起来也不难),所以之前基于nio写的网络程 ...

  9. 对象Transform,对属性赋值

    private void ContructRequest(Dictionary<string, string> dictionary, CustomerSearchRequest requ ...

  10. sprint2 项目部署+展示

    项目展示网址: http://160q49b998.51mypc.cn/ (注:所有用户密码都为123456,校内断网时访问不了)