zoukankan      html  css  js  c++  java
  • Quartz总结(二):定时任务中使用业务类(XXService)

    零、引言

    上一篇文章:讲到了Spring集成Quartz的几种基本方法。

    在实际使用的时候,往往会在定时任务中调用某个业务类中的方法,此时使用QuartzJobBean和MethodInvokeJobDetailFactoryBean的区别就出来了。

    一、QuartzJobBean

    在继承QuartzJobBean的Job类中,使用XXService的时候,会报 空指针异常,原因是因为使用此方法的时候Job对象的创建时Quartz创建的,而XXXService是通过Spring创建的,两者不是同一个系统的,所以在Job类中使用由Spring管理的对象就会报空指针异常。

    其具体使用场景如下:

    public class TestJob extends QuartzJobBean {
    
        @Autowired
        private TestService testSevice;
        
        @Override
        protected void executeInternal(JobExecutionContext arg0) throws JobExecutionException {
            testSevice.sayHi();
            System.out.println(TimeUtils.getCurrentTime());
        }
    }

    解决方式就是替换系统默认的SchedulerFactory

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

    然后在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"
        xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
        
        <bean id="quartzFactory" class="com.mc.bsframe.job.BsQuartzJobFactory"></bean>
    
        <bean id="jobDetail" class="org.springframework.scheduling.quartz.JobDetailFactoryBean">
            <property name="jobClass" value="com.mc.bsframe.job.TestJob"></property>
            <property name="durability" value="true"></property>
        </bean>
    
        <bean id="simpleTrigger" class="org.springframework.scheduling.quartz.SimpleTriggerFactoryBean">
            <property name="jobDetail" ref="jobDetail" />
            <property name="startDelay" value="3000" />
            <property name="repeatInterval" value="2000" />
        </bean>
    
        <!-- 总管理类 如果将lazy-init='false'那么容器启动就会执行调度程序 -->
        <bean id="startQuertz" lazy-init="false" autowire="no" class="org.springframework.scheduling.quartz.SchedulerFactoryBean">
            <property name="jobFactory" ref="quartzFactory"></property>
            <!-- 管理trigger -->
            <property name="triggers">
                <list>
                    <ref bean="simpleTrigger" />
                </list>
            </property>
        </bean>
    </beans>

    二、使用MethodInvokeJobDetailFactoryBean

    因为对象的创建时Spring进行创建的,所以可以直接使用。基于此,推荐大家使用此种方式进行集成,方便,简单。

  • 相关阅读:
    值类型、引用类型作为方法参数如何执行,ref与out的区别
    asp.net 常用 验证正则表达式
    ASP.NET的错误处理机制
    MSSQL与MYSQL区别
    http协议状态码对照表
    EF 跨数据库支持
    请求管道中的19个事件
    一位软件工程师的6年总结本人读了深受启发,献给所有从事IT开发的人 [转载]
    Windows 窗体的.Net 框架绘图技术
    Windows Live Messenger 8.5 去广告方法及资源文件
  • 原文地址:https://www.cnblogs.com/LiuChunfu/p/5598626.html
Copyright © 2011-2022 走看看