View Javadoc
1   /**
2    * Copyright 2005-2015 The Kuali Foundation
3    *
4    * Licensed under the Educational Community License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    * http://www.opensource.org/licenses/ecl2.php
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  package org.kuali.rice.ksb.messaging.threadpool;
17  
18  import org.apache.log4j.Logger;
19  import org.kuali.rice.core.api.config.property.ConfigContext;
20  import org.kuali.rice.ksb.util.KSBConstants;
21  
22  import java.util.concurrent.Executors;
23  import java.util.concurrent.ScheduledThreadPoolExecutor;
24  import java.util.concurrent.ThreadFactory;
25  import java.util.concurrent.TimeUnit;
26  
27  public class KSBScheduledThreadPoolExecutor extends ScheduledThreadPoolExecutor implements KSBScheduledPool {
28  
29  	private static final Logger LOG = Logger.getLogger(KSBScheduledThreadPoolExecutor.class);
30  
31  	private boolean started;
32  	private static final int DEFAULT_SIZE = 2;
33  
34  	public KSBScheduledThreadPoolExecutor() {
35  		super(DEFAULT_SIZE, new KSBThreadFactory());
36  	}
37  
38  	public boolean isStarted() {
39  		return started;
40  	}
41  
42  	public void start() throws Exception {
43  		LOG.info("Starting the KSB scheduled thread pool...");
44  		try {
45  			Integer size = new Integer(ConfigContext.getCurrentContextConfig().getProperty(KSBConstants.Config.FIXED_POOL_SIZE));
46  			this.setCorePoolSize(size);
47  		} catch (NumberFormatException nfe) {
48  			// ignore this, instead the pool will be set to DEFAULT_SIZE
49  		}
50  		LOG.info("...KSB scheduled thread pool successfully started.");
51  	}
52  
53  	public void stop() throws Exception {
54  		LOG.info("Stopping the KSB scheduled thread pool...");
55  		try {
56              int pendingTasks = this.shutdownNow().size();
57              LOG.info(pendingTasks + " pending tasks...");
58  			LOG.info("awaiting termination: " + this.awaitTermination(20, TimeUnit.SECONDS));
59  			LOG.info("...KSB scheduled thread pool successfully stopped, isShutdown=" + this.isShutdown() + ", isTerminated=" + this.isTerminated());
60  		} catch (Exception e) {
61  			LOG.warn("Exception thrown shutting down " + KSBScheduledThreadPoolExecutor.class.getSimpleName(), e);
62  		}
63  
64  	}
65  	
66  	private static class KSBThreadFactory implements ThreadFactory {
67  		
68  		private ThreadFactory defaultThreadFactory = Executors.defaultThreadFactory();
69  		
70  		public Thread newThread(Runnable runnable) {
71  			Thread thread = defaultThreadFactory.newThread(runnable);
72  			thread.setName("KSB-Scheduled-" + thread.getName());
73  			return thread;
74  	    }
75  	}
76  
77  }