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