Wiki
Clone wikilmf / Module-Scheduler
How to use the LMF Scheduler (based on QuartzScheduler)
Introduction
The Linked Media Framework includes QuartzScheduler to provide a service for recurring tasks such as periodical updates of an RSS Feed.
A list of scheduled Triggers and Jobs is available via web service under the /quartz
endpoint.
Usage
To use the scheduling service, just add
#!java @Inject private QuartzService quartzService;
The QuartzService takes care of initialising the scheduler and acts as a proxy to the Quartz instance, exposing all public methods of the QuartzScheduler API
Jobs implementing the lmf.scheduler.api.DependencyInjectedJob
-Interface will have all declared dependencies injected.
For detailed information on Quartz's Jobs and Triggers see the Quartz coockbook.
Example
Implement a Job
#!java public class MyJob implements DependencyInjectedJob { /** We need the ResourceService, ... **/ @Inject private ResourceService resourceService; /** most operations with resources must be wrapped in a transaction **/ @Inject private TransactionServer transactionService; /** ...and some job-data **/ private String targetUri, myVar; /** provide an injection hook for the rssUrl from JobData **/ public void setTargetUri(String uri) { targetUri = uri; } /** ...and for myVar **/ public void setMyVar(String var) { myVar = var; } /** this is the job-implementation **/ @Override public void execute(JobExecutionContext context) throws JobExecutionException { /** start a transaction **/ Transaction t = transactionService.getTransaction(); transactionService.begin(t); /** load the resource **/ KiWiUriResource r = resourceService.getUriResource(targetUri); /** ... do some magic here ... **/ /** commit the transaction **/ transactionService.commit(t); } }
Schedule a Job
#!java @Inject private QuartzService quartzService; private void scheduleMyJob() { /** (1) create the Job **/ JobDetail job = JobBuilder.newJob(MyJob.class) /** unique identifier of this job (TestGroup.MyTestJob) **/ .withIdentity("MyTestJob", "TestGroup") .withDescription("My first Test Job (a MyJob)") /** Provide some data **/ .usingJobData("myVar", "Hello World") .build(); /** (2) create a Schedule **/ SimpleScheduleBuilder schedule = SimpleScheduleBuilder.repeatHourlyForever(3); /** also look at - - CalendarIntervalScheduleBuilder, - - CronScheduleBuilder and - - DailyTimeIntervalScheduleBuilder **/ /** (3) create a Trigger for MyTestJob **/ Trigger trigger = TriggerBuilder.newTrigger() /** unique identifier of this trigger (TestGroup.MyTestTrigger) **/ .withIdentity("MyTestTrigger", "TestGroup") .withDescription("every 3 hours, for one week") /** the job this trigger is for **/ .forJob(job) /** provide some data, **/ .usingJobData("targetUri", "http://example.com/resource/1234") /** ... and a schedule **/ .withSchedule(schedule) .endAt(DateBuilder.futureDate(1, IntervalUnit.WEEK)) .startNow() .build(); /** (4) schedule trigger and job**/ quartzService.scheduleJob(job, trigger); }
Updated