Presentation is loading. Please wait.

Presentation is loading. Please wait.

Copyright 2007 SpringSource. Copying, publishing or distributing without express written permission is prohibited. Scheduling and Tasks Spring scheduling.

Similar presentations


Presentation on theme: "Copyright 2007 SpringSource. Copying, publishing or distributing without express written permission is prohibited. Scheduling and Tasks Spring scheduling."— Presentation transcript:

1 Copyright 2007 SpringSource. Copying, publishing or distributing without express written permission is prohibited. Scheduling and Tasks Spring scheduling and task execution abstractions

2 Copyright 2007 SpringSource. Copying, publishing or distributing without express written permission is prohibited. 2 Topics in this session Introduction (2) TaskExecutor abstraction (13) Scheduling Framework overview (20) CommonJ container thread management (7)

3 Copyright 2007 SpringSource. Copying, publishing or distributing without express written permission is prohibited. 3 The TaskExecutor Interface public interface TaskExecutor { void execute(Runnable task); } Standard Java Runnable

4 Copyright 2007 SpringSource. Copying, publishing or distributing without express written permission is prohibited. 4 Options for Task Execution (1) SyncTaskExecutor –Simple, synchronous execution SimpleAsyncTaskExecutor –Simple, asynchronous execution –Support throttling execution requests ThreadPoolTaskExecutor –Use the Java 5 ThreadPoolExecutor for scheduling –Highly configurable Thread pool size Rejection callbacks

5 Copyright 2007 SpringSource. Copying, publishing or distributing without express written permission is prohibited. 5 Options for Task Execution (2) ConcurrentTaskExecutor –Uses a Java 5 Executor for scheduling –Prefer the ThreadPoolTaskExecutor TimerTaskExecutor –Uses the JDK Timer –Supports a basic delay before scheduling WorkManagerTaskExecutor –Uses the CommonJ WorkManager –Accessed from JNDI –Full Application Server support –More on this later

6 Copyright 2007 SpringSource. Copying, publishing or distributing without express written permission is prohibited. 6 Configuring Simple TaskExecutors SyncTaskExecutor –No configuration options – its synchronous! SimpleAsyncTaskExecutor –concurrencyLimit The number of concurrent threads to allow (optional) –threadNamePrefix Add a prefix to the name of the created thread (optional) TimerTaskExecutor –timer The Timer instance to use (optional) –delay The delay, in millis, before starting the task (optional)

7 Copyright 2007 SpringSource. Copying, publishing or distributing without express written permission is prohibited. 7 Understanding ThreadPoolTaskExecutor Backed by JDK 5.0 ThreadPoolExecutor Provides full pooling and queuing support Notification of rejected errors Allows for customized thread creation Extremely configurable –Pool size –Queue capacity –Rejection behaviour

8 Copyright 2007 SpringSource. Copying, publishing or distributing without express written permission is prohibited. 8 ThreadPoolExecutor Concepts The thread pool –Configurable maximum and core sizes –Threads are not prestarted by default Customisable Thread creation –Using a ThreadFactory implementation Provides queuing capabilities for controlling how jobs are passed to the pool

9 Copyright 2007 SpringSource. Copying, publishing or distributing without express written permission is prohibited. 9 Thread Creation Policy Thread count < core pool size –Create a thread Thread count >= core pool size –Favour queuing –Task served by existing thread Cannot queue? –Create new thread if thread count < max pool size –Else – reject task

10 Copyright 2007 SpringSource. Copying, publishing or distributing without express written permission is prohibited. 10 Understanding Queuing (1) Three different strategies –Direct handoff SynchronousQueue Configurable in ThreadPoolTaskExecutor –Unbounded queues Unbounded LinkedBlockingQueue Configurable in ThreadPoolTaskExecutor –Bounded queues Bounded LinkedBlockingQueue Configurable in ThreadPoolTaskExecutor

11 Copyright 2007 SpringSource. Copying, publishing or distributing without express written permission is prohibited. 11 Understanding Queuing (2) Direct handoff –Tasks are passed directly to threads in the pool –Can lead to exhaustion of the pool –Usually used with unbound max pool size Unbounded queues –Tasks are always queued when busy thread count = core pool size –Max pool size has not relevance –Ensures smooth level of throughput –Danger of unbounded queue growth Bounded queues –More difficult to tune than unbounded queues –Can help prevent reckless queue growth

12 Copyright 2007 SpringSource. Copying, publishing or distributing without express written permission is prohibited. 12 Handling Rejected Tasks Configure a RejectedExecutionHander : public interface RejectedExecutionHandler { void rejectedExecution(Runnable r, ThreadPoolExecutor executor); }

