Package org.hornetq.tests.integration.client

Source Code of org.hornetq.tests.integration.client.HeuristicXATest

/*
* Copyright 2009 Red Hat, Inc.
* Red Hat licenses this file to you under the Apache License, version
* 2.0 (the "License"); you may not use this file except in compliance
* with the License.  You may obtain a copy of the License at
*    http://www.apache.org/licenses/LICENSE-2.0
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
* implied.  See the License for the specific language governing
* permissions and limitations under the License.
*/

package org.hornetq.tests.integration.client;

import java.util.HashMap;

import javax.management.MBeanServer;
import javax.management.MBeanServerFactory;
import javax.transaction.xa.XAResource;
import javax.transaction.xa.Xid;

import junit.framework.Assert;

import org.hornetq.api.core.SimpleString;
import org.hornetq.api.core.client.ClientConsumer;
import org.hornetq.api.core.client.ClientMessage;
import org.hornetq.api.core.client.ClientProducer;
import org.hornetq.api.core.client.ClientSession;
import org.hornetq.api.core.client.ClientSessionFactory;
import org.hornetq.api.core.management.HornetQServerControl;
import org.hornetq.core.config.Configuration;
import org.hornetq.core.logging.Logger;
import org.hornetq.core.server.HornetQServer;
import org.hornetq.core.server.Queue;
import org.hornetq.core.settings.impl.AddressSettings;
import org.hornetq.core.transaction.impl.XidImpl;
import org.hornetq.tests.integration.management.ManagementControlHelper;
import org.hornetq.tests.util.ServiceTestBase;

/**
* A HeuristicXATest
*
* @author <a href="mailto:clebert.suconic@jboss.org">Clebert Suconic</a>
*
*
*/
public class HeuristicXATest extends ServiceTestBase
{
   // Constants -----------------------------------------------------

   private static final Logger log = Logger.getLogger(HeuristicXATest.class);

   final SimpleString ADDRESS = new SimpleString("ADDRESS");

   final String body = "this is the body";

   // Attributes ----------------------------------------------------

   private MBeanServer mbeanServer;

   // Static --------------------------------------------------------

   // Constructors --------------------------------------------------

   // Public --------------------------------------------------------

   public void testInvalidCall() throws Exception
   {
      Configuration configuration = createDefaultConfig();
      configuration.setJMXManagementEnabled(true);

      HornetQServer server = createServer(false, configuration, mbeanServer, new HashMap<String, AddressSettings>());

      try
      {
         server.start();

         HornetQServerControl jmxServer = ManagementControlHelper.createHornetQServerControl(mbeanServer);

         Assert.assertFalse(jmxServer.commitPreparedTransaction("Nananananana"));
      }
      finally
      {
         if (server.isStarted())
         {
            server.stop();
         }
      }

   }

   public void testHeuristicCommit() throws Exception
   {
      internalTest(true);
   }

   public void testHeuristicRollback() throws Exception
   {
      internalTest(false);
   }

