一、spring 定时任务

spring 定时任务 ,最好使用quartz 实现。下面我以spring4为例

二、实战(默认)

1、pom配置

                <!-- spring time task -->
<dependency>
<groupId>org.quartz-scheduler</groupId>
<artifactId>quartz</artifactId>
<version>1.8.4</version>
</dependency>

2、bean 配置

<?xml version="1.0" encoding="gb2312"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx" xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd"
default-autowire="byName"> <bean name="myTaskJob" class="org.springframework.scheduling.quartz.JobDetailBean">
<property name="jobClass" value="com.yuanmeng.spring.TaskDemo" />
<property name="jobDataAsMap">
<map>
<entry key="timeout" value="36000" />
</map>
</property>
</bean> <bean id="myTaskTrigger" class="org.springframework.scheduling.quartz.SimpleTriggerBean">
<property name="jobDetail" ref="myTaskJob" />
<property name="startDelay" value="3000" />
<property name="repeatInterval" value="60000" /><!-- 每600秒调度一次 -->
</bean>
<!--启动任务-->
<bean class="org.springframework.scheduling.quartz.SchedulerFactoryBean">
<property name="triggers">
<list>
<ref bean="myTaskTrigger" />
</list>
</property>
</bean> </beans>

3、定时java类, 继承 QuartzJobBean

package com.yuanmeng.spring;

import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
import org.springframework.scheduling.quartz.QuartzJobBean; public class TaskDemo extends QuartzJobBean { private static int i = 0; @Override
protected void executeInternal(JobExecutionContext context) throws JobExecutionException { System.out.println("--------- begin ---------"); System.out.println(" i = " + i++); System.out.println("--------- end ---------");
} }

4、启动

package com.yuanmeng.spring;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.scheduling.quartz.JobDetailBean; public class Client { public static void main(String[] args) { ApplicationContext ac = new ClassPathXmlApplicationContext("classpath:spring/spring-timetask.xml"); org.springframework.scheduling.quartz.JobDetailBean bean = (JobDetailBean) ac.getBean("myTaskJob"); System.out.println(bean != null); }
}

5、看下结果

--------- begin ---------
i = 0
--------- end ---------
--------- begin ---------
i = 1
--------- end ---------
--------- begin ---------
i = 2
--------- end ---------
--------- begin ---------
i = 3
--------- end ---------
--------- begin ---------
i = 4
--------- end ---------
--------- begin ---------
i = 5
--------- end ---------
--------- begin ---------
i = 6
--------- end ---------
--------- begin ---------
i = 7
--------- end ---------
--------- begin ---------
i = 8
--------- end ---------
--------- begin ---------
i = 9
--------- end ---------
--------- begin ---------
i = 10
--------- end ---------
--------- begin ---------
i = 11
--------- end ---------
--------- begin ---------
i = 12
--------- end ---------

三、实战(不继承)

1、bean 配置

    <bean id="myTask" class="com.yuanmeng.spring.TaskDemo2" />

    <bean id="jobDetail"
class="org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean">
<!-- 指定定时任务执行类 -->
<property name="targetObject" ref="myTask" />
<!-- 指定定时任务执行方法 -->
<property name="targetMethod" value="doIt" />
<property name="concurrent" value="false" />
</bean> <bean id="simpleTrigger" class="org.springframework.scheduling.quartz.SimpleTriggerBean"> <property name="jobDetail" ref="jobDetail" /> <property name="startDelay" value="5000" /> <property name="repeatInterval" value="3000" />
</bean> <!-- 总调度用于启动Spring定时器 -->
<bean class="org.springframework.scheduling.quartz.SchedulerFactoryBean">
<property name="triggers">
<list>
<ref bean="simpleTrigger" />
</list>
</property>
</bean>

2、自定义任务类

package com.yuanmeng.spring;

public class TaskDemo2 {

    private static int i = 0;

    public void doIt() {

        System.out.println("---------doIt begin ---------");

        System.out.println(" i = " + i++);

        System.out.println("---------doIt end ---------");
} }

3、启动

ApplicationContext ac = new ClassPathXmlApplicationContext("classpath:spring/spring-timetask2.xml");

4、结果

---------doIt begin ---------
i = 0
---------doIt end ---------
---------doIt begin ---------
i = 1
---------doIt end ---------
---------doIt begin ---------
i = 2
---------doIt end ---------
---------doIt begin ---------
i = 3
---------doIt end ---------
---------doIt begin ---------
i = 4
---------doIt end ---------
---------doIt begin ---------

