quartz + springboot 注解_SpringBoot整合Quartz 2实现定时任务之一:整合

1、前言说明

《SpringBoot定时任务说明》这篇文章有说明过SpringBoot的定时任务实现方式,不过介绍的是一种轻量级的定时任务实现。但对于高级的定时需求,就难以满足了:比如工程运行过程中控制定时任务的开关等。这个时候就需要引入Quartz了,本文主要介绍了SpringBoot与Quartz的整合。

2、整合步骤

所需依赖,下述依赖包只截取了整合所需依赖,SpringBoot配置所需依赖不包括其中:

1.7

UTF-8

4.2.7.RELEASE

org.springframework

spring-context-support

${spring.version}

org.mybatis

mybatis

3.1.1

org.mybatis

mybatis-spring

1.1.1

org.springframework.boot

spring-boot-starter-jdbc

org.quartz-scheduler

quartz

2.2.3

项目整合的是最新版的Spring版本即4.2.7.RELEASE,同时Quartz也是最新版的2.2.3,如果融合工程后,出现错误,首先检查下版本是否有问题。

在Spring中使用Quartz有两种方式实现:第一种是任务类继承QuartzJobBean,第二种则是在配置文件里定义任务类和要执行的方法,类和方法可以是普通类。很显然,第二种方式远比第一种方式来的灵活。

这里采用的就是第二种方式。

run

class="org.springframework.scheduling.quartz.CronTriggerFactoryBean">

0/10 * * * * ?

TestTask类则是一个普通的Java类,没有继承任何类和实现任何接口(当然可以用注解方式来声明bean):

public class TestTask {

/** 日志对象 */

private static final Logger LOG = LoggerFactory.getLogger(TestTask.class);

public void run() {

if (LOG.isInfoEnabled()) {

LOG.info("测试任务线程开始执行");

//new ScheduleJobService().getScheduleJob();

}

}

}

至此,简单的整合大功告成,run方法将每隔5秒执行一次,因为配置了concurrent等于false,所以假如run方法的执行时间超过5秒,在执行完之前即使时间已经超过了5秒下一个定时计划执行任务仍不会被开启,如果是true,则不管是否执行完,时间到了都将开启。

顺便贴一下cronExpression表达式备忘:

e199e66db28547a5bfd3ae0d631b726d

表达式意义

84dfc05eb766454c9a2e546bbdfcea61
99fcca5d398749f4b896de3ac114646f

3、运行结果

016-07-31-13-13 [main] [com.alibaba.App] [INFO] - oops: main入口函数编码-UTF-8

. ____ _ __ _ _

/ / ___'_ __ _ _(_)_ __ __ _

( ( )___ | '_ | '_| | '_ / _` |

/ ___)| |_)| | | | | || (_| | ) ) ) )

' |____| .__|_| |_|_| |___, | / / / /

=========|_|==============|___/=/_/_/_/

:: Spring Boot :: (v1.3.6.RELEASE)

2016-07-31-13-13 [main] [com.alibaba.App] [INFO] - Starting App on localhost with PID 1781 (/Users/xiaolong/Documents/workspace/quartz-springboot/target/classes started by xiaolong in /Users/xiaolong/Documents/workspace/quartz-springboot)

2016-07-31-13-13 [main] [com.alibaba.App] [INFO] - No active profile set, falling back to default profiles: default

2016-07-31-13-13 [main] [org.springframework.boot.context.embedded.AnnotationConfigEmbeddedWebApplicationContext] [INFO] - Refreshing org.springframework.boot.context.embedded.AnnotationConfigEmbeddedWebApplicationContext@68231c67: startup date [Sun Jul 31 13:13:42 CST 2016]; root of context hierarchy

2016-07-31-13-13 [main] [org.springframework.beans.factory.xml.XmlBeanDefinitionReader] [INFO] - Loading XML bean definitions from class path resource [spring/applicationContext.xml]

2016-07-31-13-13 [main] [org.springframework.beans.factory.config.PropertyPlaceholderConfigurer] [INFO] - Loading properties file from class path resource [spring/config/application-dev.properties]

2016-07-31-13-13 [background-preinit] [org.hibernate.validator.internal.util.Version] [INFO] - HV000001: Hibernate Validator 5.2.4.Final

