public class TaskContext extends Object implements SubContext
Any task or background job can access its TaskContext using either get()
or
CallContext.get(Class)
. This provides an interface to a monitoring system which
might be present (by calling setAdapter(TaskContextAdapter)
. If no
monitoring is available, the default mechanisms of the platform are used.
Modifier and Type | Field and Description |
---|---|
static String |
MDC_SYSTEM
One the system string is changed, it will be updated in the mapped diagnostic context (MDC) using this name.
|
Constructor and Description |
---|
TaskContext()
Generates a new TaskContext.
|
Modifier and Type | Method and Description |
---|---|
void |
cancel()
Cancels the execution of this task.
|
void |
detach()
Gets notified if the associated CallContext is detached.
|
SubContext |
fork()
Returns an instance which is used in a forked
CallContext . |
static TaskContext |
get()
Provides access to the TaskContext for the current thread.
|
TaskContextAdapter |
getAdapter()
Returns the monitoring adapter which is currently active.
|
String |
getJob()
Returns the Job component of the System String
|
String |
getSubSystem()
Returns the Sub-System component of the System String
|
String |
getSystem()
Returns the System component of the System String
|
String |
getSystemString()
Returns the System String.
|
boolean |
isActive()
Determines if the execution of this task is still active.
|
<T> void |
iterateWhileActive(Iterable<T> iterable,
Consumer<T> consumer)
Utility to iterate through a collection while checking the cancelled flag.
|
void |
log(String message,
Object... args)
Writes a log message to the monitor.
|
void |
logAsCurrentState(String message,
Object... args)
Logs the given message and sets it as current state.
|
void |
markErroneous()
Signals the monitor that the execution had an error.
|
void |
setAdapter(TaskContextAdapter adapter)
Installs the given adapter as monitoring adapter.
|
TaskContext |
setJob(String job)
Sets the Job component of the System String
|
void |
setState(String newState,
Object... args)
Sets the new state of the current task.
|
TaskContext |
setSubSystem(String subSystem)
Sets the Sub-System component of the System String
|
TaskContext |
setSystem(String system)
Sets the System component of the System String
|
RateLimit |
shouldUpdateState()
Can be used to determine if the state should be refreshed.
|
String |
toString() |
void |
trace(String message,
Object... args)
Writes a debug message to the monitor.
|
public static final String MDC_SYSTEM
public TaskContext()
Normally this is should only be invoked by CallContext
. Use CallContext.get(Class)
to obtain an
instance.
public static TaskContext get()
This is boilerplate for CallContext.getCurrent().get(TaskContext.class)
public void log(String message, Object... args)
If no monitor is available, the async logger will be used.
message
- the message to logargs
- the parameters used to format the message (see Strings.apply(String, Object...)
)public void trace(String message, Object... args)
If no monitor is available, the async logger will be used.
message
- the message to logargs
- the parameters used to format the message (see Strings.apply(String, Object...)
)public void logAsCurrentState(String message, Object... args)
message
- the message to logargs
- the parameters used to format the message (see Strings.apply(String, Object...)
)public void setState(String newState, Object... args)
newState
- the message to set as stateargs
- the parameters used to format the state message (see Strings.apply(String, Object...)
)public RateLimit shouldUpdateState()
By calling shouldUpdateState().check()
an inner loop can detect if a state update should be
performed. This will limit the number of updates to a reasonable value.
public void markErroneous()
Although an error is signaled, this will not cancel or interrupt the execution of the task. This is merely a signal for an user or administrator that an unexpected or non-anticipated event occurred.
public boolean isActive()
A task can be either stopped via the cancel()
method or due to a system shutdown. In any case it is
wise for a task to check this flag every once in a while to keep the overall app responsive.
public void cancel()
Note that this will not kill the underlying thread. This will merely toggle the canceled flag. It is however the task programmers job to check this flag and interrupt / terminate all computations.
public <T> void iterateWhileActive(Iterable<T> iterable, Consumer<T> consumer)
T
- the type of elements being processediterable
- the collection to iterate throughconsumer
- the processor invoked for each elementpublic String getSystemString()
This will consist of three parts: System, Sub-System and Job. It is used to provide information which
module is currently active. Therefore the System will provide a raw information which module is
active. This might be HTTP for the web server or the category of an executor in Tasks
.
The Sub-System will provide a more detailed information, like the class name or the name of a component which is currently active.
Finally the Job will provide a detailed information what's being currently processed. This might be the effective URI of the request being processed by the web server or the name of a file currently being imported.
public String getSystem()
getSystemString()
public TaskContext setSystem(String system)
system
- the new system component to setgetSystemString()
public String getSubSystem()
getSystemString()
public TaskContext setSubSystem(String subSystem)
subSystem
- the new sub system component to setgetSystemString()
public String getJob()
getSystemString()
public TaskContext setJob(String job)
job
- the new job component to setgetSystemString()
public TaskContextAdapter getAdapter()
public void setAdapter(TaskContextAdapter adapter)
adapter
- the adapter to installpublic SubContext fork()
SubContext
CallContext
.fork
in interface SubContext
public void detach()
SubContext
detach
in interface SubContext
Copyright © 2018. All rights reserved.