[hornetq-commits] JBoss hornetq SVN: r12284 - trunk/hornetq-jms/src/main/java/org/hornetq/jms/server/recovery.

do-not-reply at jboss.org do-not-reply at jboss.org
Fri Mar 9 17:39:48 EST 2012


Author: clebert.suconic
Date: 2012-03-09 17:39:47 -0500 (Fri, 09 Mar 2012)
New Revision: 12284

Modified:
   trunk/hornetq-jms/src/main/java/org/hornetq/jms/server/recovery/HornetQResourceRecovery.java
   trunk/hornetq-jms/src/main/java/org/hornetq/jms/server/recovery/HornetQXAResourceWrapper.java
   trunk/hornetq-jms/src/main/java/org/hornetq/jms/server/recovery/RecoveryRegistry.java
   trunk/hornetq-jms/src/main/java/org/hornetq/jms/server/recovery/XARecoveryConfig.java
Log:
JBPAPP-8366 & JBPAPP-8377 - fixing leaks and duplicated resources 

Modified: trunk/hornetq-jms/src/main/java/org/hornetq/jms/server/recovery/HornetQResourceRecovery.java
===================================================================
--- trunk/hornetq-jms/src/main/java/org/hornetq/jms/server/recovery/HornetQResourceRecovery.java	2012-03-09 22:39:17 UTC (rev 12283)
+++ trunk/hornetq-jms/src/main/java/org/hornetq/jms/server/recovery/HornetQResourceRecovery.java	2012-03-09 22:39:47 UTC (rev 12284)
@@ -33,25 +33,51 @@
 {
    private final XARecoveryConfig config;
 
+   private final XAResource[] xaResources;
+
+   private int usage;
+
    public HornetQResourceRecovery(XARecoveryConfig config)
    {
       this.config = config;
+      this.xaResources = new XAResource[] { new HornetQXAResourceWrapper(config) };
    }
 
    public XAResource[] getXAResources()
    {
-      return new XAResource[]{new HornetQXAResourceWrapper(config)};
+      return xaResources;
    }
 
+   public XARecoveryConfig getConfig()
+   {
+      return config;
+   }
+
+   /** we may have several connection factories referencing the same connection recovery entry.
+    *  Because of that we need to make a count of the number of the instances that are referencing it,
+    *  so we will remove it as soon as we are done */
+   public synchronized int incrementUsage()
+   {
+      return ++usage;
+   }
+
+   public synchronized int decrementUsage()
+   {
+      return --usage;
+   }
+
    @Override
    public boolean equals(Object o)
    {
-      if (this == o) return true;
-      if (o == null || getClass() != o.getClass()) return false;
+      if (this == o)
+         return true;
+      if (o == null || getClass() != o.getClass())
+         return false;
 
-      HornetQResourceRecovery that = (HornetQResourceRecovery) o;
+      HornetQResourceRecovery that = (HornetQResourceRecovery)o;
 
-      if (config != null ? !config.equals(that.config) : that.config != null) return false;
+      if (config != null ? !config.equals(that.config) : that.config != null)
+         return false;
 
       return true;
    }

Modified: trunk/hornetq-jms/src/main/java/org/hornetq/jms/server/recovery/HornetQXAResourceWrapper.java
===================================================================
--- trunk/hornetq-jms/src/main/java/org/hornetq/jms/server/recovery/HornetQXAResourceWrapper.java	2012-03-09 22:39:17 UTC (rev 12283)
+++ trunk/hornetq-jms/src/main/java/org/hornetq/jms/server/recovery/HornetQXAResourceWrapper.java	2012-03-09 22:39:47 UTC (rev 12284)
@@ -22,6 +22,7 @@
 import org.hornetq.api.core.HornetQException;
 import org.hornetq.api.core.client.ClientSession;
 import org.hornetq.api.core.client.ClientSessionFactory;
+import org.hornetq.api.core.client.HornetQClient;
 import org.hornetq.api.core.client.ServerLocator;
 import org.hornetq.api.core.client.SessionFailureListener;
 import org.hornetq.core.logging.Logger;
@@ -56,7 +57,7 @@
 
    private XAResource delegate;
 
-   private final XARecoveryConfig[] xaRecoveryConfigs;
+   private XARecoveryConfig[] xaRecoveryConfigs;
 
    // private TransportConfiguration currentConnection;
 
@@ -242,7 +243,7 @@
     * @return the connectionFactory
     * @throws XAException for any problem
     */
-   public XAResource getDelegate(boolean retry) throws XAException
+   private XAResource getDelegate(boolean retry) throws XAException
    {
       XAResource result = null;
       Exception error = null;
@@ -316,7 +317,14 @@
 
          try
          {
-            serverLocator = xaRecoveryConfig.getHornetQConnectionFactory().getServerLocator();
+            if (xaRecoveryConfig.getDiscoveryConfiguration() != null)
+            {
+               serverLocator = HornetQClient.createServerLocator(xaRecoveryConfig.isHA(), xaRecoveryConfig.getDiscoveryConfiguration());
+            }
+            else
+            {
+               serverLocator = HornetQClient.createServerLocator(xaRecoveryConfig.isHA(), xaRecoveryConfig.getTransportConfig());
+            }
             serverLocator.disableFinalizeCheck();
             csf = serverLocator.createSessionFactory();
             if (xaRecoveryConfig.getUsername() == null)
@@ -334,10 +342,29 @@
                                       1);
             }
          }
