我正在尝试使用dropwizard应用程序中的Quartz作业调度程序来安排作业。在实现Quartz Job接口的类中,我注入了很少的服务类来使用它的方法。但它没有被注入,为该对象获取null。有人帮我这个吗?
JobManagerImpl类
public class MyJobManagerImpl {
private SchedulerFactory schedulerFactory;
private Scheduler scheduler;
private static final String group = "REFRESH";
private static final Logger logger = LoggerFactory.getLogger(TcConnectionRefreshJobManagerImpl.class);
public MyJobManagerImpl(Properties quartzConfig) {
try {
schedulerFactory = new StdSchedulerFactory(quartzConfig);
scheduler = schedulerFactory.getScheduler();
scheduler.start();
} catch (SchedulerException e) {
logger.error("Error starting scheduler", e);
}
}
public boolean addJob(String name, int cronHour, int cronMinute) throws SchedulerException {
JobDetail jobDetail = newJob(TcConnectionRefreshJob.class).withIdentity(name, group)
.requestRecovery().build();
String cronString = "0 " + cronMinute+" "+cronHour+" ? * *" ;
CronTrigger cronTrigger = newTrigger().withIdentity(name, group).withSchedule(cronSchedule(cronString).
withMisfireHandlingInstructionDoNothing()).build();
scheduler.scheduleJob(jobDetail, cronTrigger);
return true;
}
public boolean deleteJob(String name) {
JobKey jobKey = JobKey.jobKey(name,group);
TriggerKey triggerKey = TriggerKey.triggerKey(name,group);
try {
scheduler.unscheduleJob(triggerKey);
scheduler.deleteJob(jobKey);
} catch (SchedulerException e) {
logger.error("Exception occurred "+e);
return false;
}
return true;
}
ScheduledJob类
public class ScheduledJob implements Job {
public static IMyService myService;
@Override
public void execute(JobExecutionContext jobExecutionContext) throws JobExecutionException {
myService.executeAction();
}
@SuppressWarnings("static-access")
@Inject
public void setMyService(IMyService myService) {
this.myService = myService;
}
MyService实现IMyService并将IMyService绑定到我的应用程序类中的MyService实现,如bind(IMyservice.class).toInstance(new MyService());
答案 0 :(得分:1)
目前question标记的Featured标签中有一个quartz-scheduler,它正好说明了Quartz作业中的DI。
到目前为止唯一有用的答案说明了以下内容(为方便起见,我介绍的链接):
由于Quartz管理自己的线程,因此CDI不会处理类,因此您无法在Quartz类中注入bean。 deltaspike module的目的是允许在Quartz Jobs中注入CDI bean。在内部,deltaspike控制CDI上下文。