   private void internalTest(final boolean isCommit) throws Exception
   {
      Configuration configuration = createDefaultConfig();
      configuration.setJMXManagementEnabled(true);

      HornetQServer server = createServer(false, configuration, mbeanServer, new HashMap<String, AddressSettings>());
      try
      {
         server.start();
         Xid xid = newXID();

         ClientSessionFactory sf = createInVMFactory();

         ClientSession session = sf.createSession(true, false, false);

         session.createQueue(ADDRESS, ADDRESS, true);

         session.start(xid, XAResource.TMNOFLAGS);

         ClientProducer producer = session.createProducer(ADDRESS);

         ClientMessage msg = session.createMessage(true);

         msg.getBodyBuffer().writeString(body);

         producer.send(msg);

         session.end(xid, XAResource.TMSUCCESS);

         session.prepare(xid);

         session.close();

         HornetQServerControl jmxServer = ManagementControlHelper.createHornetQServerControl(mbeanServer);

         String preparedTransactions[] = jmxServer.listPreparedTransactions();

         Assert.assertEquals(1, preparedTransactions.length);

         System.out.println(preparedTransactions[0]);

         Assert.assertEquals(0, jmxServer.listHeuristicCommittedTransactions().length);
         Assert.assertEquals(0, jmxServer.listHeuristicRolledBackTransactions().length);

         if (isCommit)
         {
            jmxServer.commitPreparedTransaction(XidImpl.toBase64String(xid));
         }
         else
         {
            jmxServer.rollbackPreparedTransaction(XidImpl.toBase64String(xid));
         }

         Assert.assertEquals(0, jmxServer.listPreparedTransactions().length);
         if (isCommit)
         {
            Assert.assertEquals(1, jmxServer.listHeuristicCommittedTransactions().length);
            Assert.assertEquals(0, jmxServer.listHeuristicRolledBackTransactions().length);
         }
         else
         {
            Assert.assertEquals(0, jmxServer.listHeuristicCommittedTransactions().length);
            Assert.assertEquals(1, jmxServer.listHeuristicRolledBackTransactions().length);
         }

         if (isCommit)
         {
            Assert.assertEquals(1, ((Queue)server.getPostOffice().getBinding(ADDRESS).getBindable()).getMessageCount());

            session = sf.createSession(false, false, false);

            session.start();
            ClientConsumer consumer = session.createConsumer(ADDRESS);
            msg = consumer.receive(1000);
            Assert.assertNotNull(msg);
            msg.acknowledge();
            Assert.assertEquals(body, msg.getBodyBuffer().readString());

            session.commit();
            session.close();
         }

         Assert.assertEquals(0, ((Queue)server.getPostOffice().getBinding(ADDRESS).getBindable()).getMessageCount());

      }
      finally
      {
         if (server.isStarted())
         {
            server.stop();
         }
      }

   }

   public void testHeuristicCommitWithRestart() throws Exception
   {
      doHeuristicCompletionWithRestart(true);
   }

   public void testHeuristicRollbackWithRestart() throws Exception
   {
      doHeuristicCompletionWithRestart(false);
   }

   private void doHeuristicCompletionWithRestart(final boolean isCommit) throws Exception
   {
      Configuration configuration = createDefaultConfig();
      configuration.setJMXManagementEnabled(true);

      HornetQServer server = createServer(true, configuration, mbeanServer, new HashMap<String, AddressSettings>());
      try
      {
         server.start();
         Xid xid = newXID();

         ClientSessionFactory sf = createInVMFactory();

         ClientSession session = sf.createSession(true, false, false);

         session.createQueue(ADDRESS, ADDRESS, true);

         session.start(xid, XAResource.TMNOFLAGS);

         ClientProducer producer = session.createProducer(ADDRESS);

         ClientMessage msg = session.createMessage(true);

         msg.getBodyBuffer().writeString(body);

         producer.send(msg);

         session.end(xid, XAResource.TMSUCCESS);

         session.prepare(xid);

         session.close();

         HornetQServerControl jmxServer = ManagementControlHelper.createHornetQServerControl(mbeanServer);

         String preparedTransactions[] = jmxServer.listPreparedTransactions();

         Assert.assertEquals(1, preparedTransactions.length);
         System.out.println(preparedTransactions[0]);

         if (isCommit)
         {
            jmxServer.commitPreparedTransaction(XidImpl.toBase64String(xid));
         }
         else
         {
            jmxServer.rollbackPreparedTransaction(XidImpl.toBase64String(xid));
         }

         preparedTransactions = jmxServer.listPreparedTransactions();
         Assert.assertEquals(0, preparedTransactions.length);

         if (isCommit)
         {
            Assert.assertEquals(1, ((Queue)server.getPostOffice().getBinding(ADDRESS).getBindable()).getMessageCount());

            session = sf.createSession(false, false, false);

            session.start();
            ClientConsumer consumer = session.createConsumer(ADDRESS);
            msg = consumer.receive(1000);
            Assert.assertNotNull(msg);
            msg.acknowledge();
            Assert.assertEquals(body, msg.getBodyBuffer().readString());

            session.commit();
            session.close();
         }

         Assert.assertEquals(0, ((Queue)server.getPostOffice().getBinding(ADDRESS).getBindable()).getMessageCount());

         server.stop();

         server.start();

         jmxServer = ManagementControlHelper.createHornetQServerControl(mbeanServer);
         if (isCommit)
         {
            String[] listHeuristicCommittedTransactions = jmxServer.listHeuristicCommittedTransactions();
            Assert.assertEquals(1, listHeuristicCommittedTransactions.length);
            System.out.println(listHeuristicCommittedTransactions[0]);
         }
         else
         {
            String[] listHeuristicRolledBackTransactions = jmxServer.listHeuristicRolledBackTransactions();
            Assert.assertEquals(1, listHeuristicRolledBackTransactions.length);
            System.out.println(listHeuristicRolledBackTransactions[0]);
         }
      }
      finally
      {
         if (server.isStarted())
         {
            server.stop();
         }
      }
   }

