001: /*
002: * Copyright 2007 The Kuali Foundation
003: *
004: * Licensed under the Educational Community License, Version 1.0 (the "License");
005: * you may not use this file except in compliance with the License.
006: * You may obtain a copy of the License at
007: *
008: * http://www.opensource.org/licenses/ecl1.php
009: *
010: * Unless required by applicable law or agreed to in writing, software
011: * distributed under the License is distributed on an "AS IS" BASIS,
012: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013: * See the License for the specific language governing permissions and
014: * limitations under the License.
015: */
016: package edu.iu.uis.eden.messaging.threadpool;
017:
018: import org.apache.log4j.Logger;
019: import org.kuali.rice.config.Config;
020: import org.kuali.rice.core.Core;
021:
022: import edu.emory.mathcs.backport.java.util.concurrent.Executors;
023: import edu.emory.mathcs.backport.java.util.concurrent.PriorityBlockingQueue;
024: import edu.emory.mathcs.backport.java.util.concurrent.ThreadFactory;
025: import edu.emory.mathcs.backport.java.util.concurrent.ThreadPoolExecutor;
026: import edu.emory.mathcs.backport.java.util.concurrent.TimeUnit;
027:
028: /**
029: * A Thread Pool implementation for the KSB which implements a thread pool backed by a configuration store.
030: *
031: * @author Kuali Rice Team (kuali-rice@googlegroups.com)
032: */
033: public class KSBThreadPoolImpl extends ThreadPoolExecutor implements
034: KSBThreadPool {
035:
036: private static final Logger LOG = Logger
037: .getLogger(KSBThreadPoolImpl.class);
038:
039: public static final int DEFAULT_POOL_SIZE = 5;
040:
041: private boolean started;
042: private boolean poolSizeSet;
043:
044: public KSBThreadPoolImpl() {
045: super (
046: DEFAULT_POOL_SIZE,
047: DEFAULT_POOL_SIZE,
048: 60,
049: TimeUnit.SECONDS,
050: new PriorityBlockingQueue(
051: 1,
052: new PriorityBlockingQueuePersistedMessageComparator()),
053: new KSBThreadFactory(),
054: new ThreadPoolExecutor.AbortPolicy());
055: }
056:
057: public void setCorePoolSize(int corePoolSize) {
058: LOG.info("Setting core pool size to " + corePoolSize
059: + " threads.");
060: super .setCorePoolSize(corePoolSize);
061: this .poolSizeSet = true;
062: }
063:
064: public long getKeepAliveTime() {
065: return super .getKeepAliveTime(TimeUnit.MILLISECONDS);
066: }
067:
068: public boolean isStarted() {
069: return this .started;
070: }
071:
072: public void start() throws Exception {
073:
074: }
075:
076: public void stop() throws Exception {
077: LOG.info("Shutting down KSB threadpool.");
078: if (isStarted()) {
079: this .shutdownNow();
080: this .started = false;
081: }
082: }
083:
084: /**
085: * Loads the thread pool settings from the DAO.
086: */
087: protected void loadSettings() {
088: Core.getCurrentContextConfig().getProperty(
089: Config.THREAD_POOL_SIZE);
090:
091: if (!this .poolSizeSet) {
092: int poolSize;
093: try {
094: poolSize = new Integer(Core.getCurrentContextConfig()
095: .getProperty(Config.THREAD_POOL_SIZE));
096: } catch (NumberFormatException nfe) {
097: poolSize = -1;
098: }
099: if (poolSize == -1) {
100: poolSize = DEFAULT_POOL_SIZE;
101: }
102: setCorePoolSize(poolSize);
103: }
104:
105: }
106:
107: public Object getInstance() {
108: return this ;
109: }
110:
111: /**
112: * A simple ThreadFactory which names the thread as follows:<br>
113: * <br>
114: *
115: * <i>messageEntity</i>/KSB-pool-<i>m</i>-thread-<i>n</i><br>
116: * <br>
117: *
118: * Where <i>messageEntity</i> is the message entity of the application running the thread pool, <i>m</i> is the
119: * sequence number of the factory and <i>n</i> is the sequence number of the thread within the factory.
120: *
121: * @author Kuali Rice Team (kuali-rice@googlegroups.com)
122: */
123: private static class KSBThreadFactory implements ThreadFactory {
124:
125: private static int factorySequence = 0;
126:
127: private static int threadSequence = 0;
128:
129: private ThreadFactory defaultThreadFactory = Executors
130: .defaultThreadFactory();
131:
132: public KSBThreadFactory() {
133: factorySequence++;
134: }
135:
136: public Thread newThread(Runnable runnable) {
137: threadSequence++;
138: Thread thread = this .defaultThreadFactory
139: .newThread(runnable);
140: thread.setName(Core.getCurrentContextConfig()
141: .getMessageEntity()
142: + "/KSB-pool-"
143: + factorySequence
144: + "-thread-"
145: + threadSequence);
146: return thread;
147: }
148:
149: }
150: }
|