1 © Luxoft Training 2013 Spring Framework Module 9 Task Execution and Scheduling.

Презентация:



Advertisements
Похожие презентации
Vyacheslav Yakovenko Last update: March, 2012 Spring Framework Module 9 – Task Execution and Scheduling.
Advertisements

1 © Luxoft Training 2013 Spring Framework Module 10 JMS & EJB.
Loader Design Options Linkage Editors Dynamic Linking Bootstrap Loaders.
© 2009 Avaya Inc. All rights reserved.1 Chapter Two, Voic Pro Components Module Two – Actions, Variables & Conditions.
Evgeniy Krivosheev Vyacheslav Yakovenko Last update: Feb, 2012 Spring Framework Module 4 – JNDI.
© Luxoft Training 2013 Annotations. © Luxoft Training 2013 Java reflection / RTTI // given the name of a class, get a "Class" object that // has all info.
© 2006 Cisco Systems, Inc. All rights reserved. MPLS v MPLS VPN Implementation Configuring VRF Tables.
1 © Luxoft Training 2012 Inner and anonymous classes.
2005 Pearson Education, Inc. All rights reserved. 1 Object-Oriented Programming: Interface.
Inner Classes. 2 Simple Uses of Inner Classes Inner classes are classes defined within other classes The class that includes the inner class is called.
© 2002 IBM Corporation Confidential | Date | Other Information, if necessary © Wind River Systems, released under EPL 1.0. All logos are TM of their respective.
Учимся писать Эссе. Opinion essays § 1- introduce the subject and state your opinion § 2-4 – or more paragraphs - first viewpoint supported by reasons/
A Bill is a proposal for a new law, or a proposal to change an existing law that is presented for debate before Parliament. Bills are introduced in either.
Multiples Michael Marchenko. Definition In mathematics, a multiple is the product of any quantity and an integer. in other words, for the quantities a.
© 2005 Cisco Systems, Inc. All rights reserved. BGP v Route Selection Using Policy Controls Using Multihomed BGP Networks.
© 2009 Avaya Inc. All rights reserved.1 Chapter Three, Voic Pro Advanced Functions Module Four – Voic Campaigns.
Mobility Control and one-X Mobile. Mobility Control User Configuration Mobile Call Control requires PRI-U, BRI or SIP (RFC2833) trunks in the IP Office.
© 2006 Avaya Inc. All rights reserved. Embedded File Management and SD-Card Handling.
© Luxoft Training 2013 Using Reflection API in Java.
Why do we learn English at schools. (by Kurdina Ekaterina) Learning a new language often begins at a young age and, at some schools, is continued throughout.
Транксрипт:

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!?