   public void testRecoverHeuristicCommitWithRestart() throws Exception
   {
      doRecoverHeuristicCompletedTxWithRestart(true);
   }

   public void testRecoverHeuristicRollbackWithRestart() throws Exception
   {
      doRecoverHeuristicCompletedTxWithRestart(false);
   }

   private void doRecoverHeuristicCompletedTxWithRestart(final boolean heuristicCommit) throws Exception
   {
      Configuration configuration = createDefaultConfig();
      configuration.setJMXManagementEnabled(true);

      HornetQServer server = createServer(true, configuration, mbeanServer, new HashMap<String, AddressSettings>());
      try
      {
         server.start();
         Xid xid = newXID();

         ClientSessionFactory sf = createInVMFactory();

         ClientSession session = sf.createSession(true, false, false);

         session.createQueue(ADDRESS, ADDRESS, true);

         session.start(xid, XAResource.TMNOFLAGS);

         ClientProducer producer = session.createProducer(ADDRESS);

         ClientMessage msg = session.createMessage(true);

         msg.getBodyBuffer().writeString(body);

         producer.send(msg);

         session.end(xid, XAResource.TMSUCCESS);

         session.prepare(xid);

         session.close();

         HornetQServerControl jmxServer = ManagementControlHelper.createHornetQServerControl(mbeanServer);

         String preparedTransactions[] = jmxServer.listPreparedTransactions();

         Assert.assertEquals(1, preparedTransactions.length);
         System.out.println(preparedTransactions[0]);

         if (heuristicCommit)
         {
            jmxServer.commitPreparedTransaction(XidImpl.toBase64String(xid));
         }
         else
         {
            jmxServer.rollbackPreparedTransaction(XidImpl.toBase64String(xid));
         }

         preparedTransactions = jmxServer.listPreparedTransactions();
         Assert.assertEquals(0, preparedTransactions.length);

         if (heuristicCommit)
         {
            Assert.assertEquals(1, ((Queue)server.getPostOffice().getBinding(ADDRESS).getBindable()).getMessageCount());

            session = sf.createSession(false, false, false);

            session.start();
            ClientConsumer consumer = session.createConsumer(ADDRESS);
            msg = consumer.receive(1000);
            Assert.assertNotNull(msg);
            msg.acknowledge();
            Assert.assertEquals(body, msg.getBodyBuffer().readString());

            session.commit();
            session.close();
         }

         Assert.assertEquals(0, ((Queue)server.getPostOffice().getBinding(ADDRESS).getBindable()).getMessageCount());

         server.stop();

         server.start();

         jmxServer = ManagementControlHelper.createHornetQServerControl(mbeanServer);
         if (heuristicCommit)
         {
            String[] listHeuristicCommittedTransactions = jmxServer.listHeuristicCommittedTransactions();
            Assert.assertEquals(1, listHeuristicCommittedTransactions.length);
            System.out.println(listHeuristicCommittedTransactions[0]);
         }
         else
         {
            String[] listHeuristicRolledBackTransactions = jmxServer.listHeuristicRolledBackTransactions();
            Assert.assertEquals(1, listHeuristicRolledBackTransactions.length);
            System.out.println(listHeuristicRolledBackTransactions[0]);
         }

         session = sf.createSession(true, false, false);
         Xid[] recoveredXids = session.recover(XAResource.TMSTARTRSCAN);
         Assert.assertEquals(1, recoveredXids.length);
         Assert.assertEquals(xid, recoveredXids[0]);
         Assert.assertEquals(0, session.recover(XAResource.TMENDRSCAN).length);

         session.close();
      }
      finally
      {
         if (server.isStarted())
         {
            server.stop();
         }
      }
   }

