为了账号安全,请及时绑定邮箱和手机立即绑定

在Spring中将bean引用注入Quartz作业?

在Spring中将bean引用注入Quartz作业?

慕姐8265434 2019-08-12 10:03:23
在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对依赖注入一无所知......那么,是否有某种方法可以强制进行这种注射?谢谢!
查看完整描述

3 回答

?
精慕HU

TA贡献1845条经验 获得超8个赞

我只是SpringBeanAutowiringSupport.processInjectionBasedOnCurrentContext(this);作为我的Job.execute(JobExecutionContext context)方法的第一行。


查看完整回答
反对 回复 2019-08-12
  • 3 回答
  • 0 关注
  • 745 浏览

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信