SpringTask任务案例源码实现


写在开始

一般来说,无论是生活中或者项目中都会用到定时任务。比如我自己来说,每天晚上写一篇博客,当然这个任务的时间点可能不是那么准时。更多的例子,比如我们项目中数据的统计,文件或者数据库的定时备份等等。

举个特别常见的例子,比如一些大型网站,首页的一些数据展示。有时候并不是实时展示的,可能是十分钟或者更久更新一次,来呈现给用户。

任务介绍

就目前自己接触的定时人来来说,这里简单的聊下一下三种:

1)java自带的java.util.Timer类,这个类允许你调度一个java.util.TimerTask任务。使用这种方式可以让你的程序按照某一个频度执行,但不能在指定时间运行。

2)开源调度框架Quartz,这是一个功能丰富比较强大的的调度器,可以实现内存和实例化数据库的任务(基于配置文件的内存实现和数据库实现)

3)spring3以后自带的task任务管理,可以将它看成一个轻量级的Quartz,相对来说使用起来更加方便。

功能实现

关于1,2任务这里不做介绍,后面会为大家详细介绍,这里主要介绍一下springTask的使用方法。

这里使用的spring4.0.6版本,是支持springTask的 ,需要依赖spring-context-4.0.6.RELEASE.jar和spring-context-support-4.0.6.RELEASE.jar等等,后面会有源码提供。

springTask提供了基于xml配置和注解的两种实现方式。

基于注解的实现TestJob.java:

   
   
  1. package task;
  2. import org.springframework.scheduling.annotation.Scheduled;
  3. import org.springframework.stereotype.Component;
  4. /**
  5. * 任务测试 科帮网 http://www.52itstyle.com/
  6. * 创建者 张志朋
  7. * 创建时间 2017年4月22日
  8. *
  9. */
  10. @Component("TestJob")
  11. public class TestJob {
  12. @Scheduled(cron = "0/5 * * * * ?")//每隔1秒隔行一次
  13. public void test1()
  14. {
  15. System.out.println("job1 开始执行");
  16. }
  17. @Scheduled(cron = "0/5 * * * * ?")//每隔1秒隔行一次
  18. public void test2()
  19. {
  20. System.out.println("job2 开始执行");
  21. }
  22. }

基于注解的实现spring-context.xml:

   
   
  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. </beans>

基于配置的实现TestJob.java:

   
   
  1. package task;
  2. import org.springframework.stereotype.Component;
  3. /**
  4. * 任务测试 科帮网 http://www.52itstyle.com/
  5. * 创建者 张志朋
  6. * 创建时间 2017年4月22日
  7. *
  8. */
  9. @Component("TestJob")
  10. public class TestJob {
  11. public void test1()
  12. {
  13. System.out.println("job1 开始执行");
  14. }
  15. public void test2()
  16. {
  17. System.out.println("job2 开始执行");
  18. }
  19. }

基于配置的实现spring-context.xml:

   
   
  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="test1" cron="0/5 * * * * ?"/>
  22. <task:scheduled ref="TestJob" method="test2" cron="0/6 * * * * ?"/>
  23. </task:scheduled-tasks>
  24. </beans>

附:
cronExpression的配置说明
字段 允许值 允许的特殊字符
秒 0-59 , - /
分 0-59 , - 
/
小时 0-23 , - /
日期 1-31 , - 
? / L W C
月份 1-12 或者 JAN-DEC , - /
星期 1-7 或者 SUN-SAT , - 
? / L C #
年(可选) 留空, 1970-2099 , - * /

  • 区间
  • 通配符
    ? 你不想设置那个字段
    下面只例出几个式子

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触发
“0 
14 ?” 每天从下午2点开始到2点59分每分钟一次触发
“0 0/5 14 ?” 每天从下午2点开始到2:55分结束每5分钟一次触发
“0 0/5 14,18 ?” 每天的下午2点至2:55和6点至6点55分两个时间段内每5分钟一次触发
“0 0-5 14 ?” 每天14:00至14:05每分钟一次触发
“0 10,44 14 ? 3 WED” 三月的每周三的14:10和14:44触发
“0 15 10 ? * MON-FRI” 每个周一、周二、周三、周四、周五的10:15触发

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Spring MVC是一种基于Java的Web应用框架,它可以帮助开发者快速构建Web应用程序。要实现一个网上书城的源码,首先需要创建一个基于Spring MVC的项目。然后,我们需要定义书城的功能和需求,比如展示书籍、购买书籍、用户登录注册等。接着,我们可以定义书籍的数据模型,比如书籍的名称、作者、价格等信息。 在Spring MVC中,我们可以使用@Controller注解来定义处理HTTP请求的控制器类。我们可以在控制器类中定义各种处理请求的方法,比如展示书籍列表的方法、展示书籍详情的方法、处理用户购买书籍的方法等。通过@RequestMapping注解,我们可以将请求映射到相应的处理方法上。 此外,我们还可以使用@Service注解来定义服务类,比如书籍服务类、用户服务类等。服务类可以实现业务逻辑,比如获取书籍列表、添加购物车、生成订单等。 在视图层,我们可以使用Thymeleaf、JSP等模板引擎来构建页面,展示书籍信息、用户信息等。而在数据持久层,我们可以选择使用Hibernate、Spring Data JPA等技术来操作数据库,管理书籍信息、用户信息等数据。 最后,我们可以使用Maven或者Gradle等工具来管理项目的依赖,构建项目的打包部署。通过以上步骤,我们就可以实现一个基于Spring MVC的网上书城源码实现书籍的展示、购买和用户管理等功能。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值