1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
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
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 }