Fork me on GitHub

Guartz user's guide

Guartz provides the org.nnsoft.guice.guartz.QuartzModule that makes easier the org.quartz.Scheduler creation and org.quartz.Job creation and scheduling.

Scheduler

Once the Guice Injector will be created using the org.nnsoft.guice.guartz.QuartzModule, the org.quartz.Scheduler will be ready and started. Users can require the org.quartz.Scheduler injection for custom operations and for shutdown it.

public class MyApplicationShutdownListener
{

    @Inject
    private org.quartz.Scheduler scheduler;

    public void shutdown()
        throws Exception
    {
        ...
        scheduler.shutdown();
        ...
    }

}

Users that need to register listeners, can us the org.nnsoft.guice.guartz.QuartzModule EDSL:

Guice.createInjector(..., new org.nnsoft.guice.guartz.QuartzModule()
{

   @Override
   protected void schedule()
   {
       addJobListener( com.acme.MyJobListener.class );
       addTriggerListener( com.acme.MyTriggerListener.class );
       addSchedulerListener( com.acme.MySchedulerListener.class );
   }

});

Don't create listeners instances manually, let Guice do the job for you!

Job

Guartz manages as well org.quartz.Job instances and scheduling, a typical example of scheduling org.quartz.Job is:

Guice.createInjector(..., new org.nnsoft.guice.guartz.QuartzModule()
{

   @Override
   protected void schedule()
   {
       ...
       scheduleJob( com.acme.MyJobImpl.class ).withCronExpression( "0/2 * * * * ?" );
       ...
   }

});

Don't create jobs instances manually, let Guice do the job for you!

Please refer to JobSchedulerBuilder Javadoc to see all the scheduling configuration parameters.

Implicit scheduling

Job classes annotated with org.nnsoft.guice.guartz.Scheduled will be automatically scheduled extracting configuration parameters, i.e. given the :

@javax.inject.Singleton
@org.nnsoft.guice.guartz.Scheduled( jobName = "test", cronExpression = "0/2 * * * * ?" )
public class com.acme.MyJobImpl
    implements org.quartz.Job
{

    @javax.inject.Inject
    private MyCustomService service;

    public void execute( JobExecutionContext context )
        throws JobExecutionException
    {
        service.customOperation();
    }

}

Then, when creating the Injector instance:

Guice.createInjector(..., new org.nnsoft.guice.guartz.QuartzModule()
{

   @Override
   protected void schedule()
   {
       ...
       scheduleJob( com.acme.MyJobImpl.class );
       ...
   }

});

Starting the schedule manually

Since version 1.1, thanks to Pawel Poltorak, Guartz can be configured to let the Scheduler instance started manually.

That feature is incredibly useful due to serious deadlock issue found in the main Guice thread:

Guice.createInjector(..., new org.nnsoft.guice.guartz.QuartzModule()
{

   @Override
   protected void schedule()
   {
       ...
       configureScheduler().withManualStart();
       ...
   }

});

So users have to get the Scheduler instance and invoke the start explicitly.

Simple enough, uh?