View Javadoc

1   /*
2    * $Id: ConnectNotifier.java 21946 2011-05-18 15:26:14Z tcarlson $
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.retry.notifiers;
12  
13  import org.mule.api.retry.RetryContext;
14  import org.mule.api.retry.RetryNotifier;
15  import org.mule.config.ExceptionHelper;
16  import org.mule.context.notification.ConnectionNotification;
17  
18  import org.apache.commons.logging.Log;
19  import org.apache.commons.logging.LogFactory;
20  
21  /**
22   * Fires a {@link ConnectionNotification} each time a retry attempt is made.
23   */
24  public class ConnectNotifier implements RetryNotifier
25  {
26      protected transient final Log logger = LogFactory.getLog(ConnectNotifier.class);
27  
28      public void onSuccess(RetryContext context)
29      {
30          if (logger.isDebugEnabled())
31          {
32              logger.debug("Successfully connected to " + context.getDescription());
33          }
34  
35          fireConnectNotification(ConnectionNotification.CONNECTION_CONNECTED, context.getDescription(), context);
36      }
37  
38      public void onFailure(RetryContext context, Throwable e)
39      {
40          fireConnectNotification(ConnectionNotification.CONNECTION_FAILED, context.getDescription(), context);
41  
42          if (logger.isErrorEnabled())
43          {
44              StringBuffer msg = new StringBuffer(512);
45              msg.append("Failed to connect/reconnect: ").append(context.getDescription());
46              Throwable t = ExceptionHelper.getRootException(e);
47              msg.append(". Root Exception was: ").append(ExceptionHelper.writeException(t));
48              if (logger.isTraceEnabled())
49              {
50                  t.printStackTrace();
51              }
52              logger.error(msg.toString());
53          }
54      }
55  
56      protected void fireConnectNotification(int action, String description, RetryContext context)
57      {
58          context.getMuleContext().fireNotification(new ConnectionNotification(null, description, action));
59      }
60  }