Package com.atlassian.jira.util.thread
Class JiraThreadLocalUtils
java.lang.Object
com.atlassian.jira.util.thread.JiraThreadLocalUtils
This class has static methods that perform a number of standard operations at the start and
end of "runnable code" such as a
JiraServiceContainerImpl
or a TaskManagerImpl. Plugin developers that have previously
used this class directly should change to using JiraThreadLocalUtil from the API,
so that they can avoid having to depend on jira-core.
The main purpose of this class is to setup and clear ThreadLocal
variables that can otherwise interfere with the smooth running of JIRA
by leaking resources or polluting the information used by the next request.
You MUST remember to call postCall(Logger, WarningCallback) in a
finally block to guarantee correct behaviour. For example:
public void run()
{
JiraThreadLocalUtils.preCall();
try
{
// do runnable code here
}
finally
{
JiraThreadLocalUtils.postCall(log, myWarningCallback);
}
}
- Since:
- v3.13
- See Also:
-
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionstatic voidcheckClosed(boolean forceClose) This can be triggered when we are sure the context should be closed.static booleanstatic voidpostCall()static voidpostCall(org.apache.log4j.Logger log, JiraThreadLocalUtil.WarningCallback warningCallback) This should be called in afinallyblock to clear upThreadLocals once the runnable stuff has been done.static voidpostCall(org.slf4j.Logger log, JiraThreadLocalUtil.WarningCallback warningCallback) static voidpreCall()This should be called before any "runnable code" is called.static RunnableWraps aRunnablein a "before call" topreCall()and "after call" topostCall()which handles managing Jira thread locals.static <T> Callable<T> Wraps aCallablein a "before call" topreCall()and "after call" topostCall()which handles managing Jira thread locals.
-
Constructor Details
-
JiraThreadLocalUtils
public JiraThreadLocalUtils()
-
-
Method Details
-
preCall
public static void preCall()This should be called before any "runnable code" is called. This will setup a cleanThreadLocalenvironment for the runnable code to execute in. -
inContext
public static boolean inContext() -
postCall
public static void postCall() -
checkClosed
public static void checkClosed(boolean forceClose) This can be triggered when we are sure the context should be closed. It will log a warning if this is not the case and force close ifforceCloseis true. This means something went wrong (morepreCall()thenpostCall()on the thread stack and we want to make sure the thread is clean. Note: only call this if you just did apostCall()and you can not be part of an outer context- Parameters:
forceClose- when true force close the context
-
wrap
Wraps aRunnablein a "before call" topreCall()and "after call" topostCall()which handles managing Jira thread locals. -
postCall
public static void postCall(@Nonnull org.apache.log4j.Logger log, @Nullable JiraThreadLocalUtil.WarningCallback warningCallback) This should be called in afinallyblock to clear upThreadLocals once the runnable stuff has been done.- Parameters:
log- the log to write error messages to in casse of any problemswarningCallback- the callback to invoke in case where problems are detected after the runnable code is done running and its not cleaned up properly. This may benull, in which case those problems are logged as errors.
-
postCall
public static void postCall(@Nonnull org.slf4j.Logger log, @Nullable JiraThreadLocalUtil.WarningCallback warningCallback) -
wrap
Wraps aCallablein a "before call" topreCall()and "after call" topostCall()which handles managing Jira thread locals.
-