Spring 3.0以后版本的定时任务
自主开发的定时任务工具,spring task,可以将它比作一个轻量级的Quartz,而且使用起来很简单,除spring相关的包外不需要额外的包,而且支持注解和配置文件两种
- <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: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/task
- http://www.springframework.org/schema/task/spring-task-3.0.xsd">
- <!--
- 这里加入了
- xmlns:task="http://www.springframework.org/schema/task"
- http://www.springframework.org/schema/task
- http://www.springframework.org/schema/task/spring-task-3.0.xsd
- -->
- <task:annotation-driven /> <!-- 定时器开关-->
- <bean id="taskTest" class="com.jungle.test.TaskTest"></bean>
- <task:scheduled-tasks>
- <!--
- 这里表示的是从第五秒开始 ,每三秒执行一次 (而不是 三分之五 秒执行一次哦~~)
- -->
- <task:scheduled ref="taskTest" method="say" cron="5/3 * * * * ?" />
- <task:scheduled ref="taskTest" method="hello" cron="5/3 * * * * ?"/>
- </task:scheduled-tasks>
- import java.util.Date;
- public class TaskTest {
- public void say() {
- System.out.println("这个真好用!!!" + new Date());
- }
- public void hello(){
- System.out.println("hello!!!");
- }
- }
第二种:spring注解形式
也许我们不想每写一个任务类还要在xml文件中配置下,我们可以使用注解@Scheduled,我们看看源文件中该注解的定义:
- @Target({java.lang.annotation.ElementType.METHOD, java.lang.annotation.ElementType.ANNOTATION_TYPE})
- @Retention(RetentionPolicy.RUNTIME)
- @Documented
- public @interface Scheduled
- {
- public abstract String cron();
- public abstract long fixedDelay();
- public abstract long fixedRate();
- }
measured from the start time of the previous task. The time unit value
is measured in milliseconds.即从上一个任务开始到下一个任务开始的间隔,单位是毫秒。
- import org.springframework.scheduling.annotation.Scheduled;
- import org.springframework.stereotype.Component;
- @Component(“taskJob”)
- public class TaskJob {
- @Scheduled(cron = "0 0 3 * * ?")
- public void job1() {
- System.out.println(“任务进行中。。。”);
- }
- }
- xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:aop="http://www.springframework.org/schema/aop"
- 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/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
- http://www.springframework.org/schema/context
- http://www.springframework.org/schema/jdbc/spring-jdbc-3.0.xsd
- http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
- http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task-3.0.xsd"
- default-lazy-init="false">
- <context:annotation-config />
- <!—spring扫描注解的配置 -->
- <context:component-scan base-package="com.gy.mytask" />
- <!—开启这个配置,spring才能识别@Scheduled注解 -->
- <task:annotation-driven scheduler="qbScheduler" mode="proxy"/>
- <task:scheduler id="qbScheduler" pool-size="10"/>
Spring 3.0以后版本的定时任务的更多相关文章
- SpringBoot之整合Redis分析和实现-基于Spring Boot2.0.2版本
背景介绍 公司最近的新项目在进行技术框架升级,基于的Spring Boot的版本是2.0.2,整合Redis数据库.网上基于2.X版本的整个Redis少之又少,中间踩了不少坑,特此把整合过程记录,以供 ...
- Spring Boot2.0以上版本EmbeddedServletContainerCustomizer被WebServerFactoryCustomizer替代
在Spring Boot2.0以上配置嵌入式Servlet容器时EmbeddedServletContainerCustomizer类不存在,经网络查询发现被WebServerFactoryCusto ...
- SpringBoot之整合Quartz调度框架-基于Spring Boot2.0.2版本
1.项目基础 项目是基于Spring Boot2.x版本的 2.添加依赖 <!-- quartz依赖 --> <dependency> <groupId>org.s ...
- Spring Boot2.0 静态资源被拦截问题
在Spring Boot2.0+的版本中,只要用户自定义了拦截器,则静态资源会被拦截.但是在spring1.0+的版本中,是不会拦截静态资源的. 因此,在使用Spring Boot2.0+时,配置拦截 ...
- spring boot 2.0.0由于版本不匹配导致的NoSuchMethodError问题解析
spring boot升级到2.0.0以后,项目突然报出 NoSuchMethodError: org.springframework.boot.builder.SpringApplicationBu ...
- 0.9.0.RELEASE版本的spring cloud alibaba sentinel+gateway网关实例
sentinel除了让服务提供方.消费方用之外,网关也能用它来限流.我们基于上次整的网关(参见0.9.0.RELEASE版本的spring cloud alibaba nacos+gateway网关实 ...
- 0.9.0.RELEASE版本的spring cloud alibaba nacos+gateway网关实例
gateway就是用来替换zuul的,功能都差不多,我们看下它怎么来跟nacos一起玩.老套路,三板斧: 1.pom: <?xml version="1.0" encodin ...
- 0.9.0.RELEASE版本的spring cloud alibaba sentinel+feign降级处理实例
既然用到了feign,那么主要是针对服务消费方的降级处理.我们基于0.9.0.RELEASE版本的spring cloud alibaba nacos+feign实例添油加醋,把sentinel功能加 ...
- 0.9.0.RELEASE版本的spring cloud alibaba sentinel限流、降级处理实例
先看服务提供方的,我们在原来的sentinel实例(参见0.9.0.RELEASE版本的spring cloud alibaba sentinel实例)上加上限流.降级处理,三板斧只需在最后那一斧co ...
随机推荐
- Windows 7 的系统文件修复:sfc /scannow
在线检查与修复 C:\Windows\system32>sfc /scannow 开始系统扫描.此过程将需要一些时间. 开始系统扫描的验证阶段. 验证 100% 已完成. Windows 资源保 ...
- /proc 文件系统
linux提供了一种特殊的文件系统procfs,通常以/proc目录的形式呈现.该目录中包含了许多特殊文件用来对驱动程序和内核信息进行更高层的访问.只要应用程序有正确的访问全息,就可以通过读写这些文件 ...
- 使用NSOperation使用,创建线程中传递多个参数
参考:http://blog.csdn.net/dqjyong/article/details/7677557 参考:http://stackoverflow.com/questions/232761 ...
- Linux使用笔记: 定制core dump文件的文件名
在开发过程中,当一个Linux程序异常退出时,我们可以通过core文件来分析它异常的详细原因.缺省情况下,Linux在程序异常时不产生core文件,要想让程序异常退出时产生core dump文件,需要 ...
- mysql 正则
mysql 正则学习 基本字符匹配 select desk from dealer_info where desk regexp "82107777"; . 表示匹配任意一个字符 ...
- linux命令(10)使用kill杀死含有指定关键字的进程
命令:ps -ef|grep keyword|grep -v grep|cut -c 9-15|xargs kill -9 批量杀死包含关键字“keyword”的进程. "ps -ef&qu ...
- Npoi导出xlsx
xlsx文件可以超出,excel2003行数限制.必要的dll文件. public void Export() { string filename = Request["searchStri ...
- Java多线程之Wait()和Notify()
1.Wait()和Notify.NotifyAll都是Object的方法 2.多线程的协作是通过控制同一个对象的Wait()和Notify()完成 3.当调用Wait()方法时,当前线程进入阻塞状态, ...
- 在Windows驗證網站設定部分匿名存取
最近接連遇到幾次的需求:供內部使用的ASP.NET網站,全站使用Windows驗證,使用者以網域AD帳號登入,但網站包含少數API性質的ASHX.ASPX或MVC Action,提供其他系統呼叫整合. ...
- C# 导出 Excel 和相关打印设置
源地址:http://blog.csdn.net/wanmingtom/article/details/6125599 Excel.Application myExcel = new Excel.Ap ...