View Javadoc

1   /*
2    * $Id: AppDeployerMonitorThreadFactory.java 21453 2011-03-04 14:12:40Z aperepel $
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.module.launcher;
12  
13  import org.mule.util.concurrent.LoggingUncaughtExceptionHandler;
14  
15  import java.util.concurrent.ThreadFactory;
16  import java.util.concurrent.atomic.AtomicInteger;
17  
18  public class AppDeployerMonitorThreadFactory implements ThreadFactory
19  {
20  
21      static final AtomicInteger poolNumber = new AtomicInteger(1);
22      final ThreadGroup group;
23      final AtomicInteger threadNumber = new AtomicInteger(1);
24      final String namePrefix;
25  
26      public AppDeployerMonitorThreadFactory()
27      {
28          SecurityManager s = System.getSecurityManager();
29          group = (s != null) ? s.getThreadGroup() :
30                  Thread.currentThread().getThreadGroup();
31          namePrefix = String.format("Mule.app.deployer.monitor.%d.thread.",  poolNumber.getAndIncrement());
32      }
33  
34      public Thread newThread(Runnable r)
35      {
36          Thread t = new Thread(group, r,
37                                namePrefix + threadNumber.getAndIncrement(),
38                                0);
39          // make sure it's non-daemon, allows for an 'idle' state of Mule by preventing early termination
40          t.setDaemon(false);
41          t.setPriority(Thread.MIN_PRIORITY);
42          t.setUncaughtExceptionHandler(new LoggingUncaughtExceptionHandler());
43          return t;
44      }
45  
46  
47  }