定时任务quartz的job中注入spring bean时null的问题

定时任务quartz中的job注入spring bean时null的问题

在使用quartz作定时任务的时候难免会注入spring中的管理的bean,如果不作处理,就会出现java.lang.NullPointerException的异常

序言

我们知道quartz中实现一个定时任务有两种方法:

  • 实现Job接口
  • 继承QuartzJobBean

问题

spring-quartz.xml:

<bean id="scheduler" class="org.springframework.scheduling.quartz.SchedulerFactoryBean">
   <property name="triggers">
       <list>
           <ref bean="jobCron"/>
       </list>
   </property>
</bean>
<bean id="hwJob" class="org.springframework.scheduling.quartz.JobDetailFactoryBean">
   <property name="name" value="helloJob"/>
   <property name="group" value="group"/>
   <property name="jobClass" value="com.tian.task.HelloJob"/>
   <property name="durability" value="true"/>
</bean>

<bean id="jobCron" class="org.springframework.scheduling.quartz.CronTriggerFactoryBean">
   <property name="name" value="helloCron"/>
   <property name="group" value="trigger_group"/>
   <property name="jobDetail" ref="hwJob"/>
   <property name="cronExpression" value="0/1 * * * * ?"/>
</bean>

HelloJob.java:

@Component("helloJob")
public class HelloJob implements Job {

    private static final Logger LOGGER = LoggerFactory.getLogger(HelloJob.class);

    @Autowired
    private HelloService helloService;

    @Override
    public void execute(JobExecutionContext context) throws JobExecutionException {
        LOGGER.info("jos start...");
        LOGGER.info("hello job: "+ new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date()));
        helloService.doHelloService();
        LOGGER.info("jos end...");
    }
}

HelloService.java

@Service
public class HelloService {

    public void doHelloService(){
        System.out.println("invoke HelloService do helloService...");
    }

}

测试案例:

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = {
        "classpath:spring-context.xml",
        "classpath:mappings/**/*.xml"
})
public class HelloJobTest {

    @Test
    public void helloJobTest() throws InterruptedException {
        Thread.sleep(10000);
        System.out.println("done..");
    }

}

测试结果,经典的空指针异常:

[2018-08-14 09:35:29.474] [ INFO] HelloJob jos start...
[2018-08-14 09:35:29.474] [ INFO] HelloJob hello job: 2018-08-14 09:35:29
[2018-08-14 09:35:29.478] [ERROR] JobRunShell Job group.helloJob threw an unhandled Exception:
java.lang.NullPointerException: null
    at com.pinlor.tian.task.HelloJob.execute(HelloJob.java:31)
    at org.quartz.core.JobRunShell.run(JobRunShell.java:202)
    at org.quartz.simpl.SimpleThreadPool$WorkerThread.run(SimpleThreadPool.java:573)
