[jboss-cvs] JBoss Messaging SVN: r1844 - trunk/src/main/org/jboss/jms/tx

jboss-cvs-commits at lists.jboss.org jboss-cvs-commits at lists.jboss.org
Thu Dec 21 18:43:17 EST 2006


Author: timfox
Date: 2006-12-21 18:43:16 -0500 (Thu, 21 Dec 2006)
New Revision: 1844

Added:
   trunk/src/main/org/jboss/jms/tx/ResourceManagerFactory.java
Log:
Added missing file



Added: trunk/src/main/org/jboss/jms/tx/ResourceManagerFactory.java
===================================================================
--- trunk/src/main/org/jboss/jms/tx/ResourceManagerFactory.java	2006-12-21 23:41:19 UTC (rev 1843)
+++ trunk/src/main/org/jboss/jms/tx/ResourceManagerFactory.java	2006-12-21 23:43:16 UTC (rev 1844)
@@ -0,0 +1,161 @@
+/*
+  * JBoss, Home of Professional Open Source
+  * Copyright 2005, JBoss Inc., and individual contributors as indicated
+  * by the @authors tag. See the copyright.txt in the distribution for a
+  * full listing of individual contributors.
+  *
+  * This is free software; you can redistribute it and/or modify it
+  * under the terms of the GNU Lesser General Public License as
+  * published by the Free Software Foundation; either version 2.1 of
+  * the License, or (at your option) any later version.
+  *
+  * This software is distributed in the hope that it will be useful,
+  * but WITHOUT ANY WARRANTY; without even the implied warranty of
+  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+  * Lesser General Public License for more details.
+  *
+  * You should have received a copy of the GNU Lesser General Public
+  * License along with this software; if not, write to the Free
+  * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
+  * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
+  */
+package org.jboss.jms.tx;
+
+import java.util.HashMap;
+import java.util.Map;
+
+/**
+ * This class manages instances of ResourceManager. It ensures there is one instance per instance
+ * of JMS server as specified by the server id.
+ * 
+ * This allows different JMS connections to the same JMS server (the underlying resource is the JMS server)
+ * to use the same resource manager.
+ * 
+ * This means isSameRM() on XAResource returns true, allowing the Transaction manager to join work in one
+ * tx to another thus allowing 1PC optimization which should help performance.
+ *
+ * @author <a href="mailto:tim.fox at jboss.com">Tim Fox</a>
+ * @version $Revision: 1329 $
+ *
+ * $Id: ResourceManagerFactory.java 1329 2006-09-20 21:29:56Z ovidiu.feodorov at jboss.com $
+ */
+public class ResourceManagerFactory
+{      
+   public static ResourceManagerFactory instance = new ResourceManagerFactory();
+   
+   private Map holders;
+   
+   private ResourceManagerFactory()
+   {      
+      holders = new HashMap();
+   }
+   
+   /*
+    * Need to failover rm from old server id to new server id, merging resource managers if it already exists
+    */
+   public synchronized void handleFailover(int oldServerId, int newServerId)
+   {       
+      Holder hOld = (Holder)holders.remove(new Integer(oldServerId));
+      
+      if (hOld == null)
+      {
+         //This is ok - this would happen if there are more than one connections for the old server failing
+         //in which only the first one to fail would failover the resource manager factory - since they
+         //share the same rm
+         return;
+      }
+      
+      ResourceManager oldRM = hOld.rm;
+      
+      ResourceManager newRM = null;
+      
+      Holder hNew = (Holder)holders.get(new Integer(newServerId));      
+      
+      if (hNew != null)
+      {
+         //Need to merge into the new
+         
+         newRM = hNew.rm;
+         
+         newRM.merge(oldRM);         
+      }
+      else
+      {
+         //re-register the old rm with the new id
+         
+         Holder h = new Holder(oldRM);
+         
+         holders.put(new Integer(newServerId), h);
+      }  
+   }
+   
+   public synchronized int size()
+   {
+      return holders.size();
+   }
+      
+   public synchronized boolean containsResourceManager(int serverID)
+   {
+      return holders.containsKey(new Integer(serverID));
+   }
+   
+   /**
+    * @param serverID - server peer ID.
+    */
+   public synchronized ResourceManager checkOutResourceManager(int serverID)
+   {
+      Integer i = new Integer(serverID);
+      
+      Holder h = (Holder)holders.get(i);
+      
+      if (h == null)
+      {
+         h = new Holder();
+         
+         holders.put(i, h);
+      }
+      else
+      {
+         h.refCount++;
+      }
+      
+      return h.rm;
+   }
+   
+   public synchronized void checkInResourceManager(int serverID)
+   {
+      Integer i = new Integer(serverID);
+      
+      Holder h = (Holder)holders.get(i);
+      
+      if (h == null)
+      {
+         throw new IllegalArgumentException("Cannot find resource manager for server: " + serverID);
+      }
+      
+      h.refCount--;
+      
+      if (h.refCount == 0)
+      {
+         holders.remove(i);
+      }      
+   }
+   
+   private class Holder
+   {
+      ResourceManager rm;
+      
+      Holder()
+      {
+         rm = new ResourceManager();
+      }
+      
+      Holder(ResourceManager rm)
+      {
+         this.rm = rm;
+      }
+      
+      int refCount = 1;
+   }
+  
+}




More information about the jboss-cvs-commits mailing list