samedi 28 mars 2015

Spring: Injecting bean in Quartz class


@Service
public class StartJob implements Job {

@Resource(name = "dataService")
private DataDriver dataService;

@Override
public void execute(JobExecutionContext jobContext) throws JobExecutionException {

dataService.startAdapter();

}

}


I am trying to implement Quartz with persistent storage in my spring based application. It should able to schedule the jobs at runtime however I am having problem to inject the dependency in the StartJob class. I am getting NPE in dataService.startAdapter(); line. This DataDriver is injected and working fine in other services and controller. But I am struggling to inject it in my Job class. What is wrong with this setup? Any help?


Scheduler:



JobDetail startJobDetail = JobBuilder.newJob(StartJob.class).withIdentity("StartJobName1", "group_1").build();

Trigger startTrigger = TriggerBuilder.newTrigger().withIdentity("TriggerName1", "group_1").withSchedule(CronScheduleBuilder.dailyAtHourAndMinute(hour, minute)).build();
Scheduler startScheduler;

try {

startScheduler = new StdSchedulerFactory().getScheduler();
startScheduler.start();
startScheduler.scheduleJob(startJobDetail, startTrigger);


} catch (SchedulerException e) {
LOG.error("Unable to schedule jobs. Exception: {}", e);
}

Aucun commentaire:

Enregistrer un commentaire