View Javadoc

1   package org.eclipse.jetty.http.spi;
2   
3   //========================================================================
4   //Copyright (c) 2004-2009 Mort Bay Consulting Pty. Ltd.
5   //------------------------------------------------------------------------
6   //All rights reserved. This program and the accompanying materials
7   //are made available under the terms of the Eclipse Public License v1.0
8   //and Apache License v2.0 which accompanies this distribution.
9   //The Eclipse Public License is available at 
10  //http://www.eclipse.org/legal/epl-v10.html
11  //The Apache License v2.0 is available at
12  //http://www.opensource.org/licenses/apache2.0.php
13  //You may elect to redistribute this code under either of these licenses. 
14  //========================================================================
15  
16  import java.util.concurrent.RejectedExecutionException;
17  import java.util.concurrent.ThreadPoolExecutor;
18  import java.util.concurrent.TimeUnit;
19  
20  import org.eclipse.jetty.util.component.AbstractLifeCycle;
21  import org.eclipse.jetty.util.log.Log;
22  import org.eclipse.jetty.util.log.Logger;
23  
24  import org.eclipse.jetty.util.thread.ThreadPool;
25  
26  /**
27   * Jetty {@link ThreadPool} that bridges requests to a {@link ThreadPoolExecutor}.
28   */
29  public class ThreadPoolExecutorAdapter extends AbstractLifeCycle implements ThreadPool
30  {
31      private static final Logger LOG = Log.getLogger(ThreadPoolExecutorAdapter.class);
32  
33  	
34  	private ThreadPoolExecutor executor;
35  	
36  	public ThreadPoolExecutorAdapter(ThreadPoolExecutor executor)
37  	{
38  		this.executor = executor;
39  	}
40  
41  	public boolean dispatch(Runnable job)
42  	{
43  		try
44          {       
45  			executor.execute(job);
46              return true;
47          }
48          catch(RejectedExecutionException e)
49          {
50              LOG.warn(e);
51              return false;
52          }
53  	}
54  
55  	public int getIdleThreads()
56  	{
57  		return executor.getPoolSize()-executor.getActiveCount();
58  	}
59  
60  	public int getThreads()
61  	{
62  		return executor.getPoolSize();
63  	}
64  
65  	public boolean isLowOnThreads()
66  	{
67  		return executor.getActiveCount()>=executor.getMaximumPoolSize();
68  	}
69  
70  	public void join() throws InterruptedException
71  	{
72  		executor.awaitTermination(Long.MAX_VALUE, TimeUnit.MILLISECONDS);
73  	}
74  
75  	
76  	
77  	public boolean isFailed()
78  	{
79  		return false;
80  	}
81  
82  	public boolean isRunning()
83  	{
84  		return !executor.isTerminated() && !executor.isTerminating();
85  	}
86  
87  	public boolean isStarted()
88  	{
89  		return !executor.isTerminated() && !executor.isTerminating();
90  	}
91  
92  	public boolean isStarting()
93  	{
94  		return false;
95  	}
96  
97  	public boolean isStopped()
98  	{
99  		return executor.isTerminated();
100 	}
101 
102 	public boolean isStopping()
103 	{
104 		return executor.isTerminating();
105 	}
106 
107 	protected void doStart() throws Exception
108 	{
109 		if (executor.isTerminated() || executor.isTerminating() || executor.isShutdown())
110             throw new IllegalStateException("Cannot restart");
111 	}
112 
113 	protected void doStop() throws Exception
114 	{
115 		executor.shutdown();
116         if (!executor.awaitTermination(60, TimeUnit.SECONDS))
117         	executor.shutdownNow();
118 	}
119 
120 }