1 © Luxoft Training 2013 Spring Framework Module 9 Task Execution and Scheduling
2 © Luxoft Training 2013 Contents Spring task and scheduling API; Quartz;
3 © Luxoft Training 2013 Spring :: Task Schedulers Their purpose is to run tasks at some point in future or once after the specified time. There are three core API: Javas Timer is scheduled to run once after the specified time; API Spring Framework: packages: org.springframework.scheduling org.springframework.core.task Quartz scheduler: can be scheduled to run once after the specified time as well; can be scheduled to run at some point in future;
4 © Luxoft Training 2013 Spring :: Task :: TaskExecutor In Spring Framework, an abstraction for task scheduling is based in TaskExecutor interface. The only method provided by TaskExecutor is void execute(Runnable task), which allows to pass a task that implements Runnable interface. TaskExecutor is identical to the java.util.concurrent.Executor interface. Its primary reason for existence is to abstract away the need for Java 1.4.
5 © Luxoft Training 2013 Spring :: Task :: TaskExecutor Implementations of TaskExecutor : SimpleAsyncTaskExecutor : does not reuse any threads, rather it starts up a new thread; SyncTaskExecutor : each invocation takes place in the calling thread, does not execute invocations asynchronously; ConcurrentTaskExecutor : a wrapper for java.util.concurrent.Executor ; SimpleThreadPoolTaskExecutor : a subclass of Quartz's SimpleThreadPool ; ThreadPoolTaskExecutor ; TimerTaskExecutor ; WorkManagerTaskExecutor : uses CommonJ WorkManager
6 © Luxoft Training 2013 public class TaskExecutorExample { private class MessagePrinterTask implements Runnable { private String message; public MessagePrinterTask(String message) { this.message = message; } public void run() { System.out.println(message); } private TaskExecutor taskExecutor; public TaskExecutorExample(TaskExecutor taskExecutor) { this.taskExecutor = taskExecutor; } public void printMessages() { for(int i = 0; i < 25; i++) { taskExecutor.execute( new MessagePrinterTask("Message" + i)); } Spring :: Task :: TaskExecutor
7 © Luxoft Training 2013 <bean id="taskExecutor" class="org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor"> Spring :: Task :: TaskExecutor
8 © Luxoft Training 2013 Spring :: Task :: TaskScheduler In addition, Spring 3 introduces TaskScheduler interface: public interface TaskScheduler { ScheduledFuture schedule(Runnable task, Trigger trigger); ScheduledFuture schedule(Runnable task, Date startTime); ScheduledFuture scheduleAtFixedRate(Runnable task, Date startTime, long period); ScheduledFuture scheduleAtFixedRate(Runnable task, long period); ScheduledFuture scheduleWithFixedDelay(Runnable task, Date startTime, long delay); ScheduledFuture scheduleWithFixedDelay(Runnable task, long delay); }
9 © Luxoft Training 2013 Spring :: Task :: TaskScheduler The primary benefit of this interface is that it doesnt have to be coupled to a particular scheduler implementation. This is particularly important when running within application server environment where threads should not be directly created by application. For such cases, Spring provides TimerManagerTaskScheduler that delegates task execution to CommonJ TimerManager, typically configured with JNDI.
10 © Luxoft Training 2013 Spring :: Task :: Trigger Another interface implemented in Spring 3. The basic idea of this interface is that task execution may be determined based on past execution outcomes, that is, should be context-aware. public interface Trigger { Date nextExecutionTime(TriggerContext triggerContext); } public interface TriggerContext { Date lastScheduledExecutionTime(); Date lastActualExecutionTime(); Date lastCompletionTime(); }
11 © Luxoft Training 2013 Spring :: Task :: Trigger Example: scheduler.schedule(task, new CronTrigger("* * * MON-FRI")); Execute: Every 15 minutes; From 9 to 17; From Monday to Friday; cron syntax: * * * * * * command to be executed | | | | | | | | | | | day of week (0 - 6) (Sunday=0) | | | | month (1 - 12) | | | day of month (1 - 31) | | hour (0 - 23) | min (0 - 59) amount or amount/interval
12 © Luxoft Training 2013 Spring :: Task :: Namespace Spring 3 introduces namespace, a task that allows to initialize certain beans in application context: and to turn on auto detection of components annotated
13 © Luxoft Training 2013 Spring :: Task :: Example Task specification in application context: <task:scheduled ref="someObject" method="someMethod" fixed-rate="5000" /> <task:scheduled ref="anotherObject" method="anotherMethod" cron="*/5 * * * * MON-FRI" />
14 © Luxoft Training 2013 Spring :: Task :: Example Specifying tasks public void doSomething() { // something that should execute periodically public void doSomething() { // something that should execute periodically } * Methods to be declared component
15 © Luxoft Training 2013 Spring :: Task :: Example Specifying tasks * * * * MON-FRI") public void doSomething() { // something that should execute on weekdays only }
16 © Luxoft Training 2013 Spring :: Task :: Quartz Quartz is a commonly used external library used for tasks handling. It uses JobDetail interface when specifying the task. For such cases, Spring 3.1 provides JobDetailFactoryBean that supports both Quartz v.1 and Quartz v.2: <bean id="reportJob class="org.springframework.scheduling.quartz.JobDetailFactoryBean"> public class PrintingJob extends QuartzJobBean { protected void executeInternal(JobExecutionContext context) throws JobExecutionException { ScheduleLog.append("I'm printing job...\n"); }
17 © Luxoft Training 2013 Spring :: Task :: Example <bean id="schedulerFactoryBean" class="org.springframework.scheduling.quartz.SchedulerFactoryBean"> <bean id="reportTrigger" class="org.springframework.scheduling.quartz.SimpleTriggerFactoryBean">
18 © Luxoft Training 2013 Spring :: Task :: Quartz SchedulerFactoryBean SimpleTriggerFactoryBean CronTriggerFactoryBean Job 1 Job 2 Job 3 Job 4 Job N General bean interaction diagram:
19 © Luxoft Training 2013 Spring :: Task :: Quartz SchedulerFactoryBean SimpleTriggerFactoryBean CronTriggerFactoryBean Job 1 Job 2 Job 3 Job 4 Job N Bean interaction diagram: List of various triggers is supported
20 © Luxoft Training 2013 Spring :: Task :: Quartz SchedulerFactoryBean SimpleTriggerFactoryBean CronTriggerFactoryBean Job 1 Job 2 Job 3 Job 4 Job N Bean interaction diagram : Specifies time,interval / mask for task execution
21 © Luxoft Training 2013 Spring :: Task :: Quartz SchedulerFactoryBean SimpleTriggerFactoryBean CronTriggerFactoryBean Job 1 Job 2 Job 3 Job 4 Job N Bean interaction diagram : The tasks themselves can be abstracted away from API Quartz | Spring
22 © Luxoft Training 2013 Exercises : 10 : Using task scheduling. – 30 min for practice;
23 © Luxoft Training 2013 Any questions!?