spring 定时任务@Scheduled (spring-task注解形式)
来源:互联网 发布:ubuntu xfree 编辑:程序博客网 时间:2024/05/04 08:12
sping 中定时任务的实现大概有三种
1. ava自带的java.util.Timer类
2.使用Quartz
3.spring-task
第一种可以让你的程序按照某一个频度执行,但不能在指定时间运行,所以不做介绍
第二种功能强大,配置相对较麻烦,不做介绍
我使用的是spring-task的注解方式
前两种详细的介绍可参考文章 http://gong1208.iteye.com/blog/1773177
简单demo测试
package xhsoft.yxd.admin.task;import org.springframework.scheduling.annotation.EnableScheduling;import org.springframework.scheduling.annotation.Scheduled;import org.springframework.stereotype.Component;/** * @author WangMeng * @date 2017年1月16日 */@Component@EnableScheduling// 通过@EnableScheduling注解开启对计划任务的支持public class TaskTest{@Scheduled(cron = "1/10 * * * * ?")// 通过@Scheduled声明该方法是计划任务 使用cron属性可按照指定时间执行 具体用法可查询 “cron表达式”的相关资料void testA(){System.out.println("定时任务A每10秒执行次。。");}@Scheduled(cron = "1/5 * * * * ?")void testB(){System.out.println("定时任务B每5秒执行次。。");}}
定时任务B每5秒执行次。。
定时任务B每5秒执行次。。
定时任务A每10秒执行次。。
定时任务B每5秒执行次。。
定时任务A每10秒执行次。。
定时任务B每5秒执行次。。
在测试过程中遇到一个问题,就是定时任务执行了两次,排查原因发现整个spring 容器被加载了两次,所以原因出在tomcat服务器上
当时的servlet.xml的配置是
<Host name="localhost" appBase="webapps"
unpackWARs="true" autoDeploy="true">
<Valve className="org.apache.catalina.valves.AccessLogValve" directory="logs"
prefix="localhost_access_log" suffix=".txt"
pattern="%h %l %u %t "%r" %s %b" />
<Context debug="0" docBase="xinghan-yxd-web" path="/yxd" privileged="true" reloadable="false"/>
</Host>
经过查资料了解到 host 节点中的appBase属性值为webapps,这里的意思是会加载webapps下的所有目录,Context 节点中的docBase 属性值为xinghan-yxd-web,这里用了相对路径,这个项目也在webapps下面,所以又加载了一次,所以我这里的配置是有问题的,一般情况下Context 节点中的docBase 配置的项目应该不在webapps下面的
解决方法就是 把项目移到别的目录下
主要是理解 Host Context 两个节点作用
详细的tomcat 的servlet.xml配置文件说明,参考文章 http://blog.163.com/jxguo_05/blog/static/7194010020106810953194/
- spring 定时任务@Scheduled (spring-task注解形式)
- spring @Scheduled注解 定时任务
- spring @Scheduled注解 定时任务
- 【Spring Task】定时任务详解实例-@Scheduled
- spring-boot 定时任务之Scheduled Task
- 使用spring @Scheduled注解执行定时任务、
- 使用spring @Scheduled注解执行定时任务、
- 使用spring @Scheduled注解执行定时任务
- 使用spring @Scheduled注解执行定时任务
- 使用spring @Scheduled注解执行定时任务
- 使用spring @Scheduled注解执行定时任务、
- 使用spring @Scheduled注解执行定时任务
- 使用spring @Scheduled注解执行定时任务
- 使用spring @Scheduled注解执行定时任务
- 使用spring @Scheduled注解执行定时任务
- 使用spring @Scheduled注解执行定时任务
- 使用spring @Scheduled注解执行定时任务、
- 使用spring @Scheduled注解执行定时任务
- markdown的基本使用方法
- omnet 未定义的引用 问题
- QCustomplot使用分享(三) 图
- android代码规范和studio配置CodeStyle
- 【Hibernate】——多对多关联映射
- spring 定时任务@Scheduled (spring-task注解形式)
- activity图形化过程的绘制过程
- HTTP请求头大全 - 常用参考表对照表
- 广州光环国际:PMI-ACP敏捷项目管理认证说明会
- unity版本控制
- Adobe Photoshop CS6 的下载安装及激活
- 劳务关系与劳动关系的区别
- nodejs例子
- 摄像头标定(carlibration)、Homography以及它的matlab实现