Quartz Job允许自动注入Spring Bean

在上一篇Spring集成Quartz中,我们介绍了如何将Quartz框架集成到spring环境中来,在PrintJob示例中,我们仅仅是简单的做了Sleep操作。实际上,我们经常需要在具体的job中调用我们的Service服务,而服务通常是作为SpringBean方式托管的。

一种方案是通过ContextUtil每次去getBean,例如:

EchoService echoService = (EchoService)ContextUtil.getBean("echoService");

这些写实际上是有些别扭的,每次调用都要写这么一行代码,有时候getBean还可能导致NPE。实际上我们想要的是这样的效果:

public class PrintJob implements InterruptableJob {

   @Autowired
   EchoService echoService;
   ......
   }

能够进行自动注入,这样我们就能直接使用了。

我们先回过头来,spring集成quartz是通过spring-context-support包下的SchedulerFactoryBean实现的,配置如下:

<!-- quartz scheduler配置 -->
	<bean name="quartzScheduler"
		class="org.springframework.scheduling.quartz.SchedulerFactoryBean">
		<property name="dataSource" ref="dataSource" />
		<property name="configLocation" value="classpath:quartz.properties" />
		<property name="overwriteExistingJobs" value="true" />
		<property name="autoStartup" value="false" />
		<property name="exposeSchedulerInRepository" value="true" />
	</bean>

在该工厂类的初始化中,将创建Job的工厂类设置成了AdaptableJobFactory(在quartz框架内部默认是一个PropertySettingJobFactory的类):

@Override
	public void afterPropertiesSet() throws Exception {
		......
		// Get Scheduler instance from SchedulerFactory.
		try {
			this.scheduler = createScheduler(schedulerFactory, this.schedulerName);
			populateSchedulerContext();

			if (!this.jobFactorySet && !(this.scheduler instanceof RemoteScheduler)) {
				// Use AdaptableJobFactory as default for a local Scheduler, unless when
				// explicitly given a null value through the "jobFactory" bean property.
				*this.jobFactory = new AdaptableJobFactory();*
			}
			if (this.jobFactory != null) {
				if (this.jobFactory instanceof SchedulerContextAware) {
					((SchedulerContextAware) this.jobFactory).setSchedulerContext(this.scheduler.getContext());
				}
				this.scheduler.setJobFactory(this.jobFactory);
			}
		}

		......
	}

我们来看看AdaptableJobFactory是如何创建Job的:

package org.springframework.scheduling.quartz;

import org.quartz.Job;
import org.quartz.Scheduler;
import org.quartz.SchedulerException;
import org.quartz.spi.JobFactory;
import org.quartz.spi.TriggerFiredBundle;

public class AdaptableJobFactory implements JobFactory {

	@Override
	public Job newJob(TriggerFiredBundle bundle, Scheduler scheduler) throws SchedulerException {
		try {
			Object jobObject = createJobInstance(bundle);
			return adaptJob(jobObject);
		}
		catch (Exception ex) {
			throw new SchedulerException("Job instantiation failed", ex);
		}
	}

	protected Object createJobInstance(TriggerFiredBundle bundle) throws Exception {
		return bundle.getJobDetail().getJobClass().newInstance();
	}

	protected Job adaptJob(Object jobObject) throws Exception {
		if (jobObject instanceof Job) {
			return (Job) jobObject;
		}
		else if (jobObject instanceof Runnable) {
			return new DelegatingJob((Runnable) jobObject);
		}
		else {
			throw new IllegalArgumentException("Unable to execute job class [" + jobObject.getClass().getName() +
					"]: only [org.quartz.Job] and [java.lang.Runnable] supported.");
		}
	}

}

AdaptableJobFactory做了两件事:
1-根据JobClass创建实例(非常简单粗暴的创建实例)
2-对该实例进行代理(如果该实例是Runnable接口的实现类,则返回一个静态代理类)

那么如果我们想要自动注入,从createJobInstance着手就可以了。spring提供了一个AutowireCapableBeanFactory的类,可以对spring外部创建的实例进行属性注入。我们先来改造一个JobFactory。

/**
 * jobFactory,让Job可以进行spring属性自动注入
 * Created by gameloft9 on 2019/4/9.
 */
@Component(value = "springJobFactory")
public class SpringJobFactory extends AdaptableJobFactory {

    @Autowired
    private AutowireCapableBeanFactory capableBeanFactory;

    @Override
    protected Object createJobInstance(TriggerFiredBundle bundle) throws Exception {
        //创建job实例
        Object jobInstance = super.createJobInstance(bundle);
        //进行注入
        capableBeanFactory.autowireBean(jobInstance);

        return jobInstance;
    }
}

然后将其作为SchedulerFactoryBean的默认Job工厂类:

<!-- quartz scheduler配置 -->
	<bean name="quartzScheduler"
		class="org.springframework.scheduling.quartz.SchedulerFactoryBean">
		<property name="dataSource" ref="dataSource" />
		<property name="jobFactory" ref="springJobFactory"/>
		<property name="configLocation" value="classpath:quartz.properties" />
		<property name="overwriteExistingJobs" value="true" />
		<property name="autoStartup" value="false" />
		<property name="exposeSchedulerInRepository" value="true" />
	</bean>

这样就大功告成了。
下面我们来测试一下,先创建一个Service:

/**
 * 模拟spring bean
 * Created by gameloft9 on 2019/4/9.
 */
@Component
@Slf4j
@Data
public class EchoService {

    public void echo(String msg){
        log.info("echo {}",msg);
    }
}

然后在我们的PrintJob中通过@Autowired自动注入:

package com.gameloft9.demo.jobs;

import com.gameloft9.demo.service.EchoService;
import com.gameloft9.demo.util.ContextUtil;
import lombok.Data;
import lombok.extern.slf4j.Slf4j;
import org.quartz.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.quartz.QuartzJobBean;
import org.springframework.stereotype.Component;

/**
 * job示例
 * Created by gameloft9 on 2019/4/8.
 */
@DisallowConcurrentExecution
@PersistJobDataAfterExecution
@Slf4j
@Data
public class PrintJob implements InterruptableJob {

    @Autowired
    EchoService echoService;

    public void execute(JobExecutionContext context) throws JobExecutionException {
        try {
            String countStr = context.getJobDetail().getJobDataMap().getString("count");
            long count = 0;
            if (countStr != null){
                count = Long.parseLong(countStr);
            }

            context.getJobDetail().getJobDataMap().put("count", "" + (count + 1));

            // 模拟任务执行
            echoService.echo("执行任务中...");

            log.info("任务执行成功,累计执行次数:{}",count);
        } catch (Exception e) {
            log.error("", e);
        } finally {
        }
    }

    public void interrupt() throws UnableToInterruptJobException {
        // do nothing
    }

}

运行结果

在这里插入图片描述

  • 2
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
当在 Job 类中注入 Spring Bean 时,需要使用 Spring 提供的 JobFactory 来创建 Job 实例,这样 Job 类中的 Bean 才能被正确注入。具体实现步骤如下: 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、付费专栏及课程。

余额充值