Using Guice with Quartz

1,256 views
Skip to first unread message

Rich

unread,
Apr 2, 2007, 10:34:00 AM4/2/07
to google-guice
Hi,

I would like Guice to inject into implementations of Quartz's Job
interface, so that the jobs have access to the injected members.

Does anyone have any code samples for doing this, or any pointers? I'm
quite new to Quartz, so I don't know whether there is something to
extend in order to get Guice to do the injection...

Perhaps there is a beforeExecute() method that I can override
somewhere?

Thanks in advance

Rich

ChrisCY

unread,
May 1, 2007, 4:40:40 AM5/1/07
to google-guice
Hi,

I've only used Quartz once, and just for trivial things, but by
looking a bit at the API you can find the org.quartz.spi.JobFactory
interface.

You can create a custom JobFactory, provide it a reference of the
injector and then inject dependencies on the newly created Job object:

public class GuiceJobFactory implements JobFactory {

private final Injector injector;

public GuiceJobFactory(Injector inj) {
injector = inj;
}

public Job newJob(TriggerFiredBundle triggerFiredBundle) throws
SchedulerException {

Job job = null;

try {
job= (Job)
triggerFiredBundle.getJobDetail().getJobClass().newInstance();
} catch (Exception ex) {
throw new SchedulerException(ex);
}

injector.injectMembers(job);

return job;
}

You can then provide a new JobFactory to your scheduler through a
Provider:

public class SchedulerProvider implements Provider<Scheduler> {

@Inject Injector injector;

public Scheduler get() {
try {
org.quartz.impl.StdSchedulerFactory factory = new
org.quartz.impl.StdSchedulerFactory();
org.quartz.Scheduler scheduler = factory.getScheduler();

scheduler.setJobFactory(new GuiceJobFactory(injector));
return scheduler;
}
catch (SchedulerException ex) {
...
}
}

}

Finally, you can configure the Scheduler through a Module:


bind(Scheduler.class).toProvider(SchedulerProvider.class).asEagerSingleton();

I used "asEagerSingleton" to force the instanciation and configuration
of the Scheduler at deployment, and get all the possible configuration
errors as soon as possible.

I haven't actually used this method (I haven't used Guice for anything
yet! :D ) but a small experiment I did seems to work. I hope that
makes sense to you.

Let me know if I am diverging form Guice or Quartz filosophy.

Christophoros

Kevin Bourrillion

unread,
May 1, 2007, 4:58:28 AM5/1/07
to google...@googlegroups.com
Cool. 

I'd simplify just a bit further.

1. Put @Inject on your GJF constructor, then inject a GJF into your SP instead of the Injector.
2. Also have StdSchedulerFactory injected into SP.
3. In newJob(), using injector.getInstance(c) instead of using Class.newInstance() followed by injectMembers().  (You may have to cast the result of getJobClass() to a Class<? extends Job> to get this to work.

But it looks good.  (Not that I know anything about Quartz.)

K

har...@knoldus.com

unread,
Sep 12, 2016, 6:59:13 AM9/12/16
to google-guice
Reply all
Reply to author
Forward
0 new messages