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

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

Embed Size (px)

Citation preview

Copyright 2007 SpringSource. Copying, publishing or distributing without express written permission is prohibited.

Scheduling and Tasks

Spring scheduling and task execution abstractions

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)

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

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

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

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)

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

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

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

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

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

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);

}

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);

}

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

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()

Copyright 2007 SpringSource. Copying, publishing or distributing without express written permission is prohibited.

Demo

test.task.FibonacciLauncherTest

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

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

Copyright 2007 SpringSource. Copying, publishing or distributing without express written permission is prohibited. 19

Configuring WorkManagerExecutor

<bean id="taskExecutor" class="...WorkManagerTaskExecutor"> <property name="workManagerName" value="wm/DvdCentralWorkManager"/> <property name="resourceRef" value="true"/></bean>

JNDI name for manager

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

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

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

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

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

Copyright 2007 SpringSource. Copying, publishing or distributing without express written permission is prohibited. 25

Configuring TimerFactoryBean (1)

<bean id="timerScheduler" class="...TimerFactoryBean"> <property name="scheduledTimerTasks"> <list> <bean class="...ScheduledTimerTask"> <property name="delay" value="1"/> <property name="fixedRate" value="false"/> <property name="period" value="5000"/> <property name="runnable" ref="stockPriceTask"/> </bean> </list> </property></bean>

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

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

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

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

Copyright 2007 SpringSource. Copying, publishing or distributing without express written permission is prohibited. 30

Configuring ScheduledExecutorFactoryBean (1)

<bean id="executorScheduler" class="...ScheduledExecutorFactoryBean"> <property name="poolSize" value="1" /> <property name="scheduledExecutorTasks"> <list> <bean class="...ScheduledExecutorTask"> <property name="delay" value="1" /> <property name="fixedRate" value="false" /> <property name="period" value="2000" /> <property name="runnable" ref="stockPriceTask" /> </bean> </list> </property></bean>

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)

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)

Copyright 2007 SpringSource. Copying, publishing or distributing without express written permission is prohibited.

Demo

test.scheduling.TimerSchedulingTest

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

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

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

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

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 3rd second of the minute:

– 3 8 13 18 23 28 33 38…

Copyright 2007 SpringSource. Copying, publishing or distributing without express written permission is prohibited.

Demo

test.scheduling.QuartzSchedulingTest

Copyright 2007 SpringSource. Copying, publishing or distributing without express written permission is prohibited. 40

Understanding Cron Expressions

3/5 * 20-22 * * ?

Seconds Minutes Hours Dayof

Month

Month Dayof

Week

Check out CronTrigger JavaDoc for more examples

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

Copyright 2007 SpringSource. Copying, publishing or distributing without express written permission is prohibited. 42

Configuring TimerManagerFactoryBean (1)

<bean id="scheduler" class="...TimerManagerFactoryBean"> <property name="timerManagerName" value="tm/TimerManager" /> <property name="resourceRef" value="true" /> <property name="scheduledTimerListeners"> <bean class="...ScheduledTimerListener"> <property name="delay" value="3000"/> <property name="period" value="30000"/> <property name="runnable"> <bean class="...MethodInvokingRunnable"> <property name="targetMethod" value="deleteAll"/> <property name="targetObject" ref="orderDao"/> </bean> </property> </bean> </property></bean>

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

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

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!

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!

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

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

Copyright 2007 SpringSource. Copying, publishing or distributing without express written permission is prohibited. 49

More Information

• On the web:– http://www.springframework.org– http://dev2dev.bea.com/wlplatform/commonj/– http://www.opensymphony.com/quartz

• Books:– Pro Spring – good Quartz coverage

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)