1 | |
|
2 | |
|
3 | |
|
4 | |
|
5 | |
|
6 | |
|
7 | |
package org.mule.exception; |
8 | |
|
9 | |
import org.mule.RequestContext; |
10 | |
import org.mule.api.ExceptionPayload; |
11 | |
import org.mule.api.MuleEvent; |
12 | |
import org.mule.api.exception.MessagingExceptionHandler; |
13 | |
import org.mule.api.processor.MessageProcessor; |
14 | |
import org.mule.api.routing.RoutingException; |
15 | |
import org.mule.api.transaction.Transaction; |
16 | |
import org.mule.api.transaction.TransactionException; |
17 | |
import org.mule.context.notification.ExceptionNotification; |
18 | |
import org.mule.management.stats.FlowConstructStatistics; |
19 | |
import org.mule.message.DefaultExceptionPayload; |
20 | |
import org.mule.transaction.TransactionCoordination; |
21 | |
import org.mule.transport.NullPayload; |
22 | |
|
23 | |
|
24 | |
|
25 | |
|
26 | |
|
27 | |
|
28 | 0 | public abstract class AbstractMessagingExceptionStrategy extends AbstractExceptionListener implements MessagingExceptionHandler |
29 | |
{ |
30 | |
|
31 | |
|
32 | |
|
33 | |
public MuleEvent handleException(Exception e, MuleEvent event) |
34 | |
{ |
35 | 0 | if (enableNotifications) |
36 | |
{ |
37 | 0 | fireNotification(new ExceptionNotification(e)); |
38 | |
} |
39 | |
|
40 | 0 | logException(e); |
41 | 0 | doHandleException(e, event); |
42 | 0 | closeStream(event.getMessage()); |
43 | |
|
44 | 0 | event.getMessage().setPayload(NullPayload.getInstance()); |
45 | 0 | ExceptionPayload exceptionPayload = new DefaultExceptionPayload(e); |
46 | 0 | event.getMessage().setExceptionPayload(exceptionPayload); |
47 | 0 | if (RequestContext.getEvent() != null) |
48 | |
{ |
49 | 0 | RequestContext.setExceptionPayload(exceptionPayload); |
50 | |
} |
51 | 0 | return event; |
52 | |
} |
53 | |
|
54 | |
protected void doHandleException(Exception ex, MuleEvent event) |
55 | |
{ |
56 | |
|
57 | 0 | defaultHandler(ex); |
58 | |
|
59 | 0 | MessageProcessor target = null; |
60 | 0 | if (ex instanceof RoutingException) |
61 | |
{ |
62 | 0 | target = ((RoutingException) ex).getRoute(); |
63 | |
} |
64 | 0 | if (isRollback(ex)) |
65 | |
{ |
66 | 0 | rollback(); |
67 | |
|
68 | 0 | logger.debug("Routing exception message"); |
69 | 0 | routeException(event, target, ex); |
70 | |
} |
71 | |
else |
72 | |
{ |
73 | 0 | logger.debug("Routing exception message"); |
74 | 0 | routeException(event, target, ex); |
75 | |
|
76 | 0 | logger.debug("Committing transaction"); |
77 | 0 | commit(); |
78 | |
} |
79 | 0 | } |
80 | |
|
81 | |
protected void commit() |
82 | |
{ |
83 | 0 | Transaction tx = TransactionCoordination.getInstance().getTransaction(); |
84 | 0 | if (tx != null) |
85 | |
{ |
86 | |
try |
87 | |
{ |
88 | 0 | tx.commit(); |
89 | |
} |
90 | 0 | catch (TransactionException e) |
91 | |
{ |
92 | 0 | logger.error(e); |
93 | 0 | } |
94 | |
} |
95 | 0 | } |
96 | |
|
97 | |
protected void rollback() |
98 | |
{ |
99 | 0 | Transaction tx = TransactionCoordination.getInstance().getTransaction(); |
100 | 0 | if (tx != null) |
101 | |
{ |
102 | |
try |
103 | |
{ |
104 | 0 | tx.rollback(); |
105 | |
} |
106 | 0 | catch (TransactionException e) |
107 | |
{ |
108 | 0 | logger.error(e); |
109 | 0 | } |
110 | |
} |
111 | 0 | } |
112 | |
|
113 | |
|
114 | |
|
115 | |
|
116 | |
|
117 | |
protected void defaultHandler(Throwable t) |
118 | |
{ |
119 | |
|
120 | 0 | } |
121 | |
} |