-         catch (HornetQException e)
+         catch (Throwable e)
          {
+            log.warn("Can't connect to " + xaRecoveryConfig + " on auto-generated resource recovery", e);
+            if (log.isDebugEnabled())
+            {
+               log.debug(e.getMessage(), e);
+            }
+            
+            try
+            {
+               if (cs != null) cs.close();
+               if (serverLocator != null) serverLocator.close();
+            }
+            catch (Throwable ignored)
+            {
+               if (log.isTraceEnabled())
+               {
+                  log.trace(e.getMessage(), ignored);
+               }
+            }
             continue;
          }
+         
          cs.addFailureListener(this);
 
          synchronized (HornetQXAResourceWrapper.lock)
@@ -392,9 +419,9 @@
             oldServerLocator.close();
          }
       }
-      catch (Exception ignored)
+      catch (Throwable ignored)
       {
-         HornetQXAResourceWrapper.log.trace("Ignored error during close", ignored);
+         HornetQXAResourceWrapper.log.debug("Ignored error during close", ignored);
       }
    }
 
@@ -410,10 +437,9 @@
    {
       log.warn(e.getMessage(), e);
 
-      if (e.errorCode == XAException.XA_RETRY)
-      {
-         close();
-      }
+
+      // If any exception happened, we close the connection so we may start fresh
+      close();
       throw e;
    }
 

Modified: trunk/hornetq-jms/src/main/java/org/hornetq/jms/server/recovery/RecoveryRegistry.java
===================================================================
--- trunk/hornetq-jms/src/main/java/org/hornetq/jms/server/recovery/RecoveryRegistry.java	2012-03-09 22:39:17 UTC (rev 12283)
+++ trunk/hornetq-jms/src/main/java/org/hornetq/jms/server/recovery/RecoveryRegistry.java	2012-03-09 22:39:47 UTC (rev 12284)
@@ -28,7 +28,7 @@
  */
 public interface RecoveryRegistry
 {
-   void register(HornetQResourceRecovery resourceRecovery);
+   HornetQResourceRecovery register(HornetQResourceRecovery resourceRecovery);
 
    void unRegister(HornetQResourceRecovery xaRecoveryConfig);
 }

Modified: trunk/hornetq-jms/src/main/java/org/hornetq/jms/server/recovery/XARecoveryConfig.java
===================================================================
--- trunk/hornetq-jms/src/main/java/org/hornetq/jms/server/recovery/XARecoveryConfig.java	2012-03-09 22:39:17 UTC (rev 12283)
+++ trunk/hornetq-jms/src/main/java/org/hornetq/jms/server/recovery/XARecoveryConfig.java	2012-03-09 22:39:47 UTC (rev 12284)
@@ -13,10 +13,17 @@
 
 package org.hornetq.jms.server.recovery;
 
-import org.hornetq.jms.client.HornetQConnectionFactory;
+import java.util.Arrays;
 
