zoukankan      html  css  js  c++  java
  • spring整合quartz时间任务调度框架

    spring整合quartz框架

    1.创建maven工程

    2.导入jar包(pom.xml)

     <dependencies>
      		<dependency>
    			<groupId>org.quartz-scheduler</groupId>
    			<artifactId>quartz</artifactId>
    			<version>2.2.1</version>
    		</dependency>
    		<dependency>
    			<groupId>org.quartz-scheduler</groupId>
    			<artifactId>quartz-jobs</artifactId>
    			<version>2.2.1</version>
    		</dependency>
    		<dependency>
    			<groupId>org.slf4j</groupId>
    			<artifactId>slf4j-log4j12</artifactId>
    			<version>1.7.12</version>
    		</dependency>
    
    		<dependency>
    			<groupId>org.springframework</groupId>
    			<artifactId>spring-context</artifactId>
    			<version>4.1.7.RELEASE</version>
    		</dependency>
    		<dependency>
    			<groupId>org.springframework</groupId>
    			<artifactId>spring-context-support</artifactId>
    			<version>4.1.7.RELEASE</version>
    		</dependency>
    		<dependency>
    			<groupId>org.springframework</groupId>
    			<artifactId>spring-web</artifactId>
    			<version>4.1.7.RELEASE</version>
    		</dependency>
    		<dependency>
    			<groupId>org.springframework</groupId>
    			<artifactId>spring-tx</artifactId>
    			<version>4.1.7.RELEASE</version>
    		</dependency>
      </dependencies>
      <build>
    		<plugins>
    			<plugin>
    				<groupId>org.codehaus.mojo</groupId>
    				<artifactId>tomcat-maven-plugin</artifactId>
    				<version>1.1</version>
    				<configuration>
    					<port>9003</port>
    				</configuration>
    			</plugin>
    		</plugins>
    	</build>

    3.创建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">
    	<!-- 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>
    	<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>
    </web-app>

    4.创建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"
    	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 ">
     	
     	<context:component-scan base-package="com.baidu" />
     	
     	<!-- job -->
     	<bean id="helloJob" 
     		class="org.springframework.scheduling.quartz.JobDetailFactoryBean">
              <!--不能使用ref--> <property name="jobClass" value="com.baidu.quartz.HelloJob" /> </bean> <!-- trigger --> <bean id="simpleTrigger" class="org.springframework.scheduling.quartz.SimpleTriggerFactoryBean"> <property name="jobDetail" ref="helloJob" /> <!-- 3秒后第一次执行 --> <property name="startDelay" value="3000" /> <!-- 5秒后重复执行 --> <property name="repeatInterval" value="5000" /> </bean> <!-- scheduler --> <bean class="org.springframework.scheduling.quartz.SchedulerFactoryBean"> <property name="triggers"> <list> <ref bean="simpleTrigger"/> </list> </property> </bean> </beans>

    5.编写代码

    创建一个service

    @Component
    public class HelloService {
    	public void sayHello(){
    		System.out.println("say hello");
    	}
    } 

    方法一:

    创建helloJob

    @Component
    public class HelloJob implements Job {
    	@Autowired
    	private HelloService helloService;
    	public void execute(JobExecutionContext context)
    			throws JobExecutionException {
    		SpringBeanAutowiringSupport.processInjectionBasedOnCurrentContext(this);
    		helloService.sayHello();
    		System.out.println("test01");
    	}
    }
    

      方法二:

    创建一个JobFactory类

    @Component
    public class JobFactory extends AdaptableJobFactory{
    	@Autowired
    	private AutowireCapableBeanFactory autowireCapableBeanFactory;
    	@Override
    	protected Object createJobInstance(TriggerFiredBundle bundle)
    			throws Exception {
    		Object jobInstance = super.createJobInstance(bundle);
    		autowireCapableBeanFactory.autowireBean(jobInstance);
    		return jobInstance;
    	}
    }
    

      修改applicationContext.xml文件

    <!-- scheduler  -->
     	<bean class="org.springframework.scheduling.quartz.SchedulerFactoryBean">
     	    <property name="jobFactory" ref="jobFactory"></property>
     		<property name="triggers">
     			<list>
     				<ref bean="simpleTrigger"/>
     			</list>
     		</property>
     	</bean>
    

      

     产生空指针异常

    产生的原因:    

    quartz交给spring管理时,底层对于jobclass的管理并没有交给spring,这样在使用spring管理时会产生连个对象,

  • 相关阅读:
    ThinkPHP实现定时任务
    VUE 父子组件之间通信传值 props和 $emit,事件触发传值ref,以及兄弟组件之间的通信传值 eventBus
    JS链接转换为二维码
    VUE 动态切换列表active样式
    微信内置浏览器video标签自动全屏的问题
    JS监听video视频播放时间
    JS数据统计表 highcharts.js的运用
    JS 自动返回每个月的天数
    JS 一键复制插件应用 和 原生实现
    JS enter键一键登录
  • 原文地址:https://www.cnblogs.com/fjkgrbk/p/spring_quartz.html
Copyright © 2011-2022 走看看