• spring整合Quartz


    整合前的概述:与spring整合实质上就是把quartz的内部实现交给spring管理,很好的体现的spring的价值所在。

    1.既然要用到spring,必要的步骤就是在web.xml中配置监听器器和加载核心配置文件

     <!-- 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>

    2.首先在Spring核心配置文件中applicationContext.xml文件中配置三个重要的对象,分别是JobDetailFactoryBean,trigger,Scheduler:将trigger交给大管家

      <?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">
           
          <!-- Spring与Quartz的配置 -->
          
          
     <!-- 第一步:配置JobDetail -->
         <bean id="helloJobDetail" class="org.springframework.scheduling.quartz.JobDetailFactoryBean">
             <!-- 注入job的类型 -->
             <property name="jobClass" value="cn.quartz.job.HelloJob" />
         </bean>
         <!-- 第二步:配置trigger :简单任务-->
         <bean id="simpleTrigger" class="org.springframework.scheduling.quartz.SimpleTriggerFactoryBean">
             <!-- 注入 JobDetail任务-->
             <property name="jobDetail" ref="helloJobDetail" />
             <!-- 服务启动后第一次执行的间隔时间:毫秒 -->
             <property name="startDelay" value="5000" />
             <!-- 每隔多长时间执行一次 -->
             <property name="repeatInterval" value="2000" />
         </bean>
         <!-- 第三步:配置Scheduler -->
         <bean class="org.springframework.scheduling.quartz.SchedulerFactoryBean">
         
         <!-- 注入覆盖默认的jobFactory -->
             <property name="jobFactory" ref="jobFactory"/>    
         
             <property name="triggers">
                 <list>
                     <!-- 注入trigger -->
                     <ref bean="simpleTrigger" />
                 </list>
             </property>
         </bean>
                
          <!-- 注解扫描 -->
          <context:component-scan base-package="cn"></context:component-scan>
     </beans>

    3.编写任务对象(执行的任务继承Job),可以在该类里面调用业务层或者dao层

      package cn.quartz.job; 
      import org.quartz.Job;
      import org.quartz.JobExecutionContext;
      import org.quartz.JobExecutionException;
      import org.springframework.beans.factory.annotation.Autowired;
      import cn.service.QuartzService;
      
      public class MyJob implements Job{
         
         @Autowired
         private QuartzService quartzService;
         
         @Override
         public void execute(JobExecutionContext context) throws JobExecutionException {
     
     //        调用业务层或者dao层
             quartzService.say();
         }
     }

    4.将上面的任务对象转换为Bean,由Spring来调用

      package cn.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
         //将普通的java对象放入spring容器中变成bean
         private AutowireCapableBeanFactory capableBeanFactory;
     
         @Override
         protected Object createJobInstance(TriggerFiredBundle bundle) throws Exception {
             // 调用父类的方法:quartz基于配置job类型生产了一个job对象(没有在spring容器中)
             Object jobInstance = super.createJobInstance(bundle);
             // 将Quartz生产出来的job对象,注入到spring容器,该job对象就变成了bean
             capableBeanFactory.autowireBean(jobInstance);
             return jobInstance;
         }
     }

    5.需要在配置文件中Scheduler注入 jobFactory 

    <!-- 注入覆盖默认的jobFactory -->

    <property name="jobFactory" ref="jobFactory"/>

     

    今天要比昨天好
  • 相关阅读:
    C语言I博客作业08
    博客作业6
    C语言I博客作业05
    C语言I博客作业03
    5.C#2.0之不完整类型(完成)
    4.C#2.0之迭代器(完成)
    3.C#2.0之匿名方法(完成)
    2.C#2.0之泛型(完成)
    1.C#2.0之2.0简介(完成)
    18.C#基础之不安全代码(完成)
  • 原文地址:https://www.cnblogs.com/lichangyun/p/8424429.html
Copyright © 2020-2023  润新知