public class Quartz extends Object implements Jooby.Module
Quartz Scheduler and jobs.
{
use(new Quartz().with(MyJob.class));
}
Previous example will startup Quartz and schedule MyJob.
A job can implement the Job interface as described in the Quartz documentation
If you prefer to not implement the Job interface, all you have to do is to annotated a
method with the Scheduled annotation.
By default, job name is set the class name or to the method name. Default group is set to the package name of the job class.
A job method must follow this rules:
JobExecutionContext
Trigger are defined by the Scheduled annotation. The annotation defines a single and
required attribute, which is basically a trigger expression or a reference to one.
Run every 5 minutes, start immediately and repeat for ever:
@Scheduled("5m")
@Scheduled("5m; delay=0")
@Scheduled("5m; delay=0; repeat=*")
Previous, expressions are identical.
Run every 1 hour with an initial delay of 15 minutes for 10 times
@Scheduled("1h; delay=15m; repeat=10")
Fire at 12pm (noon) every day
0 0 12 * * ?
Fire at 10:15am every day
0 15 10 ? * *
If you have two or more jobs doing something similar, it is possible to group all them into one single class:
public class MyJobs {
@Scheduled("5minutes")
public void job1() {
...
}
@Scheduled("1h")
public void job2() {
...
}
}
Not much to add here, just let you know jobs are created by Guice.
public class MyJob {
private A a;
@Inject
public MyJob(A a) {
this.a = a;
}
@Scheduled("5minutes")
public void doWork() {
this.a.doWork();
}
}
Injecting a Scheduler
public class MyJobManager {
private Scheduler scheduler;
@Inject
public MyJobManager(Scheduler scheduler) {
this.scheduler = scheduler;
}
}
Example: Setting max number of threads
# application.conf org.quartz.threadPool.threadCount = 1 # default is number of available processors
Configuration follows the Quartz
documentation. The only difference is that you need to put add the properties on your
*.conf file, NOT in a custom quartz.properties file.
Jdbc Store is fully supported but it depends on the jooby-jdbc module. So, in order
to use the Jdbc Store you need to follow these steps:
1. Install the Jdbc module:
{
use(new Jdbc());
use(new Quartz(MyJob.class));
}
2. Set the quartz properties:
org.quartz.jobStore.class = org.quartz.impl.jdbcjobstore.JobStoreTX org.quartz.jobStore.driverDelegateClass = org.quartz.impl.jdbcjobstore.StdJDBCDelegate org.quartz.jobStore.dataSource = db
When Scheduled isn't not enough and/or if you prefer to build jobs manually, you can try
one of the available alternatives.
Example 1: build the trigger and use default job naming
{
use(new Quartz()
.with(MyJob.class, trigger -> {
trigger
.withSchedule(withIntervalInDays(3))
.startAt(futureDate(10, MINUTES));
})
);
}
Example 2: build the job, the trigger and use default job naming
{
use(new Quartz()
.with(MyJob.class, (job, trigger) -> {
job.withDescription("etc...");
trigger
.withSchedule(withIntervalInDays(3))
.startAt(futureDate(10, MINUTES));
})
);
}
Example 3: build and set everything from scratch
{
use(new Quartz()
.with(
newJob(MyJob.class).withDescription("etc...")
.build(),
newTrigger()
.withSchedule(withIntervalInDays(3))
.startAt(futureDate(10, MINUTES))
.build()
})
);
}
Enjoy it!| Constructor and Description |
|---|
Quartz(Class<?>... jobs)
Creates a new
Quartz module. |
| Modifier and Type | Method and Description |
|---|---|
com.typesafe.config.Config |
config() |
void |
configure(Env env,
com.typesafe.config.Config config,
com.google.inject.Binder binder) |
Quartz |
with(Class<?> jobClass)
Setup and schedule the provided job, it might implement a
Job and the Scheduled
annotation must be present. |
Quartz |
with(Class<? extends org.quartz.Job> jobClass,
BiConsumer<org.quartz.JobBuilder,org.quartz.TriggerBuilder<org.quartz.Trigger>> configurer)
Schedule the provided job and trigger.
|
Quartz |
with(Class<? extends org.quartz.Job> jobClass,
Consumer<org.quartz.TriggerBuilder<org.quartz.Trigger>> configurer)
Schedule the provided job and trigger.
|
Quartz |
with(org.quartz.JobDetail job,
org.quartz.Trigger trigger)
Schedule the provided job and trigger.
|
public Quartz(Class<?>... jobs)
Quartz module. Optionally add some jobs.jobs - Jobs to setup. Optional.with(Class)public Quartz with(org.quartz.JobDetail job, org.quartz.Trigger trigger)
job - A job to schedule.trigger - A trigger for provided job.public Quartz with(Class<?> jobClass)
Job and the Scheduled
annotation must be present.jobClass - A jobClass to setup and schedule.public Quartz with(Class<? extends org.quartz.Job> jobClass, BiConsumer<org.quartz.JobBuilder,org.quartz.TriggerBuilder<org.quartz.Trigger>> configurer)
jobClass - A jobClass to setup and schedule.configurer - A callback to setup the job and trigger.public Quartz with(Class<? extends org.quartz.Job> jobClass, Consumer<org.quartz.TriggerBuilder<org.quartz.Trigger>> configurer)
jobClass - A jobClass to setup and schedule.configurer - A callback to setup the trigger.public void configure(Env env, com.typesafe.config.Config config, com.google.inject.Binder binder)
configure in interface Jooby.Modulepublic com.typesafe.config.Config config()
config in interface Jooby.ModuleCopyright © 2018. All rights reserved.