View Javadoc

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