sping+quartz定时任务的最简单实践
1,启动spring容器
Tomcat启动的时候,加载web.xml的listener和context-param,spring的listener监听到对应的contextConfigLocation创建事件后,开始启动spring容器;
<listener>
<description>spring监听器</description>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:spring.xml</param-value>
</context-param>
2,初始化定时任务相关的bean

3,初始化 org.springframework.scheduling.quartz.SchedulerFactoryBean
参考 https://blog.csdn.net/beliefer/article/details/51578546 ;
<bean class="org.springframework.scheduling.quartz.SchedulerFactoryBean">
<property name="triggers">
<list>
<ref bean="hitBeibeiTrigger" />
<ref bean="hitBeibeiTrigger2" />
</list>
</property>
<property name="configLocation" value="classpath:quartz.properties"/>
</bean>
4,初始化 org.springframework.scheduling.quartz.CronTriggerFactoryBean
<bean id="hitBeibeiTrigger"
class="org.springframework.scheduling.quartz.CronTriggerFactoryBean">
<property name="jobDetail" ref="hitBeibeiJob"/>
<property name="cronExpression" value="0/30 * * * * ?"/>
</bean> <bean id="hitBeibeiTrigger2"
class="org.springframework.scheduling.quartz.CronTriggerFactoryBean">
<property name="jobDetail" ref="hitBeibeiJob"/>
<property name="cronExpression" value="0/30 * * * * ?"/>
</bean>
5,初始化 org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean
concurrent属性:true/false;参考 https://www.cnblogs.com/seeall/p/12084778.html;
<bean id="hitBeibeiJob"
class="org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean">
<property name="targetObject" ref="hitBeibeiScheduler"/>
<property name="targetMethod" value="hit"/>
<property name="concurrent" value="true"/>
</bean>
6,hitBeibeiScheduler bean介绍
/**
* 用于测试定时任务:spring+quartz
*
* date 2019/12/23
*/
@Service(value = "hitBeibeiScheduler")
public class HitBeibeiScheduler implements ApplicationContextAware {
private static final Logger LOGGER = LoggerFactory.getLogger(HitBeibeiScheduler.class); private ApplicationContext applicationContext; // 执行job
public void hit() throws InterruptedException {
LOGGER.info("现在时间是" + DateUtil.dateToString(new Date(), "yyyy-MM-dd HH:mm:ss") + ", 打贝贝...");
Thread.sleep(60000);
LOGGER.info("现在时间是" + DateUtil.dateToString(new Date(), "yyyy-MM-dd HH:mm:ss")
+ ", 过去了60秒...");
} @Override
public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
this.applicationContext = applicationContext;
}
}
sping+quartz定时任务的最简单实践的更多相关文章
- Spring Boot 入门(九):集成Quartz定时任务
本片文章续<Spring Boot 入门(八):集成RabbitMQ消息队列>,关于Quartz定时任务请参考<Quartz的基本使用之入门(2.3.0版本)> spring ...
- Quartz定时任务学习(二)web应用/Quartz定时任务学习(三)属性文件和jar
web中使用Quartz 1.首先在web.xml文件中加入 如下内容(根据自己情况设定) 在web.xml中添加QuartzInitializerServlet,Quartz为能够在web应用中使用 ...
- Quartz定时任务学习(二)web应用
web中使用Quartz 1.首先在web.xml文件中加入 如下内容(根据自己情况设定) 在web.xml中添加QuartzInitializerServlet,Quartz为能够在web应用中使用 ...
- Quartz定时任务使用小记(11月22日)
骤然接触quartz,先从小处着手,why,what,how quartz定时任务: 为什么使用quartz定时任务,以及定时任务在实际应用场景下的特定需求. 1.用户方面的需要,为了提供更好的使用体 ...
- 对quartz定时任务的初步认识
已经好久没有写技术博文了,今天就谈一谈我前两天自学的quartz定时任务吧,我对quartz定时任务的理解,就是可以设定一个时间,然后呢,在这个时间到的时候,去执行业务逻辑,这是我的简单理解,接下来看 ...
- Java学习---Quartz定时任务快速入门
Quartz是OpenSymphony开源组织在Job scheduling领域又一个开源项目,它可以与J2EE与J2SE应用程序相结合也可以单独使用.Quartz可以用来创建简单或为运行十个,百个, ...
- Spring quartz定时任务service注入问题
今天想单元测试一下spring中的quartz定时任务,job类的大致结构和下面的SpringQtz1类相似,我的是实现的org.quartz.Job接口,到最后总是发现job类里注入的service ...
- Thrift简单实践
0.什么是RPC RPC(Remote Procedure Call - 远程过程调用),是通过网络从远程计算机上请求服务,而不需要了解底层网路技术的细节.简单点说,就是像调用本地服务(方法)一样调用 ...
- Java 异步处理简单实践
Java 异步处理简单实践 http://www.cnblogs.com/fangfan/p/4047932.html 同步与异步 通常同步意味着一个任务的某个处理过程会对多个线程在用串行化处理,而异 ...
随机推荐
- Linux常用功能脚本
设置定时任务 crontab -e 1 0 * * * /bin/find /mnt/tomcat/logs/ -mtime +3 -type f -name "*.log" -e ...
- docker-ce创建gitlab-ce容器笔记
前言 vagrant + ubuntu 16.04 设置 apt 源 sudo cp /etc/apt/sources.list /etc/apt/sources.list.bak sudo vim ...
- k8s--网络模式
1.clusterip kind: Service apiVersion: v1 metadata: name: my-service spec: selector: app: nginx ports ...
- oracle死锁查询
select sess.sid ||','|| sess.serial#, lo.oracle_username, lo.os_user_name, ao.object_name, lo.locked ...
- [转]C# CancellationTokenSource 终止线程
我们在多线程中通常使用一个bool IsExit类似的代码来控制是否线程的运行与终止,其实使用CancellationTokenSource来进行控制更为好用,下面我们将介绍CancellationT ...
- 数据结构---Java---HashMap---JDK1.7
源码解读 public class HashMap<K,V> extends AbstractMap<K,V> implements Map<K,V>, Clone ...
- noi2019感想
不知道怎么想的,我现在已经没有心情写一篇完整的游记了. 发挥的是真的太差,Day1该切的T2没有切掉,想的时候漏了一个性质,便由100->45. Day1的时间全花在了T3上,结果想歪了,最后只 ...
- PHP curl_multi_info_read函数
curl_multi_info_read — 获取当前解析的cURL的相关传输信息 说明 array curl_multi_info_read ( resource $mh [, int &$ ...
- 微信公众号的SpringBoot+Quartz的定时任务Demo
SpringBoot整合quartz并不难,难在普通类实现了Job接口后等于实例化交给quartz,不受Spring管理,则service层等等其他依赖的注入将无法注入,这也是难点之一. 解决方法: ...
- python random模块随机取list中的某个值
import random from random import randint ''' random.randint()随机生一个整数int类型,可以指定这个整数的范围,同样有上限和下限值,pyth ...