the major use-case is simple. just annotate your job-implementations (in case of quartz: org.quartz.Job) with @Scheduled(cronExpression = "[your expression]") and it will be scheduled/installed automatically at the end of the bootstrapping process.
it's possible to use cdi based injection:
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@Scheduled(cronExpression = "0 0/10 * * * ?") | |
public class CdiAwareQuartzJob implements org.quartz.Job | |
{ | |
@Inject | |
private MyService service; | |
@Override | |
public void execute(JobExecutionContext context) | |
throws JobExecutionException | |
{ | |
//... | |
} | |
} |
beyond that it's also possible to inject the scheduler and control (scheduleJob, interruptJob, startJobManually,...) it manually - e.g.:
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@ApplicationScoped | |
public class SchedulerController | |
{ | |
@Inject | |
private Scheduler<Job> jobScheduler; | |
public void onPause(@Observes PauseJobEvent /*custom event*/ jobEvent) | |
{ | |
//using it with events is optional | |
this.jobScheduler.pauseJob(jobEvent.getJobClass()); | |
} | |
public void onResume(@Observes ResumeJobEvent /*custom event*/ jobEvent) | |
{ | |
//using it with events is optional | |
this.jobScheduler.resumeJob(jobEvent.getJobClass()); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@ApplicationScoped | |
public class ProjectStageAwareSchedulerController | |
{ | |
@Inject | |
private Scheduler<Job> jobScheduler; | |
@Inject | |
private ProjectStage projectStage; | |
public void registerJobs() | |
{ | |
if (ProjectStage.Production.equals(this.projectStage)) | |
{ | |
//see 'false' for @Scheduled#onStartup | |
this.jobScheduler.scheduleJob(ManualCdiAwareQuartzJob.class); | |
} | |
} | |
@Scheduled(cronExpression = "0 0/10 * * * ?", onStartup = false) | |
public class ManualCdiAwareQuartzJob implements org.quartz.Job | |
{ | |
@Inject | |
private MyService service; | |
@Override | |
public void execute(JobExecutionContext context) | |
throws JobExecutionException | |
{ | |
//... | |
} | |
} | |
} |
Update:
This add-on is part of all versions of DeltaSpike after v0.5