spring定时器的基本使用
来源:互联网 发布:2015乘用车销量数据 编辑:程序博客网 时间:2024/05/21 14:42
1.使用注解:定义一个组件,扫描一下包即可
package com.spring.schedule.demo01;import org.slf4j.Logger;import org.slf4j.LoggerFactory;import org.springframework.scheduling.annotation.EnableScheduling;import org.springframework.scheduling.annotation.Scheduled;import org.springframework.stereotype.Component;import java.text.SimpleDateFormat;import java.util.Date;@Component@EnableSchedulingpublic class ScheduledTasksForAnnotation { private static final Logger log = LoggerFactory.getLogger(ScheduledTasksForAnnotation.class); private static final SimpleDateFormat dateFormat = new SimpleDateFormat("HH:mm:ss"); //每隔5s执行一次 @Scheduled(fixedRate = 5000) //每天12点 24点执行 //@Scheduled(cron = "0 0 12,0 * * ?") public void reportCurrentTime() { System.out.println("日志打印时间:"+dateFormat.format(new Date())); }}
<!-- 配置要扫描的包 --> <!-- 使用Annotation自动注册Bean,解决事物失效问题:在主容器中不扫描@Controller注解,在SpringMvc中只扫描@Controller注解。 --> <context:component-scan base-package="com.spring.schedule"><!-- base-package 如果多个,用“,”分隔 --> <context:exclude-filter type="annotation" expression="org.springframework.stereotype.Controller"/> </context:component-scan>如果不想使用
@EnableScheduling则需增加如下配置
<!--方法一:task任务扫描注解,扫描定时任务,spring3.1后可以使用@EnableScheduling注解替代,如果不想使用注解,把下面打开即可--> <!--<task:annotation-driven />--> <!--<context:component-scan base-package="com.spring.schedule.demo01" />-->
2.使用quartz
增加一个spring-jobs.xml配置,引入到spring配置文件
<?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:context="http://www.springframework.org/schema/context"xmlns:cache="http://www.springframework.org/schema/cache"xsi:schemaLocation="http://www.springframework.org/schema/beanshttp://www.springframework.org/schema/beans/spring-beans-3.0.xsdhttp://www.springframework.org/schema/contexthttp://www.springframework.org/schema/context/spring-context-3.0.xsd http://www.springframework.org/schema/cache http://www.springframework.org/schema/cache/spring-cache.xsd" > <!-- ======================== 创建调度工厂 ======================== --> <bean id="SpringJobSchedulerFactoryBean" class="org.springframework.scheduling.quartz.SchedulerFactoryBean"> <property name="triggers"> <list> <!-- 计划开始定时任务调度,此处可以添加多个定时任务 --> <ref local="myTaskTrigger"/> <!--<ref local="..."/>--> </list> </property> </bean> <!-- ======================== 创建调度工厂中的一个调度触发器 ======================== --> <!-- 定时定时任务的触发器 --> <bean id="myTaskTrigger" class="org.springframework.scheduling.quartz.CronTriggerFactoryBean"><!--CronTriggerBean--> <property name="jobDetail"> <ref bean="myTask" /> </property> <property name="cronExpression"> <!-- 每天凌晨3:30分执行任务 --> <!--<value>0 30 3 * * ?</value>--> <!-- 每5s执行一次任务 --> <value>*/5 * * * * ?</value> </property> </bean> <!-- ======================== 这个触发器触发的实际任务 ======================== --> <bean id="myTask" class="org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean"> <property name="targetObject"> <bean class="com.spring.schedule.demo02.ScheduledTaskForQuartz" /> </property> <property name="targetMethod"> <value>reportCurrentTime</value> </property> </bean></beans>下面是实际任务的类
package com.spring.schedule.demo02;import org.slf4j.Logger;import org.slf4j.LoggerFactory;import org.springframework.stereotype.Component;import java.text.SimpleDateFormat;import java.util.Date;@Componentpublic class ScheduledTaskForQuartz { private static final Logger log = LoggerFactory.getLogger(ScheduledTaskForQuartz.class); private static final SimpleDateFormat dateFormat = new SimpleDateFormat("HH:mm:ss"); public void reportCurrentTime() { System.out.println("日志打印时间:"+dateFormat.format(new Date())); }}代码可以参考我的github:git@github.com:SincerelyUnique/spring-task.git
阅读全文
0 0
- spring定时器的基本使用
- spring定时器的使用
- Spring定时器的使用
- Spring定时器的使用
- Spring定时器的使用
- Spring定时器的使用
- Spring定时器的使用
- spring 定时器的使用
- Spring定时器的使用
- Spring定时器的使用
- Spring定时器的使用
- spring定时器的使用
- Timer定时器的基本使用
- Spring+Quartz定时器的使用
- spring定时器的使用详解
- Spring定时器的简单使用
- Spring中定时器的使用
- Spring定时器的简单使用
- 一脸懵逼学习MapReduce的原理和编程(Map局部处理,Reduce汇总)
- extern "c"用法解析
- Java连接Redis数据库 事务、管道和分布式的调用方式
- ViewGroup 事件分发
- SCXML有限状态机规范详解与使用
- spring定时器的基本使用
- 现在有一棵合法的二叉树,树的节点都是用数字表示,现在给定这棵树上所有的父子关系,求这棵树的高度
- linux环境下监控日志的变化命令 tail -f
- OkHttp 网络请求
- 顺序表应用1:多余元素删除之移位算法
- springboot-shiro
- SSH spring+springmvc+hibernate+maven 整合详解
- Docker在PHP中的实践过程
- HDU 6215 链表