View Javadoc
1   /*
2    * Copyright (c) MuleSoft, Inc.  All rights reserved.  http://www.mulesoft.com
3    * The software in this package is published under the terms of the CPAL v1.0
4    * license, a copy of which has been included with this distribution in the
5    * LICENSE.txt file.
6    */
7   package org.mule.retry.notifiers;
8   
9   import org.mule.api.retry.RetryContext;
10  import org.mule.api.retry.RetryNotifier;
11  import org.mule.config.ExceptionHelper;
12  import org.mule.context.notification.ConnectionNotification;
13  
14  import org.apache.commons.logging.Log;
15  import org.apache.commons.logging.LogFactory;
16  
17  /**
18   * Fires a {@link ConnectionNotification} each time a retry attempt is made.
19   */
20  public class ConnectNotifier implements RetryNotifier
21  {
22      protected transient final Log logger = LogFactory.getLog(ConnectNotifier.class);
23  
24      public void onSuccess(RetryContext context)
25      {
26          if (logger.isDebugEnabled())
27          {
28              logger.debug("Successfully connected to " + context.getDescription());
29          }
30  
31          fireConnectNotification(ConnectionNotification.CONNECTION_CONNECTED, context.getDescription(), context);
32      }
33  
34      public void onFailure(RetryContext context, Throwable e)
35      {
36          fireConnectNotification(ConnectionNotification.CONNECTION_FAILED, context.getDescription(), context);
37  
38          if (logger.isErrorEnabled())
39          {
40              StringBuffer msg = new StringBuffer(512);
41              msg.append("Failed to connect/reconnect: ").append(context.getDescription());
42              Throwable t = ExceptionHelper.getRootException(e);
43              msg.append(". Root Exception was: ").append(ExceptionHelper.writeException(t));
44              logger.error(msg.toString());
45          }
46      }
47  
48      protected void fireConnectNotification(int action, String description, RetryContext context)
49      {
50          context.getMuleContext().fireNotification(new ConnectionNotification(null, description, action));
51      }
52  }