001    /**
002     * Copyright 2005-2012 The Kuali Foundation
003     *
004     * Licensed under the Educational Community License, Version 2.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/ecl2.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 org.kuali.rice.ksb.messaging.threadpool;
017    
018    import org.apache.log4j.Logger;
019    import org.kuali.rice.core.api.config.property.ConfigContext;
020    import org.kuali.rice.ksb.util.KSBConstants;
021    
022    import java.util.concurrent.Executors;
023    import java.util.concurrent.ScheduledThreadPoolExecutor;
024    import java.util.concurrent.ThreadFactory;
025    import java.util.concurrent.TimeUnit;
026    
027    public class KSBScheduledThreadPoolExecutor extends ScheduledThreadPoolExecutor implements KSBScheduledPool {
028    
029            private static final Logger LOG = Logger.getLogger(KSBScheduledThreadPoolExecutor.class);
030    
031            private boolean started;
032            private static final int DEFAULT_SIZE = 2;
033    
034            public KSBScheduledThreadPoolExecutor() {
035                    super(DEFAULT_SIZE, new KSBThreadFactory());
036            }
037    
038            public boolean isStarted() {
039                    return started;
040            }
041    
042            public void start() throws Exception {
043                    LOG.info("Starting the KSB scheduled thread pool...");
044                    try {
045                            Integer size = new Integer(ConfigContext.getCurrentContextConfig().getProperty(KSBConstants.Config.FIXED_POOL_SIZE));
046                            this.setCorePoolSize(size);
047                    } catch (NumberFormatException nfe) {
048                            // ignore this, instead the pool will be set to DEFAULT_SIZE
049                    }
050                    LOG.info("...KSB scheduled thread pool successfully started.");
051            }
052    
053            public void stop() throws Exception {
054                    LOG.info("Stopping the KSB scheduled thread pool...");
055                    try {
056                            this.shutdownNow();
057                            LOG.info("awaiting termination: " + this.awaitTermination(20, TimeUnit.SECONDS));
058                            LOG.info("...KSB scheduled thread pool successfully stopped, isShutdown=" + this.isShutdown());
059                    } catch (Exception e) {
060                            LOG.warn("Exception thrown shutting down " + KSBScheduledThreadPoolExecutor.class.getSimpleName(), e);
061                    }
062    
063            }
064            
065            private static class KSBThreadFactory implements ThreadFactory {
066                    
067                    private ThreadFactory defaultThreadFactory = Executors.defaultThreadFactory();
068                    
069                    public Thread newThread(Runnable runnable) {
070                            Thread thread = defaultThreadFactory.newThread(runnable);
071                            thread.setName("KSB-Scheduled-" + thread.getName());
072                            return thread;
073                }
074            }
075    
076    }