View Javadoc

1   /*
2    * $Id: AbstractSystemExceptionStrategy.java 22772 2011-08-27 15:20:15Z dfeist $
3    * --------------------------------------------------------------------------------------
4    * Copyright (c) MuleSoft, Inc.  All rights reserved.  http://www.mulesoft.com
5    *
6    * The software in this package is published under the terms of the CPAL v1.0
7    * license, a copy of which has been included with this distribution in the
8    * LICENSE.txt file.
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   * Fire a notification, log exception, clean up transaction if any, and trigger reconnection strategy 
24   * if this is a <code>ConnectException</code>.
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          // Make sure the connector is not already being reconnected by another receiver thread.
72          if (connector.isConnecting())
73          {
74              return;
75          }
76              
77          logger.info("Exception caught is a ConnectException, attempting to reconnect...");
78          
79          // Disconnect
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          // Reconnect (retry policy will go into effect here if configured)
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