1. db-scheduler
Task scheduler module using db-scheduler.
1.1. Usage
1) Add the dependencies (hikari):
<!-- DataSource via HikariCP-->
<dependency>
<groupId>io.jooby</groupId>
<artifactId>jooby-hikari</artifactId>
<version>3.5.5</version>
</dependency>
<!-- Db Scheduler Module-->
<dependency>
<groupId>io.jooby</groupId>
<artifactId>jooby-db-scheduler</artifactId>
<version>3.5.5</version>
</dependency>
2) Add database driver (mySQL here):
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>${mysql-connector-java.version}</version>
</dependency>
3) Install DbScheduler. Add SampleJob:
import io.jooby.dbscheduler.DbSchedulerModule;
{
install(new HikariModule());
install(new DbSchedulerModule(Tasks.recurring(...)));
}
1.2. Tasks
Tasks are created as described in db-scheduler documentation. Optionally, you can annotate a method with the Scheduled annotation:
import io.jooby.dbscheduler.Scheduled;
public class SampleJob {
@Scheduled("1m")
public void everyMinute() {
...
}
}
Once you annotate your method you must create task from them with:
import io.jooby.dbscheduler.BeanTasks;
{
install(new HikariModule());
install(new DbSchedulerModule(BeanTasks.recurring(this, SampleJob.class)));
}
A task method must follow these rules:
-
Must be a public method
-
Possible arguments: none (zero),
TaskInstance
,ExecutionContext
,task data
or any other application service. -
Return value: Task can return a value, which is persisted by DbScheduler. This is known as task data or task state.
1.3. Scheduled
The Scheduled annotation supports simple and cron triggers as well as property references:
@Scheduled("1h")
@Scheduled("FIXED_DELAY|Ns")
@Scheduled("DAILY|12:30,15:30...(|time_zone)")
@Scheduled("0 0/5 * * * ?")
@Scheduled("10 0/5 * * * ?")
@Scheduled("mytask.trigger")
The mytask.trigger
must be defined in your application property file. It could be a any of previous expressions.
1.4. Configuration
Configuration from properties files is fully supported, just need to add DbSchedulerProperties properties to your application configuration file:
# Turn on/off scheduler.
db-scheduler.enabled = true
# Set number of threads to use, default is to use the number of available processor
db-scheduler.threads = 8
db-scheduler.pollingInterval = 10s
db-scheduler.alwaysPersistTimestampInUTC = true
db-scheduler.enableImmediateExecution = false
# No need to use registerShutdownHook, the scheduler is shutdown on application shutdown
db-scheduler.registerShutdownHook = false
db-scheduler.shutdownMaxWait = 1s
Check more configuration options at configuration
1.5. REST API
This modules comes with a simple REST API (sort of) to manage tasks:
import io.jooby.dbscheduler.DbSchedulerApp;
import io.jooby.dbscheduler.DbSchedulerModule;
{
install(new DbScheduler(SampleJob.class));
mount("/scheduler", new DbSchedulerApp());
}
The API supports all these operations:
GET /
GET /running
GET /{taskName}
GET /{taskName}/reschedule
GET /pause
GET /resume
GET /state