1
2
3
4
5
6
7 package org.mule.retry.policies;
8
9 import org.mule.api.MuleContext;
10 import org.mule.api.context.MuleContextAware;
11 import org.mule.api.context.WorkManager;
12 import org.mule.api.retry.RetryCallback;
13 import org.mule.api.retry.RetryContext;
14 import org.mule.api.retry.RetryNotifier;
15 import org.mule.api.retry.RetryPolicy;
16 import org.mule.api.retry.RetryPolicyTemplate;
17 import org.mule.retry.DefaultRetryContext;
18 import org.mule.retry.PolicyStatus;
19 import org.mule.retry.RetryPolicyExhaustedException;
20 import org.mule.retry.notifiers.ConnectNotifier;
21
22 import java.io.InterruptedIOException;
23 import java.util.Map;
24
25 import org.apache.commons.logging.Log;
26 import org.apache.commons.logging.LogFactory;
27
28
29
30
31
32 public abstract class AbstractPolicyTemplate implements RetryPolicyTemplate, MuleContextAware
33 {
34 protected RetryNotifier notifier = new ConnectNotifier();
35
36
37 private Map<Object, Object> metaInfo;
38
39 private MuleContext muleContext;
40
41 protected transient final Log logger = LogFactory.getLog(getClass());
42
43 public void setMuleContext(MuleContext context)
44 {
45 this.muleContext = context;
46 }
47
48 public RetryContext execute(RetryCallback callback, WorkManager workManager) throws Exception
49 {
50 PolicyStatus status = null;
51 RetryPolicy policy = createRetryInstance();
52 DefaultRetryContext context = new DefaultRetryContext(callback.getWorkDescription(),
53 metaInfo);
54 context.setMuleContext(muleContext);
55
56 try
57 {
58 Exception cause = null;
59 do
60 {
61 try
62 {
63 callback.doWork(context);
64 if (notifier != null)
65 {
66 notifier.onSuccess(context);
67 }
68 break;
69 }
70 catch (Exception e)
71 {
72 cause = e;
73 if (logger.isDebugEnabled())
74 {
75 logger.debug(cause);
76 }
77 if (notifier != null)
78 {
79 notifier.onFailure(context, cause);
80 }
81 if (cause instanceof InterruptedException || cause instanceof InterruptedIOException)
82 {
83 logger.error("Process was interrupted (InterruptedException), ceasing process");
84 break;
85 }
86 else
87 {
88 status = policy.applyPolicy(cause);
89 }
90 }
91 }
92 while (status.isOk());
93
94 if (status == null || status.isOk())
95 {
96 return context;
97 }
98 else
99 {
100 context.setFailed(cause);
101 throw new RetryPolicyExhaustedException(cause, callback.getWorkDescription());
102 }
103 }
104 finally
105 {
106 if (status != null && status.getThrowable() != null)
107 {
108 if (logger.isDebugEnabled())
109 {
110 logger.debug(status.getThrowable());
111 }
112 }
113 }
114 }
115
116 public RetryNotifier getNotifier()
117 {
118 return notifier;
119 }
120
121 public void setNotifier(RetryNotifier retryNotifier)
122 {
123 this.notifier = retryNotifier;
124 }
125
126 public Map<Object, Object> getMetaInfo()
127 {
128 return metaInfo;
129 }
130
131 public void setMetaInfo(Map<Object, Object> metaInfo)
132 {
133 this.metaInfo = metaInfo;
134 }
135
136
137 public void setId(String id)
138 {
139
140 }
141 }