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 | 0 | public abstract class AbstractPolicyTemplate implements RetryPolicyTemplate, MuleContextAware |
37 | |
{ |
38 | 0 | protected RetryNotifier notifier = new ConnectNotifier(); |
39 | |
|
40 | |
|
41 | |
private Map<Object, Object> metaInfo; |
42 | |
|
43 | |
private MuleContext muleContext; |
44 | |
|
45 | 0 | protected transient final Log logger = LogFactory.getLog(getClass()); |
46 | |
|
47 | |
public void setMuleContext(MuleContext context) |
48 | |
{ |
49 | 0 | this.muleContext = context; |
50 | 0 | } |
51 | |
|
52 | |
public RetryContext execute(RetryCallback callback, WorkManager workManager) throws Exception |
53 | |
{ |
54 | 0 | PolicyStatus status = null; |
55 | 0 | RetryPolicy policy = createRetryInstance(); |
56 | 0 | DefaultRetryContext context = new DefaultRetryContext(callback.getWorkDescription(), |
57 | |
metaInfo); |
58 | 0 | context.setMuleContext(muleContext); |
59 | |
|
60 | |
try |
61 | |
{ |
62 | 0 | Exception cause = null; |
63 | |
do |
64 | |
{ |
65 | |
try |
66 | |
{ |
67 | 0 | callback.doWork(context); |
68 | 0 | if (notifier != null) |
69 | |
{ |
70 | 0 | notifier.onSuccess(context); |
71 | |
} |
72 | 0 | break; |
73 | |
} |
74 | 0 | catch (Exception e) |
75 | |
{ |
76 | 0 | cause = e; |
77 | 0 | if (logger.isDebugEnabled()) |
78 | |
{ |
79 | 0 | logger.debug(cause); |
80 | |
} |
81 | 0 | if (notifier != null) |
82 | |
{ |
83 | 0 | notifier.onFailure(context, cause); |
84 | |
} |
85 | 0 | if (cause instanceof InterruptedException || cause instanceof InterruptedIOException) |
86 | |
{ |
87 | 0 | logger.error("Process was interrupted (InterruptedException), ceasing process"); |
88 | 0 | break; |
89 | |
} |
90 | |
else |
91 | |
{ |
92 | 0 | status = policy.applyPolicy(cause); |
93 | |
} |
94 | |
} |
95 | |
} |
96 | 0 | while (status.isOk()); |
97 | |
|
98 | 0 | if (status == null || status.isOk()) |
99 | |
{ |
100 | 0 | return context; |
101 | |
} |
102 | |
else |
103 | |
{ |
104 | 0 | context.setFailed(cause); |
105 | 0 | throw new RetryPolicyExhaustedException(cause, callback.getWorkDescription()); |
106 | |
} |
107 | |
} |
108 | |
finally |
109 | |
{ |
110 | 0 | if (status != null && status.getThrowable() != null) |
111 | |
{ |
112 | 0 | if (logger.isDebugEnabled()) |
113 | |
{ |
114 | 0 | logger.debug(status.getThrowable()); |
115 | |
} |
116 | |
} |
117 | |
} |
118 | |
} |
119 | |
|
120 | |
public RetryNotifier getNotifier() |
121 | |
{ |
122 | 0 | return notifier; |
123 | |
} |
124 | |
|
125 | |
public void setNotifier(RetryNotifier retryNotifier) |
126 | |
{ |
127 | 0 | this.notifier = retryNotifier; |
128 | 0 | } |
129 | |
|
130 | |
public Map<Object, Object> getMetaInfo() |
131 | |
{ |
132 | 0 | return metaInfo; |
133 | |
} |
134 | |
|
135 | |
public void setMetaInfo(Map<Object, Object> metaInfo) |
136 | |
{ |
137 | 0 | this.metaInfo = metaInfo; |
138 | 0 | } |
139 | |
|
140 | |
|
141 | |
public void setId(String id) |
142 | |
{ |
143 | |
|
144 | 0 | } |
145 | |
} |