为了账号安全,请及时绑定邮箱和手机立即绑定

谈一谈Spring中的定时任务

标签:
Java

通常来说,如果要执行一个定时任务,基本上的操作是这样的:

public class Task3 {  
    public static void main(String[] args) {  
        Runnable runnable = new Runnable() {  
            public void run() {  
                // task to run goes here  
                System.out.println("Hello !!");  
            }  
        };  
        ScheduledExecutorService service = Executors  
                .newSingleThreadScheduledExecutor();  
        // 第二个参数为首次执行的延时时间,第三个参数为定时执行的间隔时间  
        service.scheduleAtFixedRate(runnable, 10, 1, TimeUnit.SECONDS);  
    }  
}

这种情况要main方法中显式的调用scheduleAtFixedRate方法才能运行。但是在Spring环境中或者Web环境中没有main方法,或者没有显式的调用入口,该如何调用调用这个方法呢?

1.使用InitializingBean,当类被初始化时scheduleAtFixedRate被显式调用:

public class ScheduleTask  implements InitializingBean,Runnable{    @Override
    public void run() {
        System.out.println(  new Date() +"任务执行");
    }    @Override
    public void afterPropertiesSet() throws Exception {

        ScheduledExecutorService service = Executors.newSingleThreadScheduledExecutor();        // 第二个参数为首次执行的延时时间,第三个参数为定时执行的间隔时间
        service.scheduleAtFixedRate(this, 10, 1, TimeUnit.SECONDS);

    }
}

beans.xml:

<bean id="task"  class="com.alibaba.test.scheduling.ScheduleTask"/>

2. 使用 @Scheduled注解:

public class ScheduleTask{    @Scheduled(fixedRate=1)    public void run() {
        System.out.println(  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:p="http://www.springframework.org/schema/p"
    xmlns:sofa="http://schema.alipay.com/sofa/schema/service"
    xmlns:context="http://www.springframework.org/schema/context"
       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://schema.alipay.com/sofa/schema/service http://schema.alipay.com/sofa/sofa-service-4-0-0.xsd
         http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task.xsd
         http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd"
    default-autowire="byName">
    <bean id="task"  class="com.alibaba.test.scheduling.ScheduleTask"/>
  <task:annotation-driven>
  </task:annotation-driven></beans>

当然也可以指定定时任务线程池·。

3.只使用使用XML配置:

public class ScheduleTask{    public void run() {
        System.out.println(  new Date() +"任务执行");
    }
}
<?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:p="http://www.springframework.org/schema/p"
    xmlns:sofa="http://schema.alipay.com/sofa/schema/service"
    xmlns:context="http://www.springframework.org/schema/context"
       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://schema.alipay.com/sofa/schema/service http://schema.alipay.com/sofa/sofa-service-4-0-0.xsd
         http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task.xsd
         http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd"
    default-autowire="byName">
    <bean id="task"  class="com.alibaba.test.scheduling.ScheduleTask"/>
    <task:scheduler id="scheduler" pool-size="10"  />  <!--配置定时器-->
    <task:scheduled-tasks scheduler="scheduler">
        <task:scheduled ref="task" method="run" fixed-delay="1000" />
    </task:scheduled-tasks></beans>



作者:有效栈
链接:https://www.jianshu.com/p/e0bbd9c55fcb


点击查看更多内容
TA 点赞

若觉得本文不错,就分享一下吧!

评论

作者其他优质文章

正在加载中
  • 推荐
  • 评论
  • 收藏
  • 共同学习,写下你的评论
感谢您的支持,我会继续努力的~
扫码打赏,你说多少就多少
赞赏金额会直接到老师账户
支付方式
打开微信扫一扫,即可进行扫码打赏哦
今天注册有机会得

100积分直接送

付费专栏免费学

大额优惠券免费领

立即参与 放弃机会
微信客服

购课补贴
联系客服咨询优惠详情

帮助反馈 APP下载

慕课网APP
您的移动学习伙伴

公众号

扫描二维码
关注慕课网微信公众号

举报

0/150
提交
取消