View Javadoc

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