2016-07-31-13-13 [main] [org.springframework.boot.context.embedded.tomcat.TomcatEmbeddedServletContainer] [INFO] - Tomcat initialized with port(s): 7001 (http)

2016-07-31-13-13 [main] [org.apache.catalina.core.StandardService] [INFO] - Starting service Tomcat

2016-07-31-13-13 [main] [org.apache.catalina.core.StandardEngine] [INFO] - Starting Servlet Engine: Apache Tomcat/8.0.36

2016-07-31-13-13 [localhost-startStop-1] [org.apache.catalina.core.ContainerBase.[Tomcat].[localhost].[/]] [INFO] - Initializing Spring embedded WebApplicationContext

2016-07-31-13-13 [localhost-startStop-1] [org.springframework.web.context.ContextLoader] [INFO] - Root WebApplicationContext: initialization completed in 884 ms

2016-07-31-13-13 [main] [org.quartz.impl.StdSchedulerFactory] [INFO] - Using default implementation for ThreadExecutor

2016-07-31-13-13 [main] [org.quartz.core.SchedulerSignalerImpl] [INFO] - Initialized Scheduler Signaller of type: class org.quartz.core.SchedulerSignalerImpl

2016-07-31-13-13 [main] [org.quartz.core.QuartzScheduler] [INFO] - Quartz Scheduler v.2.2.3 created.

2016-07-31-13-13 [main] [org.quartz.simpl.RAMJobStore] [INFO] - RAMJobStore initialized.

2016-07-31-13-13 [main] [org.quartz.core.QuartzScheduler] [INFO] - Scheduler meta-data: Quartz Scheduler (v2.2.3) 'scheduler' with instanceId 'NON_CLUSTERED'

Scheduler class: 'org.quartz.core.QuartzScheduler' - running locally.

NOT STARTED.

Currently in standby mode.

Number of jobs executed: 0

Using thread pool 'org.quartz.simpl.SimpleThreadPool' - with 10 threads.

Using job-store 'org.quartz.simpl.RAMJobStore' - which does not support persistence. and is not clustered.

2016-07-31-13-13 [main] [org.quartz.impl.StdSchedulerFactory] [INFO] - Quartz scheduler 'scheduler' initialized from an externally provided properties instance.

2016-07-31-13-13 [main] [org.quartz.impl.StdSchedulerFactory] [INFO] - Quartz scheduler version: 2.2.3

2016-07-31-13-13 [main] [org.quartz.core.QuartzScheduler] [INFO] - JobFactory set to: org.springframework.scheduling.quartz.AdaptableJobFactory@58cdc845

2016-07-31-13-13 [main] [org.springframework.context.support.DefaultLifecycleProcessor] [INFO] - Starting beans in phase 2147483647

2016-07-31-13-13 [main] [org.springframework.scheduling.quartz.SchedulerFactoryBean] [INFO] - Starting Quartz Scheduler now

2016-07-31-13-13 [main] [org.quartz.core.QuartzScheduler] [INFO] - Scheduler scheduler_$_NON_CLUSTERED started.

2016-07-31-13-13 [main] [org.apache.coyote.http11.Http11NioProtocol] [INFO] - Initializing ProtocolHandler ["http-nio-7001"]

2016-07-31-13-13 [main] [org.apache.coyote.http11.Http11NioProtocol] [INFO] - Starting ProtocolHandler ["http-nio-7001"]

2016-07-31-13-13 [main] [org.apache.tomcat.util.net.NioSelectorPool] [INFO] - Using a shared selector for servlet write/read

2016-07-31-13-13 [main] [org.springframework.boot.context.embedded.tomcat.TomcatEmbeddedServletContainer] [INFO] - Tomcat started on port(s): 7001 (http)

2016-07-31-13-13 [main] [com.alibaba.App] [INFO] - Started App in 1.65 seconds (JVM running for 2.04)

2016-07-31-13-13 [scheduler_Worker-1] [com.alibaba.task.TestTask] [INFO] - 测试任务线程开始执行

2016-07-31-13-14 [scheduler_Worker-2] [com.alibaba.task.TestTask] [INFO] - 测试任务线程开始执行

4、demo源码

http://gd-rus-public.cn-hangzhou.oss-pub.aliyun-inc.com/attachment/201609/30/20160930140108/20160930quartz-springboot副本.zip

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值