quartz2 与spring4 集成 定时任务Job

 

 1.使用spring4.1 与quartz2.1集成

 

 a. Job实现类

 

package job;

import hello.MessagePrinter;
import java.text.SimpleDateFormat;
import java.util.Date;

public class MyJob {
 
 private MessagePrinter messagePrinter;
 
 private void init(){
  messagePrinter = (MessagePrinter)SpringContextHolder.getBean(MessagePrinter.class);
 }

 public void execute(){
  System.out.println((new SimpleDateFormat("yyyy-MM-dd hh:mm:ss:SSS")).format(new Date()));
 }
 
 public void execute2(){
  init();
  
  messagePrinter.printMessage();
  System.out.println((new SimpleDateFormat("yyyy-MM-dd hh:mm:ss:SSS")).format(new Date()));
 }
}

b.applicationContext-quartz.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:p="http://www.springframework.org/schema/p" xmlns:aop="http://www.springframework.org/schema/aop"
 xmlns:tx="http://www.springframework.org/schema/tx"
 xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
    http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.0.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.0.xsd">
   
   
 <!-- 定时任务Bean -->
 <bean id="myJob" class="job.MyJob" />
 

 <!-- 定义调用对象和调用对象的方法 -->
 <bean id="myJobDetail"
  class="org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean">
  <!-- 调用的类 -->
  <property name="targetObject" ref="myJob" />
  <!-- 调用类中的方法 -->
  <property name="targetMethod" value="execute" />
  <!-- 是否允许任务并发执行。当值为false时,表示必须等到前一个线程处理完毕后才再启一个新的线程 -->
  <property name="concurrent" value="false" />
 </bean>
 

 <!-- 定义触发时间 -->
 <!-- quartz-1.8以前的配置 -->
 <!--
 <bean id="myJobTrigger" class="org.springframework.scheduling.quartz.CronTriggerBean">
  <property name="jobDetail">
   <ref bean="testDetail" />
  </property>
  <property name="cronExpression">
   <value>0/10 * * * * ?</value>
  </property>
 </bean>
  -->

 <!-- quartz-2.x后的配置 -->
 <bean id="myJobTrigger"
  class="org.springframework.scheduling.quartz.CronTriggerFactoryBean">
  <property name="jobDetail">
   <ref bean="myJobDetail" />
  </property>
  <property name="cronExpression">
   <value>0/10 * * * * ?</value>
  </property>
 </bean>
 

 <!-- 总管理类:如果lazy-init='false',那么容器启动后就会执行调度程序 -->
 <!-- 如果lazy-init='true',则需要实例化该bean才能执行调度程序 -->
 <bean name="testQuartz" lazy-init="false" autowire="no"
  class="org.springframework.scheduling.quartz.SchedulerFactoryBean">
  <property name="triggers">
   <list>
    <ref bean="myJobTrigger" />
    <ref bean="myJobTrigger2" />
   </list>
  </property>
  <!-- <property name="autoStartup" value="true"/> -->
 </bean>

 
 
 
 
 <!-- 配置MyJob的第二个方法 -->
 <!-- 定义调用对象和调用对象的方法 -->
 <bean id="myJobDetail2"
  class="org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean">
  <!-- 调用的类 -->
  <property name="targetObject" ref="myJob" />
  <!-- 调用类中的方法 -->
  <property name="targetMethod" value="execute2" />
  <!-- 是否允许任务并发执行。当值为false时,表示必须等到前一个线程处理完毕后才再启一个新的线程 -->
  <property name="concurrent" value="false" />
 </bean>
 <bean id="myJobTrigger2"
  class="org.springframework.scheduling.quartz.CronTriggerFactoryBean">
  <property name="jobDetail">
   <ref bean="myJobDetail2" />
  </property>
  <property name="cronExpression">
   <value>0/10 * * * * ?</value>
  </property>
 </bean>

</beans>

 

3.获取applicationContext实例:

 

import java.util.Map;

import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;

/**
 * 实现 ApplicationContextAware 获取 ApplicationContext spring容器实例
 * @author
 *
 */
public class SpringContextHolder implements ApplicationContextAware {
 
 private static ApplicationContext applicationContext;

 @Override
 public void setApplicationContext(ApplicationContext applicationContext)
   throws BeansException {
  SpringContextHolder.applicationContext=applicationContext;
 }

 public static ApplicationContext getApplicationContext(){
  return applicationContext;
 }
 
 @SuppressWarnings("unchecked")
 public static <T>  T getBean(String name){
  if(applicationContext==null)
   return null;
  return (T)applicationContext.getBean(name);
 }
 
 @SuppressWarnings("unchecked")
 public static <T> T getBean(Class<T> clazz){
  if(applicationContext==null)
   return null;
   @SuppressWarnings("rawtypes")
   Map beanMaps = applicationContext.getBeansOfType(clazz);
   if(!(beanMaps==null || beanMaps.isEmpty())){
   return (T)beanMaps.values().iterator().next();
   }
   return null;
 }
}

   spring4 jar包下载:http://repo.springsource.org/libs-release-local/org/springframework/spring/4.1.6.RELEASE/

 

 

 

你可能感兴趣的:(quartz job)