Spring的@Scheduler注解实现定时任务

1、使用spring的 scheduled使用注解的方式
这种方法的好处是:使用方便,配置少,提高开发效率;
缺点是:如果使用服务器集群部署方式的时候,其自身无法解决定时任务重复执行的问题。
2、首先在你的applicationContext.xml中加入以下配置:

<task:executor id="executor" pool-size="5" />
<task:scheduler id="scheduler" pool-size="10" />
<task:annotation-driven executor="executor" scheduler="scheduler" />

3、在你的实现方法前加上注解@Scheduled(cron = “0 10 * * * ?”)就可以了
注意:
a、spring的@Scheduled注解 需要写在实现上、
b、 定时器的任务方法不能有返回值(如果有返回值,spring初始化的时候会告诉你有个错误、需要设定一个proxytargetclass的某个值为true、具体就去百度google吧)
c、实现类上要有组件的注解@Component
4、剩下的就是corn表达式了、具体使用以及参数请百度google、
下面只例出几个式子
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触发

以上来自http://blog.csdn.net/u010591939/article/details/50667162

项目实现:
只需要一个java类和一个spring.xml文件即可
spring.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:mvc="http://www.springframework.org/schema/mvc"
    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/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd
        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd
        http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task-3.0.xsd
        http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd ">

    <!-- 扫描包 -->
    <context:component-scan base-package="crontabTask" />

    <task:annotation-driven scheduler="qbScheduler" mode="proxy"/>
    <task:scheduler id="qbScheduler" pool-size="10"/>
</beans>

java代码示例:

package crontabTask;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import com.loveorange.bank.api.core.util.HttpClientUtils;
import com.loveorange.bank.api.core.util.HttpResponseContent;

@Component
public class CrontabTask {
    @Scheduled(cron = "0 */2 * * * ?")
    public void job1() {
        HttpResponseContent httpResponseContent = HttpClientUtils
                .get("http://localhost:8080");
        System.out.println(httpResponseContent.toString());
    }

//为方便本地测试,将定时任务配置写在新建的spring.xml文件中,定时任务单独启动或关闭,不影响主业务系统的运行
    public static void main(String[] args) {

        ApplicationContext actx = new ClassPathXmlApplicationContext("classpath:spring.xml");
    }
}

拓展:
前面提到,该定时任务在项目进行集群部署时会出现重复执行的问题,解决该问题有以下方式:
1.创建一个工程,单独写定时任务,与业务系统分离,保证定时任务只在一台服务器上执行,还方便维护。
可能遇到的问题:我的定时任务需要用到业务系统的方法,不想全部都改造成接口去调用
解决:用Maven在创建的定时任务工程的POM中添加业务工程的依赖即可。
为方便管理,通常将定时任务工程、业务相关工程等用Maven聚合工程搭建,即父工程parent下创建一个个的model,打包的话直接在parent工程上clean package,而且用git或svn管理也方便。
2.若定时任务要执行的方法都是一些接口调用的话,用linux的crond服务实现定时调用更简单
参考:http://blog.csdn.net/wangdengyang/article/details/78030652

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值