Package org.jboss.jms.server.recovery

Source Code of org.jboss.jms.server.recovery.MessagingXAResourceWrapper2

/*
* JBoss, Home of Professional Open Source
* Copyright 2006, 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.server.recovery;

import javax.jms.ExceptionListener;
import javax.jms.JMSException;
import javax.jms.Session;
import javax.transaction.xa.XAException;
import javax.transaction.xa.XAResource;
import javax.transaction.xa.Xid;

import org.jboss.jms.client.delegate.ClientConnectionDelegate;
import org.jboss.jms.client.delegate.ClientSessionDelegate;
import org.jboss.logging.Logger;

/**
* XAResourceWrapper.
*
* Mainly from org.jboss.server.XAResourceWrapper from the JBoss AS server module
*
* The reason why we don't use that class directly is that it assumes on failure of connection
* the RM_FAIL or RM_ERR is thrown, but in JBM we throw XA_RETRY since we want the recovery manager to be able
* to retry on failure without having to manually retry
*
* @author <a href="adrian@jboss.com">Adrian Brock</a>
*
* @author <a href="tim.fox@jboss.com">Tim Fox/a>
*
* @version $Revision: 45341 $
*/
public class MessagingXAResourceWrapper2 implements XAResource, ExceptionListener
{
   /** The log */
   private static final Logger log = Logger.getLogger(MessagingXAResourceWrapper2.class);
  
   /** The connection */
   private ClientConnectionDelegate connection;
  
   /** The delegate XAResource */
   private XAResource delegate;
  
   private int node;
  
   public MessagingXAResourceWrapper2(ClientConnectionDelegate connectionDelegate, int node) throws JMSException
   {
      try
      {
         connection = connectionDelegate;
         ClientSessionDelegate session = (ClientSessionDelegate)connection.createSessionDelegate(true, Session.SESSION_TRANSACTED, true);
         delegate = session.getXAResource();
        
         this.node = node;
         connection.setExceptionListener(this);
      }
      catch(JMSException e)
      {
         log.error(this + " got exception creating XAResource", e);
         this.close();
         throw e;
      }
   }
  
   public Xid[] recover(int flag) throws XAException
   {
      try
      {
         log.debug("Invoking recover(" + flag + ") on the underlying XAResource.");
         return delegate.recover(flag);
      }
      catch (XAException e)
      {
         throw check(e);
      }
   }

   public void commit(Xid xid, boolean onePhase) throws XAException
   {
      log.debug("Commit " + node + " xid " + " onePhase=" + onePhase);
      try
      {
         delegate.commit(xid, onePhase);
      }
      catch (XAException e)
      {
         throw check(e);
      }
   }

   public void rollback(Xid xid) throws XAException
   {
      log.debug("Rollback " + node + " xid " + xid);
      try
      {
         delegate.rollback(xid);
      }
      catch (XAException e)
      {
         throw check(e);
      }
   }

   public void forget(Xid xid) throws XAException
   {
      log.debug("Forget " + node + " xid " + xid);
      try
      {
         delegate.forget(xid);
      }
      catch (XAException e)
      {
         throw check(e);
      }
   }

   public boolean isSameRM(XAResource xaRes) throws XAException
   {
      if (xaRes instanceof MessagingXAResourceWrapper)
         xaRes = ((MessagingXAResourceWrapper) xaRes).getDelegate();

      try
      {
         return delegate.isSameRM(xaRes);
      }
      catch (XAException e)
      {
         throw check(e);
      }
   }

   public int prepare(Xid xid) throws XAException
   {
      try
      {
         return delegate.prepare(xid);
      }
      catch (XAException e)
      {
         throw check(e);
      }
   }

   public void start(Xid xid, int flags) throws XAException
   {
      try
      {
         delegate.start(xid, flags);
      }
      catch (XAException e)
      {
         throw check(e);
      }
   }

   public void end(Xid xid, int flags) throws XAException
   {
      try
      {
         delegate.end(xid, flags);
      }
      catch (XAException e)
      {
         throw check(e);
      }
   }

   public int getTransactionTimeout() throws XAException
   {
      try
      {
         return delegate.getTransactionTimeout();
      }
      catch (XAException e)
      {
         throw check(e);
      }
   }

   public boolean setTransactionTimeout(int seconds) throws XAException
   {
      try
      {
         return delegate.setTransactionTimeout(seconds);
      }
      catch (XAException e)
      {
         throw check(e);
      }
   }

   /**
    * Close the connection
    */
   public void close()
   {
      try
      {
         if (connection != null)
         {
            connection.closing(-1);
            connection.close();
         }
      }
      catch (Exception ignored)
      {
         log.trace("Ignored error during close", ignored);
      }
      finally
      {
         connection = null;
      }
   }

   /**
    * Check whether an XAException is fatal. If it is an RM problem
    * we close the connection so the next call will reconnect.
    *
    * @param e the xa exception
    * @return never
    * @throws XAException always
    */
   protected XAException check(XAException e) throws XAException
   {
      if (e.errorCode == XAException.XA_RETRY)
      {
         log.debug("Fatal error in provider " + node, e);
         close();
      }
      log.debug("Caught XAException. Error code: " + e.errorCode + ". Will rethrow as XAER_RMFAIL.", e);
      throw new XAException(XAException.XAER_RMFAIL);
   }

   protected void finalize() throws Throwable
   {
      close();
   }

   public void onException(JMSException arg0)
   {
      close();
   }
}
TOP

Related Classes of org.jboss.jms.server.recovery.MessagingXAResourceWrapper2

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.