public final class WorkUnit
extends java.lang.Object
WorkUnit.initCurrent();
try {
...
} finally {
WorkUnit.closeCurrent();
}
This pattern is implemented by the work(Callable)
method and its sibling.
The application thread pool implementation uses this pattern, so that in
general it is not necessary to implement it individually, except work has to
be done in a Timer Thread and will not be associated with the Application
Thread Pool (which is the preferred method!). See also
IThreadPool.executeAs(Runnable, boolean)
.
On the current thread however, after any completion, i.a. after a call to close() we release
the work unit association. This is for resource consumption reasons (and not to hold on to something
that should be collectable) but also so that we can normally see whether a thread
has been setup correctly during getCurrent(). We want to know (by exception) if a thread
is querying for a work unit although it has not been properly initialized - which in
turn indicates that it will most likely not be completed properly either.Modifier and Type | Method | Description |
---|---|---|
static void |
attach(WorkUnit unit) |
attaches a given work unit with the current thread.
|
void |
bindResource(java.lang.String key,
IWorkResource resource) |
bind and, if unit is stateful, begin resource
|
void |
close() |
close work unit.
|
static void |
closeCurrent() |
closes the current work unit (if any)
|
void |
commit() |
commit wu and and reset
|
static void |
commitCurrent() |
commits the current work unit (if any)
|
static WorkUnit |
detach() |
detaches current work unit (if any) from the current thread
|
static WorkUnit |
getCurrent() |
gets current work unit.
|
IWorkResource |
getResource(java.lang.String key) |
returns a named work resource
|
boolean |
getRollbackOnly() |
gets whether this unit is to be rolled back on close
|
static boolean |
getRollbackOnlyCurrent() |
gets whether this unit is to be rolled back on close
|
static void |
initCurrent() |
initializes a work unit orderly on a thread.
|
static WorkUnit |
queryCurrent() |
queries current work unit but will not instantiate one.
|
void |
rollback() |
rollback any transaction and reset
|
static void |
rollBackCurrent() |
rolls back the current work unit (if any)
|
void |
setRollbackOnly() |
sets this unit to be rolled back on close
|
static void |
setRollbackOnlyCurrent() |
sets this unit to be rolled back on close
|
IWorkResource |
unbindResource(java.lang.String key) |
unbind and if unit stateful rolls back resource
|
static void |
work(java.lang.Runnable r) |
Execute a runnable within a complete work unit, incl.
|
static <T> T |
work(java.util.concurrent.Callable<T> r) |
Execute a callable within a complete work unit, incl.
|
public static final WorkUnit queryCurrent()
public static final WorkUnit getCurrent()
public static void commitCurrent()
public static void rollBackCurrent()
public static void initCurrent()
public static void closeCurrent()
public static void setRollbackOnlyCurrent()
public static boolean getRollbackOnlyCurrent()
public static WorkUnit detach()
public static void attach(WorkUnit unit)
public void commit()
public void rollback()
public void setRollbackOnly()
public boolean getRollbackOnly()
public void close()
public void bindResource(java.lang.String key, IWorkResource resource)
public IWorkResource unbindResource(java.lang.String key)
public IWorkResource getResource(java.lang.String key)
public static void work(java.lang.Runnable r)
public static <T> T work(java.util.concurrent.Callable<T> r) throws java.lang.Exception
java.lang.Exception