1
2
3
4
5
6
7
8
9
10
11 package org.mule.exception;
12
13 import org.mule.RequestContext;
14 import org.mule.api.ExceptionPayload;
15 import org.mule.api.MuleContext;
16 import org.mule.api.exception.RollbackSourceCallback;
17 import org.mule.api.exception.SystemExceptionHandler;
18 import org.mule.message.DefaultExceptionPayload;
19 import org.mule.transport.AbstractConnector;
20 import org.mule.transport.ConnectException;
21
22
23
24
25
26 public class AbstractSystemExceptionStrategy extends AbstractExceptionStrategy implements SystemExceptionHandler
27 {
28 public AbstractSystemExceptionStrategy(MuleContext muleContext)
29 {
30 super(muleContext);
31 }
32
33 public void handleException(Exception ex, RollbackSourceCallback rollbackMethod)
34 {
35 fireNotification(ex);
36
37 logException(ex);
38
39 if (isRollback(ex))
40 {
41 logger.debug("Rolling back transaction");
42 rollback(rollbackMethod);
43 }
44 else
45 {
46 logger.debug("Committing transaction");
47 commit();
48 }
49
50 ExceptionPayload exceptionPayload = new DefaultExceptionPayload(ex);
51 if (RequestContext.getEvent() != null)
52 {
53 RequestContext.setExceptionPayload(exceptionPayload);
54 }
55
56 if (ex instanceof ConnectException)
57 {
58 handleReconnection((ConnectException) ex);
59 }
60 }
61
62 public void handleException(Exception ex)
63 {
64 handleException(ex, null);
65 }
66
67 protected void handleReconnection(ConnectException ex)
68 {
69 AbstractConnector connector = (AbstractConnector) ex.getFailed();
70
71
72 if (connector.isConnecting())
73 {
74 return;
75 }
76
77 logger.info("Exception caught is a ConnectException, attempting to reconnect...");
78
79
80 try
81 {
82 logger.debug("Disconnecting " + connector.getName());
83 connector.stop();
84 connector.disconnect();
85 }
86 catch (Exception e1)
87 {
88 logger.error(e1.getMessage());
89 }
90
91
92 try
93 {
94 logger.debug("Reconnecting " + connector.getName());
95 connector.connect();
96 connector.start();
97 }
98 catch (Exception e2)
99 {
100 logger.error(e2.getMessage());
101 }
102 }
103 }
104
105