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