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