spring boot的quartz的配置方式
来源:互联网 发布:微信跳淘宝链接 编辑:程序博客网 时间:2024/06/07 23:35
方式1:添加注解
在定时类上添加注解
@Component
@EnableScheduling
在定时事件上添加注解
@Scheduled(fixedRate = 1000 * 30) 或 @Scheduled(cron="0/5 * * * * *")
完整实例
在定时类上添加注解
@Component
@EnableScheduling
在定时事件上添加注解
@Scheduled(fixedRate = 1000 * 30) 或 @Scheduled(cron="0/5 * * * * *")
完整实例
@Component@Configurable@EnableSchedulingpublic class ScheduledTasks {@Scheduled(fixedRate = 1000 * 30)public void printCurrentTime() {System.out.println("current"+dateFormat().format(new Date()));}@Scheduled(cron="0/5 * * * * *")public void printCurrentByCron() {System.out.println("corn" + dateFormat().format(new Date()));}private SimpleDateFormat dateFormat() {return new SimpleDateFormat("HH:mm:ss");}}
方式2:配置
第一步:添加依赖
<dependency> <groupId>org.quartz-scheduler</groupId> <artifactId>quartz</artifactId> <version>2.2.3</version> </dependency> <dependency><!-- 该依赖必加,里面有sping对schedule的支持 --> <groupId>org.springframework</groupId> <artifactId>spring-context-support</artifactId> </dependency>第二步:定义配置文件 quartz.xml,名字随意,暂定:spring-mvc(启动时需要注入,见第6步)<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:mvc="http://www.springframework.org/schema/mvc" xmlns:aop="http://www.springframework.org/schema/aop" xmlns:task="http://www.springframework.org/schema/task" xsi:schemaLocation=" http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.2.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task.xsd"> <aop:config proxy-target-class="true"/> <context:annotation-config/> <!-- 利用import引入定时器的文件 --> <import resource="spring-quartz.xml"/> </beans>第四部:添加定时器文件spring-quartz.xml
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd"> <!-- 使用MethodInvokingJobDetailFactoryBean,任务类可以不实现Job接口,通过targetMethod指定调用方法--> <bean id="taskJob" class="com.example.quartz.test.TestTask"/> <bean id="jobDetail" class="org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean"> <property name="group" value="job_work"/> <property name="name" value="job_work_name"/> <!--false表示等上一个任务执行完后再开启新的任务--> <property name="concurrent" value="false"/> <property name="targetObject"> <ref bean="taskJob"/> </property> <property name="targetMethod"> <value>run</value> </property> </bean> <!-- 调度触发器 --> <bean id="myTrigger" class="org.springframework.scheduling.quartz.CronTriggerFactoryBean"> <property name="name" value="work_default_name"/> <property name="group" value="work_default"/> <property name="jobDetail"> <ref bean="jobDetail" /> </property> <property name="cronExpression"> <value>0/10 * * * * ?</value> </property> </bean> <!-- 调度工厂 --> <bean id="scheduler" class="org.springframework.scheduling.quartz.SchedulerFactoryBean"> <property name="triggers"> <list> <ref bean="myTrigger"/> </list> </property> </bean> </beans>
第五步:创建定时器需要的类 TestTaskpackage com.example.quartz.test; import org.slf4j.Logger; import org.slf4j.LoggerFactory; public class TestTask { /** 日志对象 */ private static final Logger LOG = LoggerFactory.getLogger(TestTask.class); public void run() { if (LOG.isInfoEnabled()) { LOG.info("测试任务线程开始执行"); } System.out.println("quarty run ....."); } }
第6步:启动时扫描quartz文件主要是这个注解 @ImportResource(locations={"classpath:spring-mvc.xml"}),这样springboot就可以扫描到我们的配置文件了
package com.example; import org.junit.runner.RunWith; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.context.annotation.ImportResource; import org.springframework.jms.core.JmsTemplate; import org.springframework.scheduling.annotation.EnableScheduling; import org.springframework.test.context.ContextConfiguration; import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; @RunWith(SpringJUnit4ClassRunner.class) @SpringBootApplication @ImportResource(locations={"classpath:spring-mvc.xml"}) @EnableScheduling public class DemoApplication{ @Autowired private JmsTemplate jmsTemplate; public static void main(String[] args) { SpringApplication.run(DemoApplication.class, args); } }
阅读全文
0 0
- spring boot的quartz的配置方式
- spring的quartz两种配置方式
- Quartz-Spring集成Quartz通过XML配置的方式
- Quartz-Spring集成Quartz通过注解配置的方式
- spring boot的Java配置方式
- spring的Quartz配置
- Spring Boot和Quartz的整合
- Spring中Quartz的配置
- Spring中Quartz的配置
- Spring中Quartz的配置
- Spring中Quartz的配置
- Spring中Quartz的配置
- Spring中Quartz的配置
- Spring中Quartz的配置
- Spring中Quartz的配置
- Spring中Quartz的配置
- Spring中Quartz的配置
- Spring中Quartz的配置
- Oracle 导入、导出命令(imp/exp)
- 经典案例--十六进制转换为十进制
- 11:大整数减法
- Visual Studio2017数据库架构比较
- 若有说明:int *p1,*p2,m=5,n;以下均是正确赋值语句的选项是( )?
- spring boot的quartz的配置方式
- MySQL
- Codeforces Round #433 (Div. 2, based on Olympiad of Metropolises)
- 将一个文件夹中的图片移动到另一个文件夹,并重命名
- 头条校招手串
- python+selenium 点击单选框-radio的实现
- pycharm Enable Django Support 设置
- TerraExplorer 6.6 for JavaScript 通过图层获取地理要素,获取图层属性信息,矢量标注等通用方法
- burp代理手机客户端不支持代理还走http协议方法