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.module.launcher;
8   
9   import java.util.concurrent.ThreadFactory;
10  import java.util.concurrent.atomic.AtomicInteger;
11  
12  /**
13   * A slightly tweaked default thread factory that uses the following pattern:
14   * <code>[%s].config.change.%d.thread.%d</code>, where %s stands for application name,
15   * the next number will tell one how many redeployments this app had during this container's
16   * lifetime and the last digit, thread count, should always be 1. Left there for debugging
17   * purposes to quickly locate any duplicate threads trying to perform a redeploy. 
18   */
19  public class ConfigChangeMonitorThreadFactory implements ThreadFactory
20  {
21  
22      static final AtomicInteger poolNumber = new AtomicInteger(1);
23      final ThreadGroup group;
24      final AtomicInteger threadNumber = new AtomicInteger(1);
25      final String namePrefix;
26      final String appName;
27  
28      public ConfigChangeMonitorThreadFactory(String appName)
29      {
30          this.appName = appName;
31          SecurityManager s = System.getSecurityManager();
32          group = (s != null) ? s.getThreadGroup() :
33                  Thread.currentThread().getThreadGroup();
34          namePrefix = String.format("[%s].config.change.%d.thread.", appName, poolNumber.getAndIncrement());
35      }
36  
37      public Thread newThread(Runnable r)
38      {
39          Thread t = new Thread(group, r,
40                                namePrefix + threadNumber.getAndIncrement(),
41                                0);
42          if (t.isDaemon())
43          {
44              t.setDaemon(false);
45          }
46          if (t.getPriority() != Thread.NORM_PRIORITY)
47          {
48              t.setPriority(Thread.NORM_PRIORITY);
49          }
50          return t;
51      }
52  
53  
54  }