1 | |
|
2 | |
|
3 | |
|
4 | |
|
5 | |
|
6 | |
|
7 | |
|
8 | |
|
9 | |
|
10 | |
|
11 | |
|
12 | |
|
13 | |
|
14 | |
|
15 | |
|
16 | |
package org.kuali.rice.ksb.messaging.threadpool; |
17 | |
import java.util.concurrent.Executors; |
18 | |
import java.util.concurrent.ScheduledThreadPoolExecutor; |
19 | |
import java.util.concurrent.ThreadFactory; |
20 | |
import java.util.concurrent.TimeUnit; |
21 | |
|
22 | |
import org.apache.log4j.Logger; |
23 | |
import org.kuali.rice.core.config.ConfigContext; |
24 | |
import org.kuali.rice.ksb.util.KSBConstants; |
25 | |
|
26 | |
public class KSBScheduledThreadPoolExecutor extends ScheduledThreadPoolExecutor implements KSBScheduledPool { |
27 | |
|
28 | 0 | private static final Logger LOG = Logger.getLogger(KSBScheduledThreadPoolExecutor.class); |
29 | |
|
30 | |
private boolean started; |
31 | |
private static final int DEFAULT_SIZE = 2; |
32 | |
|
33 | |
public KSBScheduledThreadPoolExecutor() { |
34 | 0 | super(DEFAULT_SIZE, new KSBThreadFactory()); |
35 | 0 | } |
36 | |
|
37 | |
public boolean isStarted() { |
38 | 0 | return started; |
39 | |
} |
40 | |
|
41 | |
public void start() throws Exception { |
42 | 0 | LOG.info("Starting the KSB scheduled thread pool..."); |
43 | |
try { |
44 | 0 | Integer size = new Integer(ConfigContext.getCurrentContextConfig().getProperty(KSBConstants.FIXED_POOL_SIZE)); |
45 | 0 | this.setCorePoolSize(size); |
46 | 0 | } catch (NumberFormatException nfe) { |
47 | |
|
48 | 0 | } |
49 | 0 | LOG.info("...KSB scheduled thread pool successfully started."); |
50 | 0 | } |
51 | |
|
52 | |
public void stop() throws Exception { |
53 | 0 | LOG.info("Stopping the KSB scheduled thread pool..."); |
54 | |
try { |
55 | 0 | this.shutdownNow(); |
56 | 0 | LOG.info("awaiting termination: " + this.awaitTermination(20, TimeUnit.SECONDS)); |
57 | 0 | LOG.info("...KSB scheduled thread pool successfully stopped, isShutdown=" + this.isShutdown()); |
58 | 0 | } catch (Exception e) { |
59 | 0 | LOG.warn("Exception thrown shutting down " + KSBScheduledThreadPoolExecutor.class.getSimpleName(), e); |
60 | 0 | } |
61 | |
|
62 | 0 | } |
63 | |
|
64 | 0 | private static class KSBThreadFactory implements ThreadFactory { |
65 | |
|
66 | 0 | private ThreadFactory defaultThreadFactory = Executors.defaultThreadFactory(); |
67 | |
|
68 | |
public Thread newThread(Runnable runnable) { |
69 | 0 | Thread thread = defaultThreadFactory.newThread(runnable); |
70 | 0 | thread.setName("KSB-Scheduled-" + thread.getName()); |
71 | 0 | return thread; |
72 | |
} |
73 | |
} |
74 | |
|
75 | |
} |