Reputation: 263
I am trying to scheduling a job using Quartz job scheduler in a dropwizard application. In the class which implements Quartz Job interface, am injecting few of my service classes to use it's method. But it is not getting injected, getting null for that object. Someone helps me in this please?
JobManagerImpl class
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 class
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 implements IMyService and am binding IMyService to MyService implementation in my application class like bind(IMyservice.class).toInstance(new MyService());
Upvotes: 0
Views: 3042
Reputation: 7273
There's a question in the Featured tab of the quartz-scheduler tag right now, that talks exactly about DI in Quartz jobs.
The only useful answer so far states the following (link introduced by me for your convenience):
As Quartz manages its own threads, classes are not handled by CDI so you can not inject beans in Quartz classes. The aim of the deltaspike module is to allow injecting CDI beans in Quartz Jobs. Internally, deltaspike controls CDI Contexts.
Upvotes: 1