1
2
3
4
5
6
7
8
9
10
11 package org.mule.retry.async;
12
13 import javax.resource.spi.work.Work;
14
15 import org.apache.commons.logging.Log;
16 import org.apache.commons.logging.LogFactory;
17 import org.mule.api.context.WorkManager;
18 import org.mule.api.retry.RetryCallback;
19 import org.mule.api.retry.RetryPolicyTemplate;
20 import org.mule.util.concurrent.Latch;
21
22
23
24
25
26 public class RetryWorker implements Work
27 {
28 protected transient final Log logger = LogFactory.getLog(RetryWorker.class);
29
30 private final RetryCallback callback;
31 private final WorkManager workManager;
32 private Exception exception = null;
33 private final FutureRetryContext context = new FutureRetryContext();
34 private final RetryPolicyTemplate delegate;
35 private Latch startLatch;
36
37 public RetryWorker(RetryPolicyTemplate delegate, RetryCallback callback, WorkManager workManager)
38 {
39 this(delegate, callback, workManager, null);
40 }
41
42 public RetryWorker(RetryPolicyTemplate delegate,
43 RetryCallback callback,
44 WorkManager workManager,
45 Latch startLatch)
46 {
47 this.callback = callback;
48 this.workManager = workManager;
49 this.delegate = delegate;
50 this.startLatch = startLatch;
51 if (this.startLatch == null)
52 {
53 this.startLatch = new Latch();
54 this.startLatch.countDown();
55 }
56 }
57
58 public void release()
59 {
60
61 }
62
63 public void run()
64 {
65 try
66 {
67 startLatch.await();
68 }
69 catch (InterruptedException e)
70 {
71 logger.warn("Retry thread interupted for callback: " + callback.getWorkDescription());
72 return;
73 }
74 try
75 {
76 context.setDelegateContext(delegate.execute(callback, workManager));
77 }
78 catch (Exception e)
79 {
80 this.exception = e;
81 logger.fatal(e, e);
82
83 }
84 }
85
86 public Exception getException()
87 {
88 return exception;
89 }
90
91 public FutureRetryContext getRetryContext()
92 {
93 return context;
94 }
95 }