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 this(Long.MAX_VALUE, TimeUnit.SECONDS);
32 }
33
34
35
36
37
38 public WaitPolicy(long time, TimeUnit timeUnit)
39 {
40 super();
41 this.time = (time < 0 ? Long.MAX_VALUE : time);
42 this.timeUnit = timeUnit;
43 }
44
45 @SuppressWarnings("boxing")
46 public void rejectedExecution(Runnable r, ThreadPoolExecutor e)
47 {
48 try
49 {
50 if (e.isShutdown())
51 {
52 throw new RejectedExecutionException("ThreadPoolExecutor is already shut down");
53 }
54 else if (!e.getQueue().offer(r, time, timeUnit))
55 {
56 String message = String.format("ThreadPoolExecutor did not accept within %1d %2s",
57 time, timeUnit);
58 throw new RejectedExecutionException(message);
59 }
60 }
61 catch (InterruptedException ie)
62 {
63 Thread.currentThread().interrupt();
64 throw new RejectedExecutionException(ie);
65 }
66 }
67
68 }