spring4 定时任务的更多相关文章

  1. Spring4.0编程式定时任务配置

    看过很多定时调度的配置,大多使用XML配置,觉得比较麻烦,也比较老套.这里介绍一种基于spring4.0注解编程式配置定时任务,简单清晰,使用方便.. 至于引入spring相关jar这里不多说,直接切 ...

  2. Spring定时任务,Spring4整合quartz2.2,quartz-scheduler定时任务

    Spring4整合quartz2.2,quartz-scheduler定时任务,Spring定时任务 >>>>>>>>>>>>& ...

  3. 基于Spring4+SpringMVC4+Mybatis3+Hibernate4+Junit4框架构建高性能企业级的部标GPS监控平台

    开发企业级的部标GPS监控平台,投入的开发力量很大,开发周期也很长,选择主流的开发语言以及成熟的开源技术框架来构建基础平台,是最恰当不过的事情,在设计之初就避免掉了技术选型的风险,避免以后在开发过程中 ...

  4. 定时任务-在spring中配置quartz

    使用的版本Spring4.04+Quartz2.2.3,关于jar包自行下载. 详细需要以下几个步骤来完成: 1.  定义要执行的Job类 2.  定义quartz的配置文件applicationCo ...

  5. Spring4定时器 cronTrigger和simpleTrigger实现方法

    spring4定时器 cronTrigger和simpleTrigger实现方法 Quartz 是个开源的作业调度框架,为在 Java 应用程序中进行作业调度提供了简单却强大的机制.Quartz 允许 ...

  6. spring计划任务,springMvc计划任务,Spring@Scheduled,spring定时任务

    spring计划任务,springMvc计划任务,Spring@Scheduled,spring定时任务 >>>>>>>>>>>> ...

  7. 基于Spring4的定时任务管理

    在项目中,有时会遇到定时任务的处理,下面介绍一下我的做法. 此做法基于Spring4,Spring框架搭建成功,另需引入quartz.jar,pom.xml文件中加入 <dependency&g ...

  8. Spring4整合quartz2.2.3,quartz动态任务

    Spring4整合quartz2.2.3,quartz动态任务 >>>>>>>>>>>>>>>>> ...

  9. 项目ITP(六) spring4.0 整合 Quartz 实现动态任务调度

    前言 系列文章:[传送门] 项目需求: http://www.cnblogs.com/Alandre/p/3733249.html 上一博客写的是基本调度,后来这只能用于,像每天定个时间 进行数据库备 ...

随机推荐

  1. 如何自定义一个优雅的ContentProvider

    最近在code review的时候发现很多人的provider定义的不是很好,写的很粗糙 以至于代码健壮性不够好,可读性也不强 但是你既然写了content provider 就是要给别人调用的,如果 ...

  2. php文件读写锁

    $file = fopen("test.txt", $fileOpenMode); flock($file, $lockMode) or die("Can't lock& ...

  3. ylb:子查询(嵌套子查询)和子查询(相关子查询)

    ylbtech-SQL Server:SQL Server-子查询(嵌套子查询)和子查询(相关子查询) SQL Server 子查询(嵌套子查询)和子查询(相关子查询). 1,ylb:1,子查询(嵌套 ...

  4. Android 数独游戏 记录

    Android图形编程基本概念 颜色对象 Color 类 int color = Color.bule    //蓝色 int color = Color.argb(255,255,255,255); ...

  5. 使用nodejs中httpProxy代理时候出现404异常

    在公司中使用nodejs构建代理服务器实现前后台分离,代码不能拿出来,然后出现httpProxy代理资源的时候老是出现404.明明被代理的接口是存在的.代码大概如下: var http = requi ...

  6. Redis批量导入数据

    首先准备数据文件 格式为 SET Key0 Value0 SET Key1 Value1 ... SET KeyN ValueN 利用shell转换数据 #!/bin/bash while read ...

  7. Numpy矩阵取列向量

    >>> A=matrix("1 2;3 4") >>> A matrix([[1, 2], [3, 4]]) >>> A[:, ...

  8. jquerymobile,手机端click无效

    1.直接把<script>放到html代码后面,不要放到@section里面. 2.使用代理.如下所示: <script type="text/javascript&quo ...

  9. dom select选单

    <!doctype html> <html> <head> <meta charset="utf-8"> <title> ...

  10. dom 动态生产表格

    <!doctype html> <html> <head> <meta charset="utf-8"> <title> ...