java.lang.AutoCloseable
public final class WorkUnit
extends java.lang.Object
implements java.lang.AutoCloseable
AutoCloseable
or explicitly:
try (WorkUnit.initCurrent()) { ... }or
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 work unit.
|
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 the current work unit, if any, is to be rolled back on close.
|
static WorkUnit |
initCurrent() |
initializes a work unit orderly on a thread join the current, if present.
|
static WorkUnit |
queryCurrent() |
queries current work unit but will not instantiate one.
|
void |
rollback() |
Rollback the work unit.
|
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 the current work unit, if any, 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.
|
static <T> T |
work(java.util.function.Supplier<T> supplier) |
Execute a runnable 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 WorkUnit initCurrent()
public static void closeCurrent()
close()
.public static void setRollbackOnlyCurrent()
setRollbackOnly()
public static boolean getRollbackOnlyCurrent()
getRollbackOnly()
.public static WorkUnit detach()
public static void attach(WorkUnit unit)
public void commit()
IWorkResource.beforeCompletion(boolean)
.
If during that phase the work unit is set to rollback only, a rollback is performed. See rollback()
.
Otherwise all resources receive a IWorkResource.commit()
before all resources receive a IWorkResource.afterCompletion(boolean)
.public void rollback()
IWorkResource
for more details.public void setRollbackOnly()
rollback()
, if set to true
public boolean getRollbackOnly()
public void close()
initCurrent()
is either opening a new work unit of joining an existing. In the latter case, a use counter is incremented.
A call to close only performs the actual close action, when the use counter is 1. Otherwise the use counter will simply be decreased.
The actual close action is rollback()
, if the work unit was set to rollback only (via setRollbackOnly()
or setRollbackOnlyCurrent()
) or
commit()
otherwise. In both cases, all enlisted resources will receive a close event. See IWorkResource
for more details.
If this work unit is the current work unit, it will be detached and there will be no current work unit anymore.close
in interface java.lang.AutoCloseable
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.function.Supplier<T> supplier)
public static <T> T work(java.util.concurrent.Callable<T> r) throws java.lang.Exception
java.lang.Exception