在Spring中将bean引用注入Quartz作业?
我设法在Spring中使用JobStoreTX持久存储来配置和调度Quartz作业。我不使用Spring的Quartz作业,因为我需要在运行时动态调度它们,并且我发现Spring与Quartz集成的所有示例都是对Spring配置文件中的shcedules进行硬编码...无论如何,这里是如何我安排工作:
JobDetail emailJob = JobBuilder.newJob(EMailJob.class).withIdentity("someJobKey", "immediateEmailsGroup").storeDurably().build();SimpleTrigger trigger = (SimpleTrigger) TriggerBuilder.newTrigger() .withIdentity("someTriggerKey", "immediateEmailsGroup").startAt(fireTime).build();// pass initialization parameters into the jobemailJob.getJobDataMap().put(NotificationConstants.MESSAGE_PARAMETERS_KEY, messageParameters);emailJob.getJobDataMap().put(NotificationConstants.RECIPIENT_KEY, recipient);if (!scheduler.checkExists(jobKey) && scheduler.getTrigger(triggerKey) != null) { // schedule the job to runDate scheduleTime1 = scheduler.scheduleJob(emailJob, trigger);}
EMailJob是一个简单的工作,它使用Spring的JavaMailSenderImpl类发送电子邮件。
public class EMailJob implements Job {@Autowiredprivate JavaMailSenderImpl mailSenderImpl; public EMailJob() { } public void execute(JobExecutionContext context) throws JobExecutionException { .... try { mailSenderImpl.send(mimeMessage); } catch (MessagingException e) { .... throw new JobExecutionException("EMailJob failed: " + jobKey.getName(), e); } logger.info("EMailJob finished OK");}
问题是我需要在我的EMailJob类中获得对该类的实例(JavaMailSenderImpl)的引用。当我尝试像这样注入它:
@Autowiredprivate JavaMailSenderImpl mailSenderImpl;
它没有被注入 - 引用是NULL。我假设发生了这种情况,因为实例化EMailJob类不是Spring,但是Quartz和Quartz对依赖注入一无所知......
那么,是否有某种方法可以强制进行这种注射?
谢谢!
精慕HU