1 | |
|
2 | |
|
3 | |
|
4 | |
|
5 | |
|
6 | |
|
7 | |
package org.mule.util.concurrent; |
8 | |
|
9 | |
import edu.emory.mathcs.backport.java.util.concurrent.RejectedExecutionException; |
10 | |
import edu.emory.mathcs.backport.java.util.concurrent.RejectedExecutionHandler; |
11 | |
import edu.emory.mathcs.backport.java.util.concurrent.ThreadPoolExecutor; |
12 | |
import edu.emory.mathcs.backport.java.util.concurrent.TimeUnit; |
13 | |
|
14 | |
|
15 | |
|
16 | |
|
17 | |
|
18 | |
|
19 | |
|
20 | |
public class WaitPolicy implements RejectedExecutionHandler |
21 | |
{ |
22 | |
private final long time; |
23 | |
private final TimeUnit timeUnit; |
24 | |
|
25 | |
|
26 | |
|
27 | |
|
28 | |
public WaitPolicy() |
29 | |
{ |
30 | |
|
31 | 0 | this(Long.MAX_VALUE, TimeUnit.SECONDS); |
32 | 0 | } |
33 | |
|
34 | |
|
35 | |
|
36 | |
|
37 | |
|
38 | |
public WaitPolicy(long time, TimeUnit timeUnit) |
39 | |
{ |
40 | 0 | super(); |
41 | 0 | this.time = (time < 0 ? Long.MAX_VALUE : time); |
42 | 0 | this.timeUnit = timeUnit; |
43 | 0 | } |
44 | |
|
45 | |
@SuppressWarnings("boxing") |
46 | |
public void rejectedExecution(Runnable r, ThreadPoolExecutor e) |
47 | |
{ |
48 | |
try |
49 | |
{ |
50 | 0 | if (e.isShutdown()) |
51 | |
{ |
52 | 0 | throw new RejectedExecutionException("ThreadPoolExecutor is already shut down"); |
53 | |
} |
54 | 0 | else if (!e.getQueue().offer(r, time, timeUnit)) |
55 | |
{ |
56 | 0 | String message = String.format("ThreadPoolExecutor did not accept within %1d %2s", |
57 | |
time, timeUnit); |
58 | 0 | throw new RejectedExecutionException(message); |
59 | |
} |
60 | |
} |
61 | 0 | catch (InterruptedException ie) |
62 | |
{ |
63 | 0 | Thread.currentThread().interrupt(); |
64 | 0 | throw new RejectedExecutionException(ie); |
65 | 0 | } |
66 | 0 | } |
67 | |
|
68 | |
} |