首页 > 移动平台 > 详细

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

时间:2017-05-23 16:23:46      阅读:443      评论:0      收藏:0      [点我收藏+]

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

 

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

原文:http://www.cnblogs.com/xsran/p/6894554.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!