Spring Task 定时器

尝试着用quartz写个定时器,结果失败了,项目一直在报错。

于是用spring task写了一个小例子。很简单的实现了每分钟都输出时间的功能。

1:POJO

这是个很简单的类,输出时间。 @component  我们就不需要在配置bean了,@Scheduled是定义了调度,就是定好时间后 需要做的工作。cron是时间

@Scheduled(cron = "0 * 20 * * ?")
分别是 0 * 20 秒 分钟 小时 * * ? 就说日期了, 天 月 年。
我这里写的0 * 20 * * ? 就是说每天的晚八点 20时 ,* 代表每一个 放在分钟就说每分钟。整体来说就是每晚八点 每分钟都执行下面的方法。
package com.zhao.util;

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

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

/**
 * 
 * @author: zhao
 * @time: 2016年5月26日
 * 
 * @description:定时器操作
 */
@Component("job")
public class Job {

    /*
     * 要调度的具体任务
     */
    @Scheduled(cron = "0 * 20 * * ?")
    protected void doJob() {
        Date date = new Date();
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String dateStr = format.format(date);
        System.out.println(dateStr);
    }
}

输出

  

2:spring_bean.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:context="http://www.springframework.org/schema/context"
    xmlns:aop="http://www.springframework.org/schema/aop" 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.1.xsd
         http://www.springframework.org/schema/context
         http://www.springframework.org/schema/context/spring-context-3.1.xsd
         http://www.springframework.org/schema/aop
         http://www.springframework.org/schema/aop/spring-aop-3.1.xsd
         http://www.springframework.org/schema/tx
         http://www.springframework.org/schema/tx/spring-tx-3.1.xsd
         http://www.springframework.org/schema/task
         http://www.springframework.org/schema/task/spring-task-3.1.xsd"
    default-lazy-init="false">
      <context:annotation-config />    
    <context:component-scan base-package="com.zhao.util" />  
 
    <task:annotation-driven scheduler="qbScheduler" mode="proxy"/>  
    <task:scheduler id="qbScheduler" pool-size="10"/>
</beans>

然后部署运行就好了 ,就是这么简单。

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触发 

原文地址:https://www.cnblogs.com/zhao307/p/5532523.html