spring基于配置applicationContext.xml实现定时任务

spring3.x可以通过<task>标签轻易的定义定时任务,而且不需要依赖第三方jar包如quartz等,这个是spring自己的实现,但不支持集群。

在Spring3.0中引用了新的命名空间-task:
task:scheduler 用于定义一个ThreadPoolTaskScheduler,并可以指定线程池的大小,
即pool-size.所有任务队列都将会在指定大小的线程池中运行:  

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:mvc="http://www.springframework.org/schema/mvc"
    xmlns:aop="http://www.springframework.org/schema/aop"
    xmlns:task="http://www.springframework.org/schema/task"
    xsi:schemaLocation="http://www.springframework.org/schema/beans 
                           http://www.springframework.org/schema/beans/spring-beans.xsd
                           http://www.springframework.org/schema/context 
                           http://www.springframework.org/schema/context/spring-context.xsd
                           http://www.springframework.org/schema/mvc
                              http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd
                              http://www.springframework.org/schema/aop
                            http://www.springframework.org/schema/aop/spring-aop-3.2.xsd
                            http://www.springframework.org/schema/task    
                           http://www.springframework.org/schema/task/spring-task-3.2.xsd"
    default-autowire="byName" default-lazy-init="true">
    
    <!-- 配置映射任务执行的类 -->
       <bean id="myTask" class="com.xsran.core.task.MyTask" />
       <task:scheduled-tasks scheduler="myScheduler">  
           <!-- 每十分钟执行一次 passStudyTask:任务类中执行的方法      -->
        <task:scheduled ref="myTask" method="passStudyTask"cron="0 0/10 * * * ?"/>  
    </task:scheduled-tasks>  
    <task:scheduler id="myScheduler" pool-size="10"/>    
</beans>

java代码:

package com.xsran.core.task;

import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import com.xsran.core.mybase.Constants;
import com.xsran.core.myutil.Collections3;
import com.xsran.core.myutil.LogUtils;
import com.xsran.modules.task.model.QuartzManage;
import com.xsran.modules.task.service.QuartzManageService;

public class MyTask {
    @Autowired
    private QuartzManageService quartzManageService;    
    

    /** 
     * 定时传送 
     */
    public void passStudyTask() {
        
//      Map<String, String> map = System.getenv();
        try {
            QuartzManage quartzManage = quartzManageService.findQuartzByTaskName("TaskName");//执行方法获取数据
            if (quartzManage != null ) {
                System.out.println(quartzManage);
                
            }            
        } catch (Exception e) {
            // TODO: handle exception
        }
    }
}

Spring3.0同样也使用cron表达式。与Quartz不同的是,Spring3.0不支持年,而Quartz支持年。 

cron表达式:-是用空格分开的时间字段,不使用年。
*(秒0-59)   
*(分钟0-59) 
*(小时0-23) 
*(日期1-31) 
*(月份1-12或是JAN-DEC) 
*(星期1-7或是SUN-SAT) 

示例:
*/5  * * * * 6-7  :: 每个周6到周日,每隔5秒钟执行一次。

*/1 * * 7-9 1-2 1-7 :: 1月到2月中的7号到9号,且必须要满足周一到周日,每隔1秒钟执行一次。

*/1 * * 7-9 1,5 1-7  :: 注意里面的,(逗号),只有1月和5月的7到9号,且必须要满足周一到周日,每一秒钟执行一次。

*/1 17-59 * 7-9 1,5 1-7 :: 只解释17-59,是指从第17分钟到第59分钟,在指定的时间内,每一秒种执行一次

* 17-59 * 7-9 1,5 1-7  :: 此代码的功能与上面完全相同。如果不写秒即为每一秒执行一次。

  59 19-23 * 7-9 1,5 1-7  :: 19分-23分的每59秒钟时只执行一次。

  59 19,26 * 7-9 1,5 1-7  :: 注意里面的,(逗号),是指只有19分或是26分的56秒钟时执行一次。

  * * 16-23 7-9 1,5 1-7  :: 定义每天的16点到23点每一秒钟执行一次。

  59 59 23 * * 1-5  :: 定义每周1到周5,晚上23:59:59秒只执行一次。
这个相当用有。可以工作时间每天给用户发邮件

注:1、本人blog旨在为自己做代码笔记,同时供大家作参考;

  2、本博文字内容拷自blog:http://xls9577087.iteye.com/blog/2123425

 

转载于:https://www.cnblogs.com/xsran/p/6894554.html

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值