Also, I needed to get the injections working within my job. Also took me time to realize what to really search for.. My workaround was to pass the Injector object to the jobDataMap of the jobDetail.
jobDetail.getJobDataMap().put("Injector", jobFactory.guice);
And within my Job class, i can use the getInstance of the injector. :)
Injector guice = (Injector) arg0.getJobDetail().getJobDataMap().get("Injector");
So those objects that handle my Storage and some of my utils are done like this.
mailSender = guice.getInstance(MailSender.class);
loadersStorage = guice.getInstance(LoadersStorage.class);
Here's what my quartz code looks like.
QuartzScheduler
public class QuartzScheduler {
private final Scheduler scheduler;
@Inject
public QuartzScheduler(final SchedulerFactory factory, final GuiceJobFactory jobFactory) throws SchedulerException, ParseException {
scheduler = factory.getScheduler();
scheduler.setJobFactory(jobFactory);
//...
JobDetail jobDetail = new JobDetail("myJob", null, FileWatcherJob.class);
jobDetail.getJobDataMap().put("Injector", jobFactory.guice);
CronTrigger trigger = new CronTrigger("myCronTrigger", "cronGroup");
trigger.setCronExpression("your cron expression");
trigger.setStartTime(TriggerUtils.getEvenMinuteDate(new Date())); // start on the next even minute
trigger.setName("myTrigger");
scheduler.scheduleJob(jobDetail, trigger);
// scheduler.start();
}
public final Scheduler getScheduler() {
return scheduler;
}
public void shutdown() {
try {
scheduler.shutdown();
} catch (SchedulerException e) {
// ... handle it
}
}
public void start() {
try {
scheduler.start();
} catch (SchedulerException ex) {
Logger.getLogger(QuartzScheduler.class.getName()).log(Level.SEVERE, null, ex);
}
}
GuiceJobFactory
public class GuiceJobFactory implements JobFactory {
public final Injector guice;
@Inject
public GuiceJobFactory(final Injector guice) {
this.guice = guice;
}
public Job newJob(TriggerFiredBundle bundle) throws SchedulerException {
JobDetail jobDetail = bundle.getJobDetail();
jobDetail.getJobDataMap().put("Injector", guice);
Class jobClass = jobDetail.getJobClass();
Job job = (Job) guice.getInstance(jobClass);
guice.injectMembers(job);
return job;
}
}
Quartz Module
public class QuartzModule extends AbstractModule {
@Override
protected void configure() {
bind(SchedulerFactory.class).to(StdSchedulerFactory.class).in(Scopes.SINGLETON);
bind(GuiceJobFactory.class).in(Scopes.SINGLETON);
bind(QuartzScheduler.class).in(Scopes.SINGLETON);
}
}
My Job
public class FileWatcherJob implements Job {
//... declarations here
public void execute(JobExecutionContext arg0) throws JobExecutionException {
System.out.println("Execute");
try {
Injector guice = (Injector) arg0.getJobDetail().getJobDataMap().get("Injector");
mailSender = guice.getInstance(MailSender.class);
loadersStorage = guice.getInstance(LoadersStorage.class);
entityManagerFactory = guice.getInstance(EntityManagerFactory.class);
persistenceContextHelper = guice.getInstance(PersistenceContextHelper.class);
long newTimeStamp = file.lastModified();
if (FileWatcherJob.timeStamp != newTimeStamp) {
FileWatcherJob.timeStamp = newTimeStamp;
onChange(file);
}
} catch (NullPointerException ex) {
ex.printStackTrace();
}
}
public void onChange(File file) {
//some logic
}
//...
}
Glad the article has helped.
ReplyDeleteUwe from codesmell.org