13 Copyright 2007 SpringSource. Copying, publishing or distributing without express written permission is prohibited. 13 Customising Thread Creation Configure a custom ThreadFactory : public interface ThreadFactory { Thread newThread(Runnable r); }

14 Copyright 2007 SpringSource. Copying, publishing or distributing without express written permission is prohibited. 14 Configuring ThreadPoolTaskExecutor (1) corePoolSize –Specify the core pool size –Default is 1 maxPoolSize –Specify the max pool size –Default is Integer.MAX_VALUE (unbounded) keepAliveSeconds –Specifies how long a Thread (count > core pool size) are kept alive –Default is 60

15 Copyright 2007 SpringSource. Copying, publishing or distributing without express written permission is prohibited. 15 Configuring ThreadPoolTaskExecutor (1) queueCapacity –Specifies the capacity of the queue –Defaults to Integer.MAX_VALUE for unbounded –Set to >0 for bounded queue –Set to 0 for a direct handoff rejectedExecutionHandler –Specify the RejectedExecutionHandler to use threadFactory –Specify the ThreadFactory to use –Defaults to Executors.defaultThreadFactory()

16 Copyright 2007 SpringSource. Copying, publishing or distributing without express written permission is prohibited. Demo test.task.FibonacciLauncherTest

17 Copyright 2007 SpringSource. Copying, publishing or distributing without express written permission is prohibited. 17 Introducing CommonJ Thread management for appserver container (c.f. EJB2) Standardised API sets –Created by IBM and BEA –Submitted to JCP Covers many areas –Service Data Objects (SDO) –WorkManager and TimerManager WorkManager is available in both WebLogic Server 9 and WebSphere 6

18 Copyright 2007 SpringSource. Copying, publishing or distributing without express written permission is prohibited. 18 CommonJ and Spring Task Execution with WorkManager –Lookup WorkManager from JNDI –Use WorkManagerTaskExecutor Supports JNDI lookup internally Scheduling with TimerManager –Lookup TimerManager from JNDI –Use TimerManagerFactoryBean Support JNDI lookup internally Configure instances of ScheduledTimerListener to describe jobs

19 Copyright 2007 SpringSource. Copying, publishing or distributing without express written permission is prohibited. 19 Configuring WorkManagerExecutor <property name="workManagerName" value="wm/DvdCentralWorkManager"/> <property name="resourceRef" value="true"/> JNDI name for manager

20 Copyright 2007 SpringSource. Copying, publishing or distributing without express written permission is prohibited. 20 Configuring WorkManagerExecutor (2) workManager –Specify the WorkManager instance to use workManagerName –Specify the JNDI name of the WorkManager –Automatic JNDI lookup jndiEnvironment –Specify the JNDI environment properties resourceRef –Is the JNDI name a resource reference? jndiTemplate –The JndiTemplate instance to use for lookups

21 Copyright 2007 SpringSource. Copying, publishing or distributing without express written permission is prohibited. 21 Scheduling Concepts (1) Jobs/Task –The unit of work to perform within the configured schedule Schedule –The details governing the execution of the job –May be a simple start time and time period or a more complex expression such as 'every Monday at 10:00am' Initial delay –The initial delay before the commencement of the first execution

22 Copyright 2007 SpringSource. Copying, publishing or distributing without express written permission is prohibited. 22 Scheduling Concepts (2) Period –The period between subsequent executions of a job –Exact semantics are governed by the fixed rate/delay option Fixed Rate –Indicates that period is rate at which jobs execute (i.e every 5 minutes) –May get executions in rapid succession if delays occur –Scheduled relative to the initial execution Fixed Delay –Indicates that period is the delay between termination of execution A and commencement of execution B. –Scheduled relative to the execution time of the previous execution

23 Copyright 2007 SpringSource. Copying, publishing or distributing without express written permission is prohibited. 23 Options for Scheduling TimerFactoryBean –Uses the JDK Timer scheduler –Fixed rate/delay scheduling ScheduledExecutorFactoryBean –Uses the Java 5.0 ScheduledExecutorService –Fixed rate/delay scheduling SchedulerFactoryBean –Uses a Quartz scheduler –Extremely flexible scheduling support Use cron expressions TimerManagerFactoryBean –Uses a CommonJ TimerManager –Full ApplicationServer support –Fixed rate/delay scheduling

24 Copyright 2007 SpringSource. Copying, publishing or distributing without express written permission is prohibited. 24 Scheduling with JDK Timer Configure an instance of TimerFactoryBean Associate instances of ScheduledTimerTask –Wrap the logic to invoke in a Runnable or TimerTask –Defines the execution details –Consider using MethodInvokingRunnable to automatically delegate to a service