[2018-08-14 09:35:29.479] [ERROR] ErrorLogger Job (group.helloJob threw an exception.

解决方案

通过继承SpringBeanJobFactory将产生的job注入到spring容器中,这样就能调用spring容器中的其他Bean

SpringQuartzJobFactory.java

@Component("springQuartzJobFactory")
public class SpringQuartzJobFactory extends SpringBeanJobFactory {

    @Autowired
    private AutowireCapableBeanFactory beanFactory;

    @Override
    protected Object createJobInstance(TriggerFiredBundle bundle) throws Exception {
        Object obj = super.createJobInstance(bundle);
        beanFactory.autowireBean(obj);
        return obj;
    }
}

修改spring-quartz.xml中的SchedulerFactoryBean增加属性jobFactorySpringQuartzJobFactory,其他的不变:

<bean id="scheduler" class="org.springframework.scheduling.quartz.SchedulerFactoryBean">
    <property name="jobFactory" ref="springQuartzJobFactory"/>
    <property name="triggers">
        <list>
            <ref bean="jobCron"/>
        </list>
    </property>
</bean>

测试结果:

[2018-08-14 10:04:02.002] [ INFO] HelloJob jos start...
[2018-08-14 10:04:02.002] [ INFO] HelloJob hello job: 2018-08-14 10:04:02
invoke HelloService do helloService...
[2018-08-14 10:04:02.002] [ INFO] HelloJob jos end...
done..
  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
当在 Job注入 Spring Bean ,需要使用 Spring 提供的 JobFactory 来创建 Job 实例,这样 JobBean 才能被正确注入。具体实现步骤如下: 1. 创建一个实现了 SpringJobFactory 接口的类,用于创建 Job 实例。 ```java import org.quartz.Job; import org.quartz.Scheduler; import org.quartz.SchedulerContext; import org.quartz.spi.JobFactory; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.context.ApplicationContext; import org.springframework.stereotype.Component; @Component public class AutowiringSpringBeanJobFactory implements JobFactory { @Autowired private ApplicationContext context; /** * {@inheritDoc} */ @Override public Job newJob(TriggerFiredBundle bundle, Scheduler scheduler) throws SchedulerException { final Job job = (Job) context.getBean(bundle.getJobDetail().getJobClass()); final SchedulerContext schedulerContext = scheduler.getContext(); schedulerContext.put("applicationContext", context); return job; } } ``` 2. 在 Quartz 配置注册 JobFactory,如下所示: ```java import javax.sql.DataSource; import org.quartz.JobDetail; import org.quartz.Scheduler; import org.quartz.Trigger; import org.quartz.impl.StdSchedulerFactory; import org.quartz.impl.jdbcjobstore.JobStoreTX; import org.quartz.impl.jdbcjobstore.PostgreSQLDelegate; import org.quartz.impl.jdbcjobstore.StdJDBCDelegate; import org.quartz.impl.jdbcjobstore.StdJDBCJobStore; import org.quartz.impl.jdbcjobstore.oracle.OracleDelegate; import org.quartz.spi.JobFactory; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Qualifier; import org.springframework.context.ApplicationContext; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.core.env.Environment; @Configuration public class QuartzConfig { @Autowired private Environment env; @Bean public JobFactory jobFactory(ApplicationContext applicationContext) { AutowiringSpringBeanJobFactory jobFactory = new AutowiringSpringBeanJobFactory(); jobFactory.setApplicationContext(applicationContext); return jobFactory; } @Bean public Scheduler scheduler(Trigger[] triggers, @Qualifier("quartzDataSource") DataSource dataSource) throws SchedulerException { StdSchedulerFactory factory = new StdSchedulerFactory(); factory.initialize(quartzProperties()); Scheduler scheduler = factory.getScheduler(); scheduler.setJobFactory(jobFactory); scheduler.setDataSource(dataSource); scheduler.setQuartzProperties(quartzProperties()); scheduler.start(); for (Trigger trigger : triggers) { scheduler.scheduleJob(trigger.getJobDetail(), trigger); } return scheduler; } @Bean public JobDetail jobDetail() { return JobBuilder.newJob().ofType(MyJob.class) .storeDurably().withIdentity("MyJob").withDescription("Invoke My Job service...").build(); } @Bean public Trigger trigger(JobDetail job) { return TriggerBuilder.newTrigger().forJob(job) .withIdentity("MyJobTrigger").withDescription("My Job trigger").withSchedule(CronScheduleBuilder.cronSchedule(env.getProperty("myjob.cron.expression"))).build(); } @Bean public Properties quartzProperties() { Properties properties = new Properties(); properties.setProperty(StdSchedulerFactory.PROP_SCHED_INSTANCE_NAME, env.getProperty("scheduler.instance.name")); properties.setProperty(StdSchedulerFactory.PROP_SCHED_INSTANCE_ID, env.getProperty("scheduler.instance.id")); properties.setProperty(StdSchedulerFactory.PROP_THREAD_POOL_CLASS, env.getProperty("scheduler.threadPool.class")); properties.setProperty(StdSchedulerFactory.PROP_THREAD_POOL_THREAD_COUNT, env.getProperty("scheduler.threadPool.threadCount")); properties.setProperty(StdSchedulerFactory.PROP_JOB_STORE_CLASS, env.getProperty("scheduler.jobStore.class")); properties.setProperty(StdSchedulerFactory.PROP_JOB_STORE_DRIVER_DELEGATE_CLASS, env.getProperty("scheduler.jobStore.driverDelegateClass")); properties.setProperty(StdSchedulerFactory.PROP_JOB_STORE_USE_PROPERTIES, env.getProperty("scheduler.jobStore.useProperties")); properties.setProperty(StdSchedulerFactory.PROP_JOB_STORE_DATASOURCE, env.getProperty("scheduler.jobStore.dataSource")); properties.setProperty(StdSchedulerFactory.PROP_JOB_STORE_TABLE_PREFIX, env.getProperty("scheduler.jobStore.tablePrefix")); properties.setProperty(StdSchedulerFactory.PROP_JOB_STORE_IS_CLUSTERED, env.getProperty("scheduler.jobStore.isClustered")); properties.setProperty(StdSchedulerFactory.PROP_JOB_STORE_CLUSTER_CHECKIN_INTERVAL, env.getProperty("scheduler.jobStore.clusterCheckinInterval")); properties.setProperty(StdSchedulerFactory.PROP_JOB_STORE_MAX_MISFIRES_TO_HANDLE_AT_A_TIME, env.getProperty("scheduler.jobStore.maxMisfiresToHandleAtATime")); properties.setProperty(StdSchedulerFactory.PROP_JOB_STORE_MISFIRE_THRESHOLD, env.getProperty("scheduler.jobStore.misfireThreshold")); return properties; } } ``` 3. 在 Job注入 Bean,如下所示: ```java import org.quartz.Job; import org.quartz.JobExecutionContext; import org.quartz.JobExecutionException; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.context.ApplicationContext; import org.springframework.stereotype.Component; @Component public class MyJob implements Job { @Autowired private MyService myService; /** * {@inheritDoc} */ @Override public void execute(JobExecutionContext context) throws JobExecutionException { ApplicationContext applicationContext = (ApplicationContext) context.getScheduler().getContext().get("applicationContext"); applicationContext.getAutowireCapableBeanFactory().autowireBean(this); myService.doSomething(); } } ``` 通过这种方式,就可以在 QuartzJob注入 Spring Bean。
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值