sping+quartz定时任务的最简单实践
2024-10-21 02:55:51
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;
}
}
最新文章
- Windows Azure Service Bus (2) 队列(Queue)入门
- saiku 无密码登陆
- POJ 1743 (后缀数组+不重叠最长重复子串)
- [转]freemarker中的list
- hibernate 连接 oracle数据库
- 3A. Shortest path of the king
- 防止aspx木马的IIS SPY变态功能
- Spring Boot(5)一个极简且完整的后台框架
- 数据库部分(MySql)_3
- python3之Django基础篇
- go标准库的学习-crypto/sha256
- Java HTML to PDF 支持SVG
- Java基础编程题——水仙花数
- Linux pwn入门教程——CTF比赛
- Linux服务器同步时间
- : error C3861: “Sleep”: 找不到标识符
- Elasticsearch查询规则(一)match和term
- keepalived安装文档
- 设置PYTHONIOENCODING
- Oracle彻底卸载