25 Copyright 2007 SpringSource. Copying, publishing or distributing without express written permission is prohibited. 25 Configuring TimerFactoryBean (1)

26 Copyright 2007 SpringSource. Copying, publishing or distributing without express written permission is prohibited. 26 Configuring TimerFactoryBean (2) TimerFactoryBean Properties –daemon Should the timer run as a daemon thread? Default is true –scheduledTimerTasks The list of ScheduledTimerTask objects detailing the units of work and their schedules

27 Copyright 2007 SpringSource. Copying, publishing or distributing without express written permission is prohibited. 27 Configuring TimerFactoryBean (3) ScheduledTimerTask Properties –delay The initial delay before the first execution –period The period between execution –fixedRate Indicates whether to use fixed rate (true) or fixed delay (false) execution Default is false –runnable Specifies the Runnable to run at each execution Wrapped in a TimerTask –timerTask Specifies the TimerTask to run at each execution

28 Copyright 2007 SpringSource. Copying, publishing or distributing without express written permission is prohibited. 28 The MethodInvokingRunnable Class Wrapper around a bean Reflectively invokes a chosen method Removes the need to create Runnable classes for all tasks –Simply wrap your logic in a MethodInvokingRunnable instance Can't perform any additional logic in the task

29 Copyright 2007 SpringSource. Copying, publishing or distributing without express written permission is prohibited. 29 Scheduling with ScheduledExecutorService Configure an instance of ScheduledExecutorFactoryBean Associate instances of ScheduledExecutorTask –Wrap the logic to invoke in a Runnable –Defines the execution details –Consider using MethodInvokingRunnable to automatically delegate to a service

30 Copyright 2007 SpringSource. Copying, publishing or distributing without express written permission is prohibited. 30 Configuring ScheduledExecutorFactoryBean (1)

31 Copyright 2007 SpringSource. Copying, publishing or distributing without express written permission is prohibited. 31 ScheduledExecutorFactoryBean Properties –poolSize Size of the core pool – max pool is not important Default is 1 –scheduledTimerTasks The list of ScheduledTimerTask objects detailing the units of work and their schedules Configuring ScheduledExecutorFactoryBean (2)

32 Copyright 2007 SpringSource. Copying, publishing or distributing without express written permission is prohibited. 32 ScheduledExecutorTask Properties –delay The initial delay before the first execution –period The period between execution –fixedRate Indicates whether to use fixed rate (true) or fixed delay (false) execution Default is false –runnable Specifies the Runnable to run at each execution Wrapped in a TimerTask Configuring ScheduledExecutorFactoryBean (3)

33 Copyright 2007 SpringSource. Copying, publishing or distributing without express written permission is prohibited. Demo test.scheduling.TimerSchedulingTest

34 Copyright 2007 SpringSource. Copying, publishing or distributing without express written permission is prohibited. 34 Introducing Quartz Open-source scheduling engine from OpenSymphony Provides highly configurable scheduling options –Cron expressions –Exclusions with Calendars Persistence jobs Cluster support

35 Copyright 2007 SpringSource. Copying, publishing or distributing without express written permission is prohibited. 35 Quartz Components (1) Job –Represents the unit of work to perform –Implemented by you StatefulJob –Marker interface for Jobs whose state should be maintained Scheduler –The main component in Quartz –Handles all the scheduling of Jobs Trigger –Controls the frequency with which scheduled Jobs are executed JobDetail –Stores a Job along with any Job data Calendar –Used to define inclusions/exclusions in a Triggers usual schedule JobStore –Mechanism used to store Job data –Default is RAMJobStore

36 Copyright 2007 SpringSource. Copying, publishing or distributing without express written permission is prohibited. 36 Configuring a Scheduler in Spring Use SchedulerFactoryBean Important Properties –schedulerName Name of the scheduler instance Important when using multiple Schedulers –jobDetails List of JobDetails to register Referred to by Triggers –triggers The list of Triggers used to schedule Jobs May include the JobDetail directly

37 Copyright 2007 SpringSource. Copying, publishing or distributing without express written permission is prohibited. 37 Persistent Jobs Stores Job schedule details –Runtime registered Jobs –StatefulJob state Enabling: –Add the Quartz tables to your database –Configure a DataSource for that database –Inject the DataSource into the SchedulerFactoryBean –Specify optional PlatformTransactionManager to use for registering Jobs and Triggers

