在路上

 找回密码
 立即注册
在路上 站点首页 学习 查看内容

SpringMvc自动任务调度之task实现

2016-7-29 15:47| 发布者: zhangjf| 查看: 581| 评论: 0

摘要: 如果只是简单的跑个任务其实spring升级到3后已经自带任务调度器了,相比之下Spring task无论是理解还是使用都简单很多。但是Quartz有线程和线程管理以及集群等高级特性,所以大家可以自行选择了。不过一般情况下,觉 ...
如果只是简单的跑个任务其实spring升级到3后已经自带任务调度器了,相比之下Spring task无论是理解还是使用都简单很多。但是Quartz有线程和线程管理以及集群等高级特性,所以大家可以自行选择了。不过一般情况下,觉得SpringTask足够了。


Spring Task提供两种方式进行配置,注解和配置文件。使用注解虽然简单,不用配置xml,但是相对于修改比较频繁的任务来说,打包编译的过程也是挺麻烦的,建议使用配置文件实现。
源码:http://my.oschina.net/52love/blog/712254
  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  3. xmlns:context="http://www.springframework.org/schema/context" xmlns:jdbc="http://www.springframework.org/schema/jdbc"
  4. xmlns:jee="http://www.springframework.org/schema/jee" xmlns:tx="http://www.springframework.org/schema/tx"
  5. xmlns:util="http://www.springframework.org/schema/util" xmlns:task="http://www.springframework.org/schema/task" xsi:schemaLocation="
  6. http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
  7. http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd
  8. http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc-4.0.xsd
  9. http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-4.0.xsd
  10. http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.0.xsd
  11. http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-4.0.xsd
  12. http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task-4.0.xsd">
  13. <description>Spring Configuration</description>
  14. <context:component-scan base-package="task"/>
  15. <!-- 配置任务线性池 -->
  16. <task:executor id="executor" pool-size="10" />
  17. <task:scheduler id="scheduler" pool-size="10"/>
  18. <task:annotation-driven scheduler="scheduler" executor="executor" proxy-target-class="true"/>
  19. <!-- 配置文件实现 如果使用配置文件实现 把注释解开即可然后 删除掉代码的注解-->
  20. <!-- <task:scheduled-tasks scheduler="scheduler">
  21. <task:scheduled ref="TestJob" method="test" cron="0/1 * * * * ?"/>
  22. </task:scheduled-tasks> -->
  23. </beans>
复制代码
  1. package task;
  2. import org.springframework.scheduling.annotation.Scheduled;
  3. import org.springframework.stereotype.Component;
  4. @Component("TestJob")
  5. public class TestJob {
  6. @Scheduled(cron = "0/5 * * * * ?")//每隔5秒隔行一次
  7. public void test1()
  8. {
  9. System.out.println("job1 开始执行");
  10. }
  11. @Scheduled(cron = "0/5 * * * * ?")//每隔5秒隔行一次
  12. public void test2()
  13. {
  14. System.out.println("job2 开始执行");
  15. }
  16. }
复制代码
上一篇:考试系统下一篇:StringBuffer用法

最新评论

小黑屋|在路上 ( 蜀ICP备15035742号-1 

;

GMT+8, 2025-5-6 12:19

Copyright 2015-2025 djqfx

返回顶部