public class WorkManagerTaskExecutor extends JndiLocatorSupport implements AsyncListenableTaskExecutor, SchedulingTaskExecutor, WorkManager, BootstrapContextAware, InitializingBean
TaskExecutor
implementation
that delegates to a JCA 1.7 WorkManager, implementing the
WorkManager
interface.
This is mainly intended for use within a JCA ResourceAdapter implementation, but may also be used in a standalone environment, delegating to a locally embedded WorkManager implementation (such as Geronimo's).
Also implements the JCA 1.7 WorkManager interface itself, delegating all calls to the target WorkManager. Hence, a caller can choose whether it wants to talk to this executor through the Spring TaskExecutor interface or the WorkManager interface.
This adapter is also capable of obtaining a JCA WorkManager from JNDI.
This is for example appropriate on the Geronimo application server, where
WorkManager GBeans (e.g. Geronimo's default "DefaultWorkManager" GBean)
can be linked into the Java EE environment through "gbean-ref" entries
in the geronimo-web.xml
deployment descriptor.
setWorkManager(javax.resource.spi.work.WorkManager)
,
WorkManager.scheduleWork(javax.resource.spi.work.Work)
CONTAINER_PREFIX
logger
TIMEOUT_IMMEDIATE, TIMEOUT_INDEFINITE
IMMEDIATE, INDEFINITE, UNKNOWN
Constructor and Description |
---|
WorkManagerTaskExecutor()
Create a new WorkManagerTaskExecutor, expecting bean-style configuration.
|
WorkManagerTaskExecutor(WorkManager workManager)
Create a new WorkManagerTaskExecutor for the given WorkManager.
|
Modifier and Type | Method and Description |
---|---|
void |
afterPropertiesSet()
Invoked by the containing
BeanFactory after it has set all bean properties
and satisfied BeanFactoryAware , ApplicationContextAware etc. |
void |
doWork(Work work) |
void |
doWork(Work work,
long delay,
ExecutionContext executionContext,
WorkListener workListener) |
void |
execute(Runnable task)
Execute the given
task . |
void |
execute(Runnable task,
long startTimeout)
Execute the given
task . |
protected WorkManager |
getDefaultWorkManager()
Obtain a default WorkManager to delegate to.
|
boolean |
prefersShortLivedTasks()
This task executor prefers short-lived work units.
|
void |
scheduleWork(Work work) |
void |
scheduleWork(Work work,
long delay,
ExecutionContext executionContext,
WorkListener workListener) |
void |
setBlockUntilCompleted(boolean blockUntilCompleted)
Set whether to let
execute(java.lang.Runnable) block until the work
has been completed. |
void |
setBlockUntilStarted(boolean blockUntilStarted)
Set whether to let
execute(java.lang.Runnable) block until the work
has been actually started. |
void |
setBootstrapContext(BootstrapContext bootstrapContext)
Specify the JCA BootstrapContext that contains the
WorkManager to delegate to.
|
void |
setTaskDecorator(TaskDecorator taskDecorator)
Specify a custom
TaskDecorator to be applied to any Runnable
about to be executed. |
void |
setWorkListener(WorkListener workListener)
Specify a JCA WorkListener to apply, if any.
|
void |
setWorkManager(WorkManager workManager)
Specify the JCA WorkManager instance to delegate to.
|
void |
setWorkManagerName(String workManagerName)
Set the JNDI name of the JCA WorkManager.
|
long |
startWork(Work work) |
long |
startWork(Work work,
long delay,
ExecutionContext executionContext,
WorkListener workListener) |
<T> Future<T> |
submit(Callable<T> task)
Submit a Callable task for execution, receiving a Future representing that task.
|
Future<?> |
submit(Runnable task)
Submit a Runnable task for execution, receiving a Future representing that task.
|
<T> ListenableFuture<T> |
submitListenable(Callable<T> task)
Submit a
Callable task for execution, receiving a ListenableFuture
representing that task. |
ListenableFuture<?> |
submitListenable(Runnable task)
Submit a
Runnable task for execution, receiving a ListenableFuture
representing that task. |
convertJndiName, isResourceRef, lookup, lookup, setResourceRef
getJndiEnvironment, getJndiTemplate, setJndiEnvironment, setJndiTemplate
public WorkManagerTaskExecutor()
public WorkManagerTaskExecutor(WorkManager workManager)
workManager
- the JCA WorkManager to delegate topublic void setWorkManager(WorkManager workManager)
public void setWorkManagerName(String workManagerName)
This can either be a fully qualified JNDI name, or the JNDI name relative to the current environment naming context if "resourceRef" is set to "true".
public void setBootstrapContext(BootstrapContext bootstrapContext)
setBootstrapContext
in interface BootstrapContextAware
bootstrapContext
- BootstrapContext object to be used by this objectInitializingBean.afterPropertiesSet()
,
ApplicationContextAware.setApplicationContext(org.springframework.context.ApplicationContext)
public void setBlockUntilStarted(boolean blockUntilStarted)
execute(java.lang.Runnable)
block until the work
has been actually started.
Uses the JCA startWork
operation underneath,
instead of the default scheduleWork
.
public void setBlockUntilCompleted(boolean blockUntilCompleted)
execute(java.lang.Runnable)
block until the work
has been completed.
Uses the JCA doWork
operation underneath,
instead of the default scheduleWork
.
public void setWorkListener(@Nullable WorkListener workListener)
This shared WorkListener instance will be passed on to the
WorkManager by all execute(java.lang.Runnable)
calls on this TaskExecutor.
public void setTaskDecorator(TaskDecorator taskDecorator)
TaskDecorator
to be applied to any Runnable
about to be executed.
Note that such a decorator is not necessarily being applied to the
user-supplied Runnable
/Callable
but rather to the actual
execution callback (which may be a wrapper around the user-supplied task).
The primary use case is to set some execution context around the task's invocation, or to provide some monitoring/statistics for task execution.
NOTE: Exception handling in TaskDecorator
implementations
is limited to plain Runnable
execution via execute
calls.
In case of #submit
calls, the exposed Runnable
will be a
FutureTask
which does not propagate any exceptions; you might
have to cast it and call Future#get
to evaluate exceptions.
public void afterPropertiesSet() throws NamingException
InitializingBean
BeanFactory
after it has set all bean properties
and satisfied BeanFactoryAware
, ApplicationContextAware
etc.
This method allows the bean instance to perform validation of its overall configuration and final initialization when all bean properties have been set.
afterPropertiesSet
in interface InitializingBean
NamingException
protected WorkManager getDefaultWorkManager()
The default implementation returns a SimpleTaskWorkManager
.
Can be overridden in subclasses.
public void execute(Runnable task)
TaskExecutor
task
.
The call might return immediately if the implementation uses an asynchronous execution strategy, or might block in the case of synchronous execution.
execute
in interface Executor
execute
in interface TaskExecutor
task
- the Runnable
to execute (never null
)public void execute(Runnable task, long startTimeout)
AsyncTaskExecutor
task
.execute
in interface AsyncTaskExecutor
task
- the Runnable
to execute (never null
)startTimeout
- the time duration (milliseconds) within which the task is
supposed to start. This is intended as a hint to the executor, allowing for
preferred handling of immediate tasks. Typical values are AsyncTaskExecutor.TIMEOUT_IMMEDIATE
or AsyncTaskExecutor.TIMEOUT_INDEFINITE
(the default as used by TaskExecutor.execute(Runnable)
).public Future<?> submit(Runnable task)
AsyncTaskExecutor
null
result upon completion.submit
in interface AsyncTaskExecutor
task
- the Runnable
to execute (never null
)public <T> Future<T> submit(Callable<T> task)
AsyncTaskExecutor
submit
in interface AsyncTaskExecutor
task
- the Callable
to execute (never null
)public ListenableFuture<?> submitListenable(Runnable task)
AsyncListenableTaskExecutor
Runnable
task for execution, receiving a ListenableFuture
representing that task. The Future will return a null
result upon completion.submitListenable
in interface AsyncListenableTaskExecutor
task
- the Runnable
to execute (never null
)ListenableFuture
representing pending completion of the taskpublic <T> ListenableFuture<T> submitListenable(Callable<T> task)
AsyncListenableTaskExecutor
Callable
task for execution, receiving a ListenableFuture
representing that task. The Future will return the Callable's result upon
completion.submitListenable
in interface AsyncListenableTaskExecutor
task
- the Callable
to execute (never null
)ListenableFuture
representing pending completion of the taskpublic boolean prefersShortLivedTasks()
prefersShortLivedTasks
in interface SchedulingTaskExecutor
true
if this TaskExecutor
prefers
short-lived taskspublic void doWork(Work work) throws WorkException
doWork
in interface WorkManager
WorkException
public void doWork(Work work, long delay, ExecutionContext executionContext, WorkListener workListener) throws WorkException
doWork
in interface WorkManager
WorkException
public long startWork(Work work) throws WorkException
startWork
in interface WorkManager
WorkException
public long startWork(Work work, long delay, ExecutionContext executionContext, WorkListener workListener) throws WorkException
startWork
in interface WorkManager
WorkException
public void scheduleWork(Work work) throws WorkException
scheduleWork
in interface WorkManager
WorkException
public void scheduleWork(Work work, long delay, ExecutionContext executionContext, WorkListener workListener) throws WorkException
scheduleWork
in interface WorkManager
WorkException