   public void testForgetHeuristicCommitAndRestart() throws Exception
   {
      doForgetHeuristicCompletedTxAndRestart(true);
   }

   public void testForgetHeuristicRollbackAndRestart() throws Exception
   {
      doForgetHeuristicCompletedTxAndRestart(false);
   }

   private void doForgetHeuristicCompletedTxAndRestart(final boolean heuristicCommit) throws Exception
   {
      Configuration configuration = createDefaultConfig();
      configuration.setJMXManagementEnabled(true);

      HornetQServer server = createServer(true, configuration, mbeanServer, new HashMap<String, AddressSettings>());
      try
      {
         server.start();
         Xid xid = newXID();

         ClientSessionFactory sf = createInVMFactory();

         ClientSession session = sf.createSession(true, false, false);

         session.createQueue(ADDRESS, ADDRESS, true);

         session.start(xid, XAResource.TMNOFLAGS);

         ClientProducer producer = session.createProducer(ADDRESS);

         ClientMessage msg = session.createMessage(true);

         msg.getBodyBuffer().writeBytes(new byte[123]);

         producer.send(msg);

         session.end(xid, XAResource.TMSUCCESS);

         session.prepare(xid);

         HornetQServerControl jmxServer = ManagementControlHelper.createHornetQServerControl(mbeanServer);

         String preparedTransactions[] = jmxServer.listPreparedTransactions();

         Assert.assertEquals(1, preparedTransactions.length);
         System.out.println(preparedTransactions[0]);

         if (heuristicCommit)
         {
            jmxServer.commitPreparedTransaction(XidImpl.toBase64String(xid));
         }
         else
         {
            jmxServer.rollbackPreparedTransaction(XidImpl.toBase64String(xid));
         }

         preparedTransactions = jmxServer.listPreparedTransactions();
         Assert.assertEquals(0, preparedTransactions.length);

         session.forget(xid);

         session.close();

         if (heuristicCommit)
         {
            Assert.assertEquals(0, jmxServer.listHeuristicCommittedTransactions().length);
         }
         else
         {
            Assert.assertEquals(0, jmxServer.listHeuristicRolledBackTransactions().length);
         }

         server.stop();

         server.start();

         session = sf.createSession(true, false, false);
         Xid[] recoveredXids = session.recover(XAResource.TMSTARTRSCAN);
         Assert.assertEquals(0, recoveredXids.length);
         jmxServer = ManagementControlHelper.createHornetQServerControl(mbeanServer);
         if (heuristicCommit)
         {
            Assert.assertEquals(0, jmxServer.listHeuristicCommittedTransactions().length);
         }
         else
         {
            Assert.assertEquals(0, jmxServer.listHeuristicRolledBackTransactions().length);
         }

         session.close();
      }
      finally
      {
         if (server.isStarted())
         {
            server.stop();
         }
      }
   }

   // Package protected ---------------------------------------------

   // Protected -----------------------------------------------------

   @Override
   protected void tearDown() throws Exception
   {
      MBeanServerFactory.releaseMBeanServer(mbeanServer);
      super.tearDown();
   }

   @Override
   protected void setUp() throws Exception
   {
      super.setUp();
      mbeanServer = MBeanServerFactory.createMBeanServer();
   }

   // Private -------------------------------------------------------

   // Inner classes -------------------------------------------------

}
TOP

Related Classes of org.hornetq.tests.integration.client.HeuristicXATest

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.