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