您的位置:首页 > 编程语言 > Java开发

spring定时任务(1):使用component注解实现静态定时任务

2017-03-16 13:31 561 查看
环境:myeclipse10.7+spring 3.1

一、在服务器端编写任务类

package com.conbao.component.task.controller;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;

import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

/**
* 定时任务作业类
*
* @author code陈
*
*/
@Component("task")
public class Task {
DateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

@Scheduled(cron = "0 10-30 9 * * ?")
public void pushQuestionnaire() {

System.out.println("定时任务1,自动执行:" + format.format(new Date()));
}

@Scheduled(cron = "0 10-30 9 * * ?")
public void pushQuestionnaire2() {

System.out.println("定时任务2,自动执行:" + format.format(new Date()));
}
}


二、在服务器端配置xml
<?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:aop="http://www.springframework.org/schema/aop"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:task="http://www.springframework.org/schema/task"
xsi:schemaLocation=" http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/jdbc/spring-jdbc-3.0.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task-3.0.xsd"
default-lazy-init="false">

<context:annotation-config />
<!—spring扫描注解的配置 :能扫描到task所在的包就行,因为我的程序里还有其他注解,所以base-package是较外层的package  -->
<context:component-scan base-package="com.conbao.component" />

<!—开启这个配置 -->
<task:annotation-driven scheduler="qbScheduler" mode="proxy"/>
<task:scheduler id="qbScheduler" pool-size="10"/>
</beans>


三、最后的运行效果



<解释>

(1)这是一个通过spring的@component和@Scheduled实现的定时任务,由于在代码中已经写死了定时任务的执行时间(cron="* * * * * *"),所以可以叫做静态定时任务,但是在很多时候,我们本身也不确定一个任务执行的时间,比如是在前台通过一个日历,选择执行一个任务的时间,那么我们就需要动态改变cron,这就是动态定时任务,具体实现,下一节再整理。

(2)其实也可以通过spring的其他配置方式或者其他途径比如java自带的timer类或者Quartz来实现定时任务,大家可以去网上查看,本人是由于项目的框架是spring,所以使用注解比较方便。

(3)cron的设置规则,从网上摘来,可根据自己需求设计。

字段   允许值   允许的特殊字符

秒    0-59    , - * /

分    0-59    , - * /

小时    0-23    , - * /

日期    1-31    , - * ? / L W C

月份    1-12 或者 JAN-DEC    , - * /

星期    1-7 或者 SUN-SAT    , - * ? / L C #

年(可选)    留空, 1970-2099    , - * / 

- 区间  

* 通配符  

? 你不想设置那个字段

下面只例出几个式子 

CRON表达式    含义 

"0 0 12 * * ?"    每天中午十二点触发 

"0 15 10 ? * *"    每天早上10:15触发 

"0 15 10 * * ?"    每天早上10:15触发 

"0 15 10 * * ? *"    每天早上10:15触发 

"0 15 10 * * ? 2005"    2005年的每天早上10:15触发 

"0 * 14 * * ?"    每天从下午2点开始到2点59分每分钟一次触发 

"0 0/5 14 * * ?"    每天从下午2点开始到2:55分结束每5分钟一次触发 

"0 0/5 14,18 * * ?"    每天的下午2点至2:55和6点至6点55分两个时间段内每5分钟一次触发 

"0 0-5 14 * * ?"    每天14:00至14:05每分钟一次触发 

"0 10,44 14 ? 3 WED"    三月的每周三的14:10和14:44触发 

"0 15 10 ? * MON-FRI"    每个周一、周二、周三、周四、周五的10:15触发
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: