定时任务Quartz_spring的配置文件
来源:互联网 发布:俄罗斯作家知乎 编辑:程序博客网 时间:2024/06/08 10:17
Quartz定时任务整合spring的配置文件(applicationContext.xml)
<?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:tx="http://www.springframework.org/schema/tx"
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.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd">
<context:component-scan base-package="com.douyu.quartz"/>
<!-- quartz_spring的配置 -->
<!-- job -->
<bean id="helloJob" class="org.springframework.scheduling.quartz.JobDetailFactoryBean">
<property name="jobClass" value="com.douyu.quartz.HelloJob"></property>
</bean>
<!-- trigger -->
<bean id="simpleTrigger" class="org.springframework.scheduling.quartz.SimpleTriggerFactoryBean">
<property name="jobDetail" ref="helloJob"/>
<!-- 从当前时间开始 -->
<property name="startDelay" value="0"/>
<property name="repeatInterval" value="3000"/>
</bean>
<!-- <bean id="cronTrigger" class="org.springframework.scheduling.quartz.CronTriggerFactoryBean">
<property name="jobDetail" ref="helloJob"/>
<property name="cronExpression" value="0/3 * * ? * *"></property>
</bean> -->
<!-- scheduler -->
<bean class="org.springframework.scheduling.quartz.SchedulerFactoryBean">
<property name="jobFactory" ref="jobFactory"></property>
<property name="triggers">
<list>
<!-- <ref bean="cronTrigger"/> -->
<ref bean="simpleTrigger"/>
</list>
</property>
</bean>
</beans>
web.xml的配置
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://java.sun.com/xml/ns/javaee"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
id="WebApp_ID" version="2.5">
<welcome-file-list>
<welcome-file>index.html</welcome-file>
<welcome-file>index.htm</welcome-file>
<welcome-file>index.jsp</welcome-file>
<welcome-file>default.html</welcome-file>
<welcome-file>default.htm</welcome-file>
<welcome-file>default.jsp</welcome-file>
</welcome-file-list>
<!-- spring配置文件位置 -->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:applicationContext.xml</param-value>
</context-param>
<!-- spring的核心监听器 -->
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
</web-app>
解决Spring+Quartz无法自动注入bean问题
package com.douyu.quartz.service;import org.quartz.spi.TriggerFiredBundle;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.config.AutowireCapableBeanFactory;
import org.springframework.scheduling.quartz.AdaptableJobFactory;
import org.springframework.stereotype.Service;
@Service("jobFactory")
public class JobFactory extends AdaptableJobFactory{
@Autowired
private AutowireCapableBeanFactory capableBeanFactory;
@Override
protected Object createJobInstance(TriggerFiredBundle bundle)
throws Exception {
Object jobInstance = super.createJobInstance(bundle);
capableBeanFactory.autowireBean(jobInstance);
return jobInstance;
}
}
另外一种解决方案
在Job类的excute方法下写入
SpringBeanAutowiringSupport.processInjectionBasedOnCurrentContext(this);
这样,在job类中使用@autowired 就可以完成jobDetail的注入;这样在applicationContext.xml的scheduler配置中也不用在注入jobFactory
- 定时任务Quartz_spring的配置文件
- spring 定时任务的配置文件解析
- Spring的quart(定时任务)配置文件
- spring schedule定时任务(二):配置文件的方式
- 定时任务 Quartz调度(采用配置文件)
- 定时任务 Quartz调度(采用配置文件)
- Spring配置定时任务,时间放到配置文件
- 定时任务的执行
- spring的定时任务
- oracle的定时任务
- mysql的定时任务
- 定时任务的执行
- spring的定时任务
- ExtJS的定时任务
- spring的定时任务
- Spring的定时任务
- android的定时任务
- 定时任务的实现
- anime.js 实战:实现一个带有描边动画效果的复选框
- 3年程序员应具备的技能(转)
- Advanced Programming in UNIX Environment Episode 20
- bzoj2424 [HAOI2010]订货 费用流
- Hive 简介
- 定时任务Quartz_spring的配置文件
- net获取两个日期之间的天数和工作日数总结
- [Linux] Windows 和 Linux 远程桌面连接
- 单片机有很多种
- Two Semiknights Meet CodeForces
- 什么是php单例模式?
- for循环的简介及break和continue的区别
- 解决springBoot无法访问controller
- DNS、网关介绍