Class JiraThreadLocalUtils

java.lang.Object
com.atlassian.jira.util.thread.JiraThreadLocalUtils

public class JiraThreadLocalUtils extends Object
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 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 clean ThreadLocal environment 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 if forceClose is true. This means something went wrong (more preCall() then postCall() on the thread stack and we want to make sure the thread is clean. Note: only call this if you just did a postCall() and you can not be part of an outer context
      Parameters:
      forceClose - when true force close the context
    • wrap

      public static Runnable wrap(Runnable runnable)
      Wraps a Runnable in a "before call" to preCall() and "after call" to postCall() 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 a finally block to clear up ThreadLocals once the runnable stuff has been done.
      Parameters:
      log - the log to write error messages to in casse of any problems
      warningCallback - 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 be null, in which case those problems are logged as errors.
    • postCall

      public static void postCall(@Nonnull org.slf4j.Logger log, @Nullable JiraThreadLocalUtil.WarningCallback warningCallback)
    • wrap

      public static <T> Callable<T> wrap(Callable<T> callable)
      Wraps a Callable in a "before call" to preCall() and "after call" to postCall() which handles managing Jira thread locals.