1 //========================================================================
2 //$$Id: ThreadPoolExecutorAdapter.java,v 1.3 2007/11/02 12:39:41 ludovic_orban Exp $$
3 //
4 //------------------------------------------------------------------------
5 //Licensed under the Apache License, Version 2.0 (the "License");
6 //you may not use this file except in compliance with the License.
7 //You may obtain a copy of the License at
8 //http://www.apache.org/licenses/LICENSE-2.0
9 //Unless required by applicable law or agreed to in writing, software
10 //distributed under the License is distributed on an "AS IS" BASIS,
11 //WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 //See the License for the specific language governing permissions and
13 //limitations under the License.
14 //========================================================================
15 
16 package org.mortbay.jetty.j2se6;
17 
18 import java.util.concurrent.RejectedExecutionException;
19 import java.util.concurrent.ThreadPoolExecutor;
20 import java.util.concurrent.TimeUnit;
21 
22 import org.mortbay.component.LifeCycle;
23 import org.mortbay.log.Log;
24 import org.mortbay.thread.ThreadPool;
25 
26 /**
27  * Jetty {@link ThreadPool} that bridges requests to a {@link ThreadPoolExecutor}.
28  * @author lorban
29  */
30 public class ThreadPoolExecutorAdapter implements ThreadPool, LifeCycle
31 {
32 
33 	private ThreadPoolExecutor executor;
34 
ThreadPoolExecutorAdapter(ThreadPoolExecutor executor)35 	public ThreadPoolExecutorAdapter(ThreadPoolExecutor executor)
36 	{
37 		this.executor = executor;
38 	}
39 
dispatch(Runnable job)40 	public boolean dispatch(Runnable job)
41 	{
42 		try
43         {
44 			executor.execute(job);
45             return true;
46         }
47         catch(RejectedExecutionException e)
48         {
49             Log.warn(e);
50             return false;
51         }
52 	}
53 
getIdleThreads()54 	public int getIdleThreads()
55 	{
56 		return executor.getPoolSize()-executor.getActiveCount();
57 	}
58 
getThreads()59 	public int getThreads()
60 	{
61 		return executor.getPoolSize();
62 	}
63 
isLowOnThreads()64 	public boolean isLowOnThreads()
65 	{
66 		return executor.getActiveCount()>=executor.getMaximumPoolSize();
67 	}
68 
join()69 	public void join() throws InterruptedException
70 	{
71 		executor.awaitTermination(Long.MAX_VALUE, TimeUnit.MILLISECONDS);
72 	}
73 
74 
75 
isFailed()76 	public boolean isFailed()
77 	{
78 		return false;
79 	}
80 
isRunning()81 	public boolean isRunning()
82 	{
83 		return !executor.isTerminated() && !executor.isTerminating();
84 	}
85 
isStarted()86 	public boolean isStarted()
87 	{
88 		return !executor.isTerminated() && !executor.isTerminating();
89 	}
90 
isStarting()91 	public boolean isStarting()
92 	{
93 		return false;
94 	}
95 
isStopped()96 	public boolean isStopped()
97 	{
98 		return executor.isTerminated();
99 	}
100 
isStopping()101 	public boolean isStopping()
102 	{
103 		return executor.isTerminating();
104 	}
105 
start()106 	public void start() throws Exception
107 	{
108 		if (executor.isTerminated() || executor.isTerminating() || executor.isShutdown())
109             throw new IllegalStateException("Cannot restart");
110 	}
111 
stop()112 	public void stop() throws Exception
113 	{
114 		executor.shutdown();
115         if (!executor.awaitTermination(60,TimeUnit.SECONDS))
116         	executor.shutdownNow();
117 	}
118 
119 }
120