+import org.hornetq.api.core.DiscoveryGroupConfiguration;
+import org.hornetq.api.core.TransportConfiguration;
+
 /**
+ * 
+ * This represents the configuration of a single connection factory.
+ * 
  * @author <a href="mailto:andy.taylor at jboss.com">Andy Taylor</a>
+ * @author Clebert Suconic
  *
  * A wrapper around info needed for the xa recovery resource
  *         Date: 3/23/11
@@ -24,22 +31,46 @@
  */
 public class XARecoveryConfig
 {
-   private final HornetQConnectionFactory hornetQConnectionFactory;
+   
+   private final boolean ha;
+   private final TransportConfiguration[] transportConfiguration;
+   private final DiscoveryGroupConfiguration discoveryConfiguration;
    private final String username;
    private final String password;
 
-   public XARecoveryConfig(HornetQConnectionFactory hornetQConnectionFactory, String username, String password)
+   public XARecoveryConfig(final boolean ha, final TransportConfiguration[] transportConfiguration, final String username, final String password)
    {
-      this.hornetQConnectionFactory = hornetQConnectionFactory;
+      this.transportConfiguration = transportConfiguration;
+      this.discoveryConfiguration = null;
       this.username = username;
       this.password = password;
+      this.ha = ha;
    }
 
-   public HornetQConnectionFactory getHornetQConnectionFactory()
+   public XARecoveryConfig(final boolean ha, final DiscoveryGroupConfiguration discoveryConfiguration, final String username, final String password)
    {
-      return hornetQConnectionFactory;
+      this.discoveryConfiguration = discoveryConfiguration;
+      this.transportConfiguration = null;
+      this.username = username;
+      this.password = password;
+      this.ha = ha;
    }
+   
+   public boolean isHA()
+   {
+      return ha;
+   }
 
+   public DiscoveryGroupConfiguration getDiscoveryConfiguration()
+   {
+      return discoveryConfiguration;
+   }
+   
+   public TransportConfiguration[] getTransportConfig()
+   {
+      return transportConfiguration;
+   }
+
    public String getUsername()
    {
       return username;
@@ -49,24 +80,44 @@
    {
       return password;
    }
+   
+   /* (non-Javadoc)
+    * @see java.lang.Object#hashCode()
+    */
+   @Override
+   public int hashCode()
+   {
+      final int prime = 31;
+      int result = 1;
+      result = prime * result + ((discoveryConfiguration == null) ? 0 : discoveryConfiguration.hashCode());
+      result = prime * result + Arrays.hashCode(transportConfiguration);
+      return result;
+   }
 
+   /* (non-Javadoc)
+    * @see java.lang.Object#equals(java.lang.Object)
+    */
    @Override
-   public boolean equals(Object o)
+   public boolean equals(Object obj)
    {
-      if (this == o) return true;
-      if (o == null || getClass() != o.getClass()) return false;
-
-      XARecoveryConfig that = (XARecoveryConfig) o;
-
-      if (hornetQConnectionFactory != null ? !hornetQConnectionFactory.equals(that.hornetQConnectionFactory) : that.hornetQConnectionFactory != null)
+      if (this == obj)
+         return true;
+      if (obj == null)
          return false;
-      if (password != null ? !password.equals(that.password) : that.password != null) return false;
-      if (username != null ? !username.equals(that.username) : that.username != null) return false;
-
+      if (getClass() != obj.getClass())
+         return false;
+      XARecoveryConfig other = (XARecoveryConfig)obj;
+      if (discoveryConfiguration == null)
+      {
+         if (other.discoveryConfiguration != null)
+            return false;
+      }
+      else if (!discoveryConfiguration.equals(other.discoveryConfiguration))
+         return false;
+      if (!Arrays.equals(transportConfiguration, other.transportConfiguration))
+         return false;
       return true;
    }
-   
-   
 
    /* (non-Javadoc)
     * @see java.lang.Object#toString()
@@ -74,20 +125,12 @@
    @Override
    public String toString()
    {
-      return "XARecoveryConfig [hornetQConnectionFactory=" + hornetQConnectionFactory +
+      return "XARecoveryConfig [transportConfiguration = " + Arrays.toString(transportConfiguration) +
+             ", discoveryConfiguration = " + discoveryConfiguration +
              ", username=" +
              username +
              ", password=" +
              password +
              "]";
    }
-
-   @Override
-   public int hashCode()
-   {
-      int result = hornetQConnectionFactory != null ? hornetQConnectionFactory.hashCode() : 0;
-      result = 31 * result + (username != null ? username.hashCode() : 0);
-      result = 31 * result + (password != null ? password.hashCode() : 0);
-      return result;
-   }
 }



More information about the hornetq-commits mailing list