38 Copyright 2007 SpringSource. Copying, publishing or distributing without express written permission is prohibited. 38 Trigger Options (1) CronTrigger –Provides support for Unix-style cron expressions –Supports inclusions/exclusions with Calendars –Extremely powerful –Supports complex expressions such as: Every Monday, Wednesday and Friday at 10:00am The last Monday of every month at noon The last weekday of the month at 11:30am Every 5 seconds starting at the 3 rd second of the minute: –3 8 13 18 23 28 33 38…

39 Copyright 2007 SpringSource. Copying, publishing or distributing without express written permission is prohibited. Demo test.scheduling.QuartzSchedulingTest

40 Copyright 2007 SpringSource. Copying, publishing or distributing without express written permission is prohibited. 40 Understanding Cron Expressions 3/5 * 20-22 * * ? SecondsMinutesHoursDay of Month Day of Week Check out CronTrigger JavaDoc for more examples

41 Copyright 2007 SpringSource. Copying, publishing or distributing without express written permission is prohibited. 41 More on Quartz… Misfire instructions Calendars –Fine-grained control over scheduling Multitude of configuration options SchedulerListeners, JobListeners and TriggerListeners –Monitor your Scheduler, Jobs and Triggers Cluster support

42 Copyright 2007 SpringSource. Copying, publishing or distributing without express written permission is prohibited. 42 Configuring TimerManagerFactoryBean (1)

43 Copyright 2007 SpringSource. Copying, publishing or distributing without express written permission is prohibited. 43 Configuring TimerManagerFactoryBean (2) Standard JNDI properties timerManager –The TimerManager instance to use timerManagerName –The JNDI name of the TimerManager scheduledTimerListeners –The list of ScheduledTimerListeners to configure with the TimerManager

44 Copyright 2007 SpringSource. Copying, publishing or distributing without express written permission is prohibited. 44 Configuring TimerManagerFactoryBean (3) ScheduledTimerListener Properties –delay The initial delay before the first execution –firstTime The Date of the initial execution –period The period between execution –fixedRate Indicates whether to use fixed rate (true) or fixed delay (false) execution Default is false –runnable Specifies the Runnable to run at each execution Wrapped in a TimerListener –timerListener Specifies the TimerListener to run at each execution

45 Copyright 2007 SpringSource. Copying, publishing or distributing without express written permission is prohibited. 45 Choosing a Scheduler As opposed to simple asynchronous execution using TaskExecutor Web applications –Use Quartz Simple-standalone environment –Use ScheduledExecutorService Multiple threads Highly configurable –Timer-based scheduling is okay for really simple cases –Consider Quartz for complex trigger support Strict Java EE environment –Consider WorkManager Threading support is built in –Most people still prefer Quartz however Can use a WorkManager as its thread pool through Spring!

46 Copyright 2007 SpringSource. Copying, publishing or distributing without express written permission is prohibited. 46 Summary Comprehensive task execution support –ThreadPoolExecutor for Java 5 –Take advantage of CommonJ WorkManager if your platform supports it –SyncTaskExecutor is great for testing Multiple options for scheduling –Java 5 ScheduledExecutorService –CommonJ TimerManager –Quartz Its all about choice!

47 Copyright 2007 SpringSource. Copying, publishing or distributing without express written permission is prohibited. 47 Scheduling in Spring 1.2 Two solutions for scheduling –JDK Timer Basic support for repeatable tasks Basic definition of schedule No clustering support No support for persistent tasks –Quartz Rich-schedule definitions using cron expressions Full clustering support Support for persistent jobs

48 Copyright 2007 SpringSource. Copying, publishing or distributing without express written permission is prohibited. 48 What's new in Spring 2.0? Scheduling –JDK ScheduledExecutorService, Timer –CommonJ WorkManager –Application Server support Supported on WebLogic Server 9 and WebSphere 6 Task Execution –Simple API for task execution –Many different implementations Simple Threading ThreadPoolExecutor CommonJ WorkManager

49 Copyright 2007 SpringSource. Copying, publishing or distributing without express written permission is prohibited. 49 More Information On the web: –http://www.springframework.orghttp://www.springframework.org –http://dev2dev.bea.com/wlplatform/commonj/http://dev2dev.bea.com/wlplatform/commonj/ –http://www.opensymphony.com/quartzhttp://www.opensymphony.com/quartz Books: –Pro Spring – good Quartz coverage

50 Copyright 2007 SpringSource. Copying, publishing or distributing without express written permission is prohibited. 50 Topics in this session Introduction (2) TaskExecutor abstraction (13) Framework overview (20) CommonJ container thread management (7)


Download ppt "Copyright 2007 SpringSource. Copying, publishing or distributing without express written permission is prohibited. Scheduling and Tasks Spring scheduling."

Similar presentations


Ads by Google