1 | |
|
2 | |
|
3 | |
|
4 | |
|
5 | |
|
6 | |
|
7 | |
|
8 | |
|
9 | |
|
10 | |
|
11 | |
|
12 | |
|
13 | |
|
14 | |
|
15 | |
|
16 | |
package org.kuali.rice.ksb.messaging.threadpool; |
17 | |
|
18 | |
import org.apache.log4j.Logger; |
19 | |
import org.kuali.rice.core.config.Config; |
20 | |
import org.kuali.rice.core.config.ConfigContext; |
21 | |
import org.kuali.rice.core.util.ClassLoaderUtils; |
22 | |
|
23 | |
import java.util.concurrent.Executors; |
24 | |
import java.util.concurrent.PriorityBlockingQueue; |
25 | |
import java.util.concurrent.ThreadFactory; |
26 | |
import java.util.concurrent.ThreadPoolExecutor; |
27 | |
import java.util.concurrent.TimeUnit; |
28 | |
|
29 | |
|
30 | |
|
31 | |
|
32 | |
|
33 | |
|
34 | |
public class KSBThreadPoolImpl extends ThreadPoolExecutor implements KSBThreadPool { |
35 | |
|
36 | 0 | private static final Logger LOG = Logger.getLogger(KSBThreadPoolImpl.class); |
37 | |
|
38 | |
public static final int DEFAULT_POOL_SIZE = 5; |
39 | |
|
40 | |
private boolean started; |
41 | |
private boolean poolSizeSet; |
42 | |
|
43 | |
public KSBThreadPoolImpl() { |
44 | 0 | super(DEFAULT_POOL_SIZE, DEFAULT_POOL_SIZE, 60, TimeUnit.SECONDS, new PriorityBlockingQueue(1, new PriorityBlockingQueuePersistedMessageComparator()), new KSBThreadFactory(ClassLoaderUtils.getDefaultClassLoader()), new ThreadPoolExecutor.AbortPolicy()); |
45 | 0 | } |
46 | |
|
47 | |
public void setCorePoolSize(int corePoolSize) { |
48 | 0 | LOG.info("Setting core pool size to " + corePoolSize + " threads."); |
49 | 0 | super.setCorePoolSize(corePoolSize); |
50 | 0 | this.poolSizeSet = true; |
51 | 0 | } |
52 | |
|
53 | |
public long getKeepAliveTime() { |
54 | 0 | return super.getKeepAliveTime(TimeUnit.MILLISECONDS); |
55 | |
} |
56 | |
|
57 | |
public boolean isStarted() { |
58 | 0 | return this.started; |
59 | |
} |
60 | |
|
61 | |
public void start() throws Exception { |
62 | 0 | LOG.info("Starting the KSB thread pool..."); |
63 | 0 | loadSettings(); |
64 | 0 | this.started = true; |
65 | 0 | LOG.info("...KSB thread pool successfully started."); |
66 | 0 | } |
67 | |
|
68 | |
public void stop() throws Exception { |
69 | 0 | if (isStarted()) { |
70 | 0 | LOG.info("Shutting down KSB thread pool..."); |
71 | 0 | this.shutdownNow(); |
72 | 0 | this.started = false; |
73 | 0 | LOG.info("...KSB thread pool successfully shut down."); |
74 | |
} |
75 | 0 | } |
76 | |
|
77 | |
|
78 | |
|
79 | |
|
80 | |
protected void loadSettings() { |
81 | 0 | String threadPoolSizeStr = ConfigContext.getCurrentContextConfig().getProperty(Config.THREAD_POOL_SIZE); |
82 | 0 | if (!this.poolSizeSet) { |
83 | 0 | int poolSize = DEFAULT_POOL_SIZE; |
84 | |
try { |
85 | 0 | poolSize = new Integer(threadPoolSizeStr); |
86 | 0 | } catch (NumberFormatException nfe) { |
87 | 0 | LOG.error( "loadSettings(): Unable to parse the pool size: '"+threadPoolSizeStr+"'"); |
88 | 0 | } |
89 | 0 | setCorePoolSize(poolSize); |
90 | |
} |
91 | 0 | } |
92 | |
|
93 | |
public Object getInstance() { |
94 | 0 | return this; |
95 | |
} |
96 | |
|
97 | |
|
98 | |
|
99 | |
|
100 | |
|
101 | |
|
102 | |
|
103 | |
|
104 | |
|
105 | |
|
106 | |
|
107 | |
|
108 | |
|
109 | |
private static class KSBThreadFactory implements ThreadFactory { |
110 | |
|
111 | 0 | private static int factorySequence = 0; |
112 | |
|
113 | 0 | private static int threadSequence = 0; |
114 | |
|
115 | 0 | private ThreadFactory defaultThreadFactory = Executors.defaultThreadFactory(); |
116 | |
|
117 | |
private ClassLoader contextClassLoader; |
118 | |
|
119 | 0 | public KSBThreadFactory(ClassLoader contextClassLoader) { |
120 | 0 | this.contextClassLoader = contextClassLoader; |
121 | 0 | factorySequence++; |
122 | 0 | } |
123 | |
|
124 | |
public Thread newThread(Runnable runnable) { |
125 | 0 | threadSequence++; |
126 | 0 | Thread thread = this.defaultThreadFactory.newThread(runnable); |
127 | |
|
128 | |
|
129 | 0 | thread.setContextClassLoader(contextClassLoader); |
130 | 0 | thread.setName(ConfigContext.getCurrentContextConfig().getServiceNamespace() + "/KSB-pool-" + factorySequence + "-thread-" |
131 | |
+ threadSequence); |
132 | 0 | return thread; |
133 | |
} |
134 | |
|
135 | |
} |
136 | |
} |