001/**
002 * Copyright 2005-2017 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 */
016package org.kuali.rice.ksb.messaging.threadpool;
017
018import org.apache.log4j.Logger;
019import org.kuali.rice.core.api.config.property.ConfigContext;
020import org.kuali.rice.ksb.util.KSBConstants;
021
022import java.util.concurrent.Executors;
023import java.util.concurrent.ScheduledThreadPoolExecutor;
024import java.util.concurrent.ThreadFactory;
025import java.util.concurrent.TimeUnit;
026
027public 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            int pendingTasks = this.shutdownNow().size();
057            LOG.info(pendingTasks + " pending tasks...");
058                        LOG.info("awaiting termination: " + this.awaitTermination(20, TimeUnit.SECONDS));
059                        LOG.info("...KSB scheduled thread pool successfully stopped, isShutdown=" + this.isShutdown() + ", isTerminated=" + this.isTerminated());
060                } catch (Exception e) {
061                        LOG.warn("Exception thrown shutting down " + KSBScheduledThreadPoolExecutor.class.getSimpleName(), e);
062                }
063
064        }
065        
066        private static class KSBThreadFactory implements ThreadFactory {
067                
068                private ThreadFactory defaultThreadFactory = Executors.defaultThreadFactory();
069                
070                public Thread newThread(Runnable runnable) {
071                        Thread thread = defaultThreadFactory.newThread(runnable);
072                        thread.setName("KSB-Scheduled-" + thread.getName());
073                        return thread;
074            }
075        }
076
077}