View Javadoc
1   /*
2    * Copyright (c) MuleSoft, Inc.  All rights reserved.  http://www.mulesoft.com
3    * The software in this package is published under the terms of the CPAL v1.0
4    * license, a copy of which has been included with this distribution in the
5    * LICENSE.txt file.
6    */
7   package org.mule.util.concurrent;
8   
9   import org.mule.api.MuleContext;
10  import org.mule.util.StringUtils;
11  
12  /**
13   * Encapsulates thread naming logic for Mule standalone and embedded scenarios.
14   */
15  public class ThreadNameHelper
16  {
17  
18      private ThreadNameHelper()
19      {
20          // do not instantiate
21      }
22  
23      public static String receiver(MuleContext muleContext, String connectorName)
24      {
25          return String.format("%s%s.receiver", getPrefix(muleContext), connectorName);
26      }
27  
28      public static String dispatcher(MuleContext muleContext, String connectorName)
29      {
30          return String.format("%s%s.dispatcher", getPrefix(muleContext), connectorName);
31      }
32  
33      public static String requester(MuleContext muleContext, String connectorName)
34      {
35          return String.format("%s%s.requester", getPrefix(muleContext), connectorName);
36      }
37  
38      public static String asyncProcessor(MuleContext muleContext, String mpName)
39      {
40          return String.format("%s%s.processor.async", getPrefix(muleContext), mpName);
41      }
42  
43      public static String sedaService(MuleContext muleContext, String name)
44      {
45          return String.format("%sseda.%s", getPrefix(muleContext), name);
46      }
47  
48      public static String flow(MuleContext muleContext, String name)
49      {
50          return String.format("%sflow.%s", getPrefix(muleContext), name);
51  
52      }
53  
54      /**
55       * Generate a generic thread name prefix for this context.
56       * @param muleContext context to generate the name prefix for
57       * @return "[appName]." if Mule is running as a container, otherwise empty string
58       */
59      public static String getPrefix(MuleContext muleContext)
60      {
61          final boolean containerMode = muleContext.getConfiguration().isContainerMode();
62          final String id = muleContext.getConfiguration().getId();
63  
64          return containerMode ? String.format("[%s].", id) : StringUtils.EMPTY;
65      }
66  }