javax.jms.XASession Java Examples

The following examples show how to use javax.jms.XASession. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: SimpleOpenWireTest.java    From activemq-artemis with Apache License 2.0 6 votes vote down vote up
@Test
public void testXAResourceRolledBackRemoved() throws Exception {
   Queue queue = null;

   Xid xid = newXID();
   try (XAConnection xaconnection = xaFactory.createXAConnection()) {
      XASession session = xaconnection.createXASession();
      queue = session.createQueue(queueName);
      session.getXAResource().start(xid, XAResource.TMNOFLAGS);
      MessageProducer producer = session.createProducer(queue);
      producer.send(session.createTextMessage("xa message"));
      session.getXAResource().end(xid, XAResource.TMSUCCESS);
      session.getXAResource().rollback(xid);
   }
   XidImpl xid1 = new XidImpl(xid);
   Transaction transaction = server.getResourceManager().getTransaction(xid1);
   assertNull(transaction);
}
 
Example #2
Source File: SimpleOpenWireTest.java    From activemq-artemis with Apache License 2.0 6 votes vote down vote up
@Test
public void testXAResourceCommittedRemoved() throws Exception {
   Queue queue = null;

   Xid xid = newXID();
   try (XAConnection xaconnection = xaFactory.createXAConnection()) {
      XASession session = xaconnection.createXASession();
      queue = session.createQueue(queueName);
      session.getXAResource().start(xid, XAResource.TMNOFLAGS);
      MessageProducer producer = session.createProducer(queue);
      producer.send(session.createTextMessage("xa message"));
      session.getXAResource().end(xid, XAResource.TMSUCCESS);
      session.getXAResource().commit(xid, true);
   }
   XidImpl xid1 = new XidImpl(xid);
   Transaction transaction = server.getResourceManager().getTransaction(xid1);
   assertNull(transaction);
}
 
Example #3
Source File: JMSBridgeImpl.java    From activemq-artemis with Apache License 2.0 6 votes vote down vote up
private static void releaseCommunications(Session session, String sessionName) {
   final XASession xaSession = (XASession) session;

   if (xaSession.getXAResource() instanceof ClientSessionInternal) {
      try {
         ((ClientSessionInternal) xaSession.getXAResource()).getSessionContext().releaseCommunications();
      } catch (Throwable t) {
         ActiveMQJMSBridgeLogger.LOGGER.warnf(t, "Cannot release communications on %s", sessionName);
      }
   } else {
      if (ActiveMQJMSBridgeLogger.LOGGER.isTraceEnabled()) {
         ActiveMQJMSBridgeLogger.LOGGER.tracef("Cannot cast XAResource of %s to ClientSessionInternal and release communications: found class %s",
                                               sessionName, xaSession.getClass().getCanonicalName());
      }
   }
}
 
Example #4
Source File: JMSBridgeImpl.java    From activemq-artemis with Apache License 2.0 6 votes vote down vote up
private void enlistResources(final Transaction tx) throws Exception {
   if (ActiveMQJMSBridgeLogger.LOGGER.isTraceEnabled()) {
      ActiveMQJMSBridgeLogger.LOGGER.trace("Enlisting resources in tx");
   }

   XAResource resSource = ((XASession) sourceSession).getXAResource();

   tx.enlistResource(resSource);

   XAResource resDest = ((XASession) targetSession).getXAResource();

   tx.enlistResource(resDest);

   if (ActiveMQJMSBridgeLogger.LOGGER.isTraceEnabled()) {
      ActiveMQJMSBridgeLogger.LOGGER.trace("Enlisted resources in tx");
   }
}
 
Example #5
Source File: SimpleOpenWireTest.java    From activemq-artemis with Apache License 2.0 6 votes vote down vote up
@Test
public void testXAPrepare() throws Exception {
   try {

      XAConnection connection = xaFactory.createXAConnection();

      XASession xasession = connection.createXASession();

      Xid xid = newXID();
      xasession.getXAResource().start(xid, XAResource.TMNOFLAGS);
      Queue queue = xasession.createQueue(queueName);
      MessageProducer producer = xasession.createProducer(queue);
      producer.send(xasession.createTextMessage("hello"));
      producer.send(xasession.createTextMessage("hello"));
      xasession.getXAResource().end(xid, XAResource.TMSUCCESS);

      xasession.getXAResource().prepare(xid);

      connection.close();

      System.err.println("Done!!!");
   } catch (Exception e) {
      e.printStackTrace();
   }
}
 
Example #6
Source File: ActiveMQXAConnectionFactoryTest.java    From activemq-artemis with Apache License 2.0 6 votes vote down vote up
public void testRollbackXaErrorCode() throws Exception {
   String brokerName = "rollbackErrorCode";
   BrokerService broker = BrokerFactory.createBroker(new URI("broker:(tcp://localhost:0)/" + brokerName));
   broker.start();
   broker.waitUntilStarted();
   ActiveMQXAConnectionFactory cf = new ActiveMQXAConnectionFactory(broker.getTransportConnectors().get(0).getConnectUri());
   XAConnection connection = (XAConnection) cf.createConnection();
   connection.start();
   XASession session = connection.createXASession();
   XAResource resource = session.getXAResource();

   Xid tid = createXid();
   try {
      resource.rollback(tid);
      fail("Expected xa exception on no tx");
   } catch (XAException expected) {
      LOG.info("got expected xa", expected);
      assertEquals("no tx", XAException.XAER_NOTA, expected.errorCode);
   }
   connection.close();
   broker.stop();
}
 
Example #7
Source File: JmsPoolSession.java    From pooled-jms with Apache License 2.0 5 votes vote down vote up
@Override
public XAResource getXAResource() {
    final PooledSessionHolder session;
    try {
        session = safeGetSessionHolder();
    } catch (JMSException e) {
        throw JMSExceptionSupport.createRuntimeException(e);
    }

    if (session.getSession() instanceof XASession) {
        return ((XASession) session.getSession()).getXAResource();
    }

    return null;
}
 
Example #8
Source File: FailureXATest.java    From activemq-artemis with Apache License 2.0 5 votes vote down vote up
private void doTestCrashServerAfterXACommit(boolean onePhase) throws Exception {
   ActiveMQConnectionFactory connectionFactory = new ActiveMQConnectionFactory();
   XAConnection connection = connectionFactory.createXAConnection();

   try {
      Session session = connection.createSession(true, Session.SESSION_TRANSACTED);
      Queue queue = session.createQueue("Queue1");
      final XASession xaSession = connection.createXASession();
      MessageConsumer consumer = xaSession.createConsumer(queue);

      MessageProducer producer = session.createProducer(queue);
      producer.send(session.createTextMessage("hello " + 1));
      session.commit();

      XAResource xaResource = xaSession.getXAResource();
      final Xid xid = newXID();
      xaResource.start(xid, XAResource.TMNOFLAGS);

      connection.start();
      Assert.assertNotNull(consumer.receive(5000));

      xaResource.end(xid, XAResource.TMSUCCESS);

      try {
         xaResource.commit(xid, onePhase);
         Assert.fail("didn't get expected exception!");
      } catch (XAException xae) {
         if (onePhase) {
            //expected error code is XAER_RMFAIL
            Assert.assertEquals(XAException.XAER_RMFAIL, xae.errorCode);
         } else {
            //expected error code is XA_RETRY
            Assert.assertEquals(XAException.XA_RETRY, xae.errorCode);
         }
      }
   } finally {
      connection.close();
   }
}
 
Example #9
Source File: DtxCommitNegativeTestCase.java    From product-ei with Apache License 2.0 5 votes vote down vote up
/**
 * Tests if committing a DTX branch without starting it throws an exception
 */
@Test(groups = { "wso2.mb", "dtx" }, expectedExceptions = XAException.class,
      expectedExceptionsMessageRegExp = ".*Error while committing dtx session.*")
public void commitDtxBranchWithoutEnding()
        throws NamingException, JMSException, XAException, XPathExpressionException {
    String queueName = "DtxCommitTestCaseCommitDtxBranchWithoutEnding";

    InitialContext initialContext = JMSClientHelper
            .createInitialContextBuilder("admin", "admin", "localhost", getAMQPPort())
            .withQueue(queueName)
            .build();

    // Publish to queue and rollback
    XAConnectionFactory connectionFactory = (XAConnectionFactory) initialContext
            .lookup(JMSClientHelper.QUEUE_XA_CONNECTION_FACTORY);

    XAConnection xaConnection = connectionFactory.createXAConnection();
    xaConnection.start();
    XASession xaSession = xaConnection.createXASession();

    XAResource xaResource = xaSession.getXAResource();
    Session session = xaSession.getSession();

    Destination xaTestQueue = (Destination) initialContext.lookup(queueName);
    session.createQueue(queueName);
    MessageProducer producer = session.createProducer(xaTestQueue);

    Xid xid = JMSClientHelper.getNewXid();

    xaResource.start(xid, XAResource.TMNOFLAGS);
    producer.send(session.createTextMessage("Test 1"));
    // xaResource.end(xid, XAResource.TMSUCCESS);

    // xaResource.prepare(xid);

    xaResource.commit(xid, false);

    session.close();
    xaConnection.close();
}
 
Example #10
Source File: SimpleOpenWireTest.java    From activemq-artemis with Apache License 2.0 5 votes vote down vote up
@Test
public void testXASimple() throws Exception {
   XAConnection connection = xaFactory.createXAConnection();

   Collection<Session> sessions = new LinkedList<>();

   for (int i = 0; i < 10; i++) {
      XASession session = connection.createXASession();
      session.getXAResource().start(newXID(), XAResource.TMNOFLAGS);
      sessions.add(session);
   }

   connection.close();

}
 
Example #11
Source File: ActiveMQXAConnectionFactoryTest.java    From activemq-artemis with Apache License 2.0 5 votes vote down vote up
public void testCloseSendConnection() throws Exception {
   String brokerName = "closeSend";
   BrokerService broker = BrokerFactory.createBroker(new URI("broker:(tcp://localhost:0)/" + brokerName));
   broker.start();
   broker.waitUntilStarted();
   ActiveMQXAConnectionFactory cf = new ActiveMQXAConnectionFactory(broker.getTransportConnectors().get(0).getConnectUri());
   XAConnection connection = (XAConnection) cf.createConnection();
   connection.start();
   XASession session = connection.createXASession();
   XAResource resource = session.getXAResource();
   Destination dest = new ActiveMQQueue(getName());

   // publish a message
   Xid tid = createXid();
   resource.start(tid, XAResource.TMNOFLAGS);
   MessageProducer producer = session.createProducer(dest);
   ActiveMQTextMessage message = new ActiveMQTextMessage();
   message.setText(getName());
   producer.send(message);

   connection.close();

   //comment out this check as it doesn't apply to artemis
   //assertTransactionGoneFromBroker(tid);

   broker.stop();
}
 
Example #12
Source File: DtxPrepareNegativeTestCase.java    From product-ei with Apache License 2.0 5 votes vote down vote up
/**
 * Tests if preparing a DTX branch after setting fail flag in dtx.end throws an exception
 */
@Test(groups = { "wso2.mb", "dtx" }, expectedExceptions = XAException.class)
public void prepareDtxBranchAfterEndFails()
        throws NamingException, JMSException, XAException, XPathExpressionException {
    String queueName = "DtxPrepareTestCasePrepareDtxBranchAfterEndFails";

    InitialContext initialContext = JMSClientHelper
            .createInitialContextBuilder("admin", "admin", "localhost", getAMQPPort())
            .withQueue(queueName)
            .build();

    // Publish to queue and rollback
    XAConnectionFactory connectionFactory = (XAConnectionFactory) initialContext
            .lookup(JMSClientHelper.QUEUE_XA_CONNECTION_FACTORY);

    XAConnection xaConnection = connectionFactory.createXAConnection();
    xaConnection.start();
    XASession xaSession = xaConnection.createXASession();

    XAResource xaResource = xaSession.getXAResource();
    Session session = xaSession.getSession();

    Destination xaTestQueue = (Destination) initialContext.lookup(queueName);
    session.createQueue(queueName);
    MessageProducer producer = session.createProducer(xaTestQueue);

    Xid xid = JMSClientHelper.getNewXid();

    // We are not starting the dtx branch
    xaResource.start(xid, XAResource.TMNOFLAGS);
    producer.send(session.createTextMessage("Test 1"));
    xaResource.end(xid, XAResource.TMFAIL);

    xaResource.prepare(xid);

    xaResource.rollback(xid);

    session.close();
    xaConnection.close();
}
 
Example #13
Source File: SessionTest.java    From activemq-artemis with Apache License 2.0 5 votes vote down vote up
@Test
public void testGetXAResource2() throws Exception {
   XAConnection conn = getXAConnectionFactory().createXAConnection();
   XASession sess = conn.createXASession();

   sess.getXAResource();
   conn.close();
}
 
Example #14
Source File: ActiveMQRASessionFactoryImpl.java    From activemq-artemis with Apache License 2.0 5 votes vote down vote up
/**
 * Create a XA session
 *
 * @return The XA session
 * @throws JMSException Thrown if an error occurs
 */
@Override
public XASession createXASession() throws JMSException {
   if (ActiveMQRALogger.LOGGER.isTraceEnabled()) {
      ActiveMQRALogger.LOGGER.trace("createXASession()");
   }

   checkClosed();
   return allocateConnection(type);
}
 
Example #15
Source File: QueueDistributedTransactionTest.java    From ballerina-message-broker with Apache License 2.0 5 votes vote down vote up
@Test
public void testPublisherWithRollback() throws NamingException, JMSException, XAException, IOException {
    String queueName = "testPublisherWithRollback";
    String testMessage = "testPublisherWithRollback-Message";
    InitialContext initialContext = initialContextBuilder.withXaConnectionFactory()
                                                         .withQueue(queueName)
                                                         .build();

    XAConnectionFactory xaConnectionFactory =
            (XAConnectionFactory) initialContext.lookup(ClientHelper.XA_CONNECTION_FACTORY);

    XAConnection xaConnection = xaConnectionFactory.createXAConnection();
    XASession xaSession = xaConnection.createXASession();
    XAResource xaResource = xaSession.getXAResource();

    Session session = xaSession.getSession();
    Queue queue = session.createQueue(queueName);
    MessageProducer producer = session.createProducer(queue);
    xaConnection.start();

    XidImpl xid = new XidImpl(0, "branchId_1".getBytes(), "globalId_1".getBytes());
    xaResource.start(xid, XAResource.TMNOFLAGS);
    producer.send(session.createTextMessage(testMessage));
    xaResource.end(xid, XAResource.TMSUCCESS);

    int prepareOK = xaResource.prepare(xid);
    Assert.assertEquals(prepareOK, XAResource.XA_OK, "Prepare phase should return XA_OK");

    xaResource.rollback(xid);

    // Check whether the message is published to queue.
    QueueMetadata queueMetadata = restApiClient.getQueueMetadata(queueName);

    Assert.assertEquals((int) queueMetadata.getSize(), 0, "Queue should be empty");

    session.close();
    xaConnection.close();
}
 
Example #16
Source File: ConnectionTest.java    From activemq-artemis with Apache License 2.0 5 votes vote down vote up
@Test
public void testXAInstanceof() throws Exception {
   conn = cf.createConnection();

   assertFalse(conn instanceof XAConnection);
   Session sess = conn.createSession(false, Session.AUTO_ACKNOWLEDGE);

   assertFalse(sess instanceof XASession);
}
 
Example #17
Source File: OutgoingConnectionTest.java    From activemq-artemis with Apache License 2.0 5 votes vote down vote up
@Test
public void testOutgoingXAResourceWrapper() throws Exception {
   XAQueueConnection queueConnection = qraConnectionFactory.createXAQueueConnection();
   XASession s = queueConnection.createXASession();

   XAResource resource = s.getXAResource();
   assertTrue(resource instanceof ActiveMQXAResourceWrapper);

   ActiveMQXAResourceWrapperImpl xaResourceWrapper = (ActiveMQXAResourceWrapperImpl) resource;
   assertTrue(xaResourceWrapper.getJndiName().equals("java://jmsXA NodeId:" + server.getNodeID()));
   assertTrue(xaResourceWrapper.getProductVersion().equals(VersionLoader.getVersion().getFullVersion()));
   assertTrue(xaResourceWrapper.getProductName().equals(ActiveMQResourceAdapter.PRODUCT_NAME));
}
 
Example #18
Source File: SessionTest.java    From activemq-artemis with Apache License 2.0 5 votes vote down vote up
@Test
public void testGetSession2() throws Exception {
   deployConnectionFactory(0, JMSFactoryType.CF, "ConnectionFactory", "/ConnectionFactory");
   XAConnection conn = getXAConnectionFactory().createXAConnection();
   XASession sess = conn.createXASession();

   sess.getSession();
   conn.close();
}
 
Example #19
Source File: PooledJCAConnection.java    From pooled-jms with Apache License 2.0 5 votes vote down vote up
@Override
protected XAResource createXaResource(JmsPoolSession session) throws JMSException {
    XAResource xares = ((XASession)session.getInternalSession()).getXAResource();
    if (name != null) {
        xares = new WrapperNamedXAResource(xares, name);
    }
    return xares;
}
 
Example #20
Source File: XAJMSContextImpl.java    From tomee with Apache License 2.0 4 votes vote down vote up
@Override
public XAResource getXAResource() {
    return XASession.class.cast(session()).getXAResource();
}
 
Example #21
Source File: ActiveMQJMSContext.java    From activemq-artemis with Apache License 2.0 4 votes vote down vote up
public XAResource getXAResource() {
   checkSession();
   return ((XASession) session).getXAResource();
}
 
Example #22
Source File: ActiveMQXAConnection.java    From activemq-artemis with Apache License 2.0 4 votes vote down vote up
@Override
public synchronized XASession createXASession() throws JMSException {
   checkClosed();
   return (XASession) createSessionInternal(isXA(), true, Session.SESSION_TRANSACTED, ActiveMQSession.TYPE_GENERIC_SESSION);
}
 
Example #23
Source File: XATest.java    From activemq-artemis with Apache License 2.0 4 votes vote down vote up
@Test
public void testMultipleSessionsOneTxCommitSend1PCOptimization() throws Exception {
   // Since both resources have some RM, TM will probably use 1PC optimization

   XAConnection conn = null;

   Connection conn2 = null;

   try {
      conn = xacf.createXAConnection();
      conn.start();

      tm.begin();

      // Create 2 sessions and enlist them
      XASession sess1 = conn.createXASession();
      XAResource res1 = sess1.getXAResource();
      XASession sess2 = conn.createXASession();
      XAResource res2 = sess2.getXAResource();

      Transaction tx = tm.getTransaction();
      tx.enlistResource(res1);
      tx.enlistResource(res2);

      // Send 2 messages - one from each session

      MessageProducer prod1 = sess1.createProducer(queue1);
      MessageProducer prod2 = sess2.createProducer(queue1);

      prod1.send(sess1.createTextMessage("echidna1"));
      prod2.send(sess2.createTextMessage("echidna2"));

      tx.delistResource(res1, XAResource.TMSUCCESS);
      tx.delistResource(res2, XAResource.TMSUCCESS);

      // commit
      tm.commit();

      // Messages should be in queue

      conn2 = cf.createConnection();
      Session sess = conn2.createSession(false, Session.AUTO_ACKNOWLEDGE);
      MessageConsumer cons = sess.createConsumer(queue1);
      conn2.start();

      TextMessage r1 = (TextMessage) cons.receive(5000);
      Assert.assertNotNull(r1);
      Assert.assertEquals("echidna1", r1.getText());

      TextMessage r2 = (TextMessage) cons.receive(5000);
      Assert.assertNotNull(r2);
      Assert.assertEquals("echidna2", r2.getText());

   } finally {
      if (conn != null) {
         conn.close();
      }
      if (conn2 != null) {
         conn2.close();
      }
   }
}
 
Example #24
Source File: XATest.java    From activemq-artemis with Apache License 2.0 4 votes vote down vote up
@Test
public void testMultipleSessionsOneTxCommitAcknowledge() throws Exception {
   XAConnection conn = null;
   Connection conn2 = null;

   try {
      // First send 2 messages
      conn2 = cf.createConnection();
      Session sessProducer = conn2.createSession(false, Session.AUTO_ACKNOWLEDGE);
      MessageProducer prod = sessProducer.createProducer(queue1);
      Message m = sessProducer.createTextMessage("jellyfish1");
      prod.send(m);
      m = sessProducer.createTextMessage("jellyfish2");
      prod.send(m);

      conn = xacf.createXAConnection();
      conn.start();

      tm.begin();

      // Create 2 sessions and enlist them
      XASession sess1 = conn.createXASession();
      ClientSessionInternal res1 = (ClientSessionInternal) sess1.getXAResource();
      XASession sess2 = conn.createXASession();
      ClientSessionInternal res2 = (ClientSessionInternal) sess2.getXAResource();
      res1.setForceNotSameRM(true);
      res2.setForceNotSameRM(true);

      Transaction tx = tm.getTransaction();
      tx.enlistResource(res1);
      tx.enlistResource(res2);

      // Receive the messages, one on each consumer
      MessageConsumer cons1 = sess1.createConsumer(queue1);
      TextMessage r1 = (TextMessage) cons1.receive(5000);

      Assert.assertNotNull(r1);
      Assert.assertEquals("jellyfish1", r1.getText());

      cons1.close();

      MessageConsumer cons2 = sess2.createConsumer(queue1);
      TextMessage r2 = (TextMessage) cons2.receive(5000);

      Assert.assertNotNull(r2);
      Assert.assertEquals("jellyfish2", r2.getText());

      tx.delistResource(res1, XAResource.TMSUCCESS);
      tx.delistResource(res2, XAResource.TMSUCCESS);

      // commit
      tm.commit();

      Session sess = conn2.createSession(false, Session.AUTO_ACKNOWLEDGE);
      MessageConsumer cons = sess.createConsumer(queue1);
      conn2.start();

      TextMessage r3 = (TextMessage) cons.receive(100);
      Assert.assertNull(r3);

   } finally {
      if (conn != null) {
         conn.close();
      }
      if (conn2 != null) {
         conn2.close();
      }
   }

}
 
Example #25
Source File: XATest.java    From activemq-artemis with Apache License 2.0 4 votes vote down vote up
@Test
public void testMultipleSessionsOneTxCommitAcknowledge1PCOptimization() throws Exception {
   XAConnection conn = null;
   Connection conn2 = null;

   // Since both resources have some RM, TM will probably use 1PC optimization

   try {
      // First send 2 messages
      conn2 = cf.createConnection();
      Session sessProducer = conn2.createSession(false, Session.AUTO_ACKNOWLEDGE);
      MessageProducer prod = sessProducer.createProducer(queue1);
      Message m = sessProducer.createTextMessage("jellyfish1");
      prod.send(m);
      m = sessProducer.createTextMessage("jellyfish2");
      prod.send(m);

      conn = xacf.createXAConnection();
      conn.start();

      tm.begin();

      // Create 2 sessions and enlist them
      XASession sess1 = conn.createXASession();
      XAResource res1 = sess1.getXAResource();
      XASession sess2 = conn.createXASession();
      XAResource res2 = sess2.getXAResource();

      Transaction tx = tm.getTransaction();
      tx.enlistResource(res1);
      tx.enlistResource(res2);

      // Receive the messages, one on each consumer
      MessageConsumer cons1 = sess1.createConsumer(queue1);
      TextMessage r1 = (TextMessage) cons1.receive(5000);

      Assert.assertNotNull(r1);
      Assert.assertEquals("jellyfish1", r1.getText());

      cons1.close();

      MessageConsumer cons2 = sess2.createConsumer(queue1);
      TextMessage r2 = (TextMessage) cons2.receive(5000);

      Assert.assertNotNull(r2);
      Assert.assertEquals("jellyfish2", r2.getText());

      tx.delistResource(res1, XAResource.TMSUCCESS);
      tx.delistResource(res2, XAResource.TMSUCCESS);

      // commit
      tm.commit();

      Session sess = conn2.createSession(false, Session.AUTO_ACKNOWLEDGE);
      MessageConsumer cons = sess.createConsumer(queue1);
      conn2.start();

      TextMessage r3 = (TextMessage) cons.receive(100);
      Assert.assertNull(r3);

   } finally {
      if (conn != null) {
         conn.close();
      }
      if (conn2 != null) {
         conn2.close();
      }
   }

}
 
Example #26
Source File: XATest.java    From activemq-artemis with Apache License 2.0 4 votes vote down vote up
@Test
public void test2PCSendRollback() throws Exception {
   XAConnection conn = null;
   Connection conn2 = null;
   try {
      conn = xacf.createXAConnection();

      tm.begin();

      XASession sess = conn.createXASession();
      XAResource res = sess.getXAResource();

      // prevent 1Pc optimisation
      // res.setForceNotSameRM(true);

      XAResource res2 = new DummyXAResource();

      Transaction tx = tm.getTransaction();
      tx.enlistResource(res);
      tx.enlistResource(res2);

      MessageProducer prod = sess.createProducer(null);
      prod.setDeliveryMode(DeliveryMode.NON_PERSISTENT);
      Message m = sess.createTextMessage("XATest1");
      prod.send(queue1, m);
      m = sess.createTextMessage("XATest2");
      prod.send(queue1, m);

      tx.delistResource(res, XAResource.TMSUCCESS);
      tx.delistResource(res2, XAResource.TMSUCCESS);

      tm.rollback();

      conn2 = cf.createConnection();
      conn2.start();
      Session sessReceiver = conn2.createSession(false, Session.AUTO_ACKNOWLEDGE);
      MessageConsumer cons = sessReceiver.createConsumer(queue1);
      Message m2 = cons.receive(100);
      Assert.assertNull(m2);

   } finally {
      if (conn != null) {
         conn.close();
      }
      if (conn2 != null) {
         conn2.close();
      }
   }
}
 
Example #27
Source File: XATest.java    From activemq-artemis with Apache License 2.0 4 votes vote down vote up
@Test
public void test2PCSendFailOnPrepare() throws Exception {
   XAConnection conn = null;
   Connection conn2 = null;
   try {
      conn = xacf.createXAConnection();

      tm.begin();

      XASession sess = conn.createXASession();
      XAResource res = sess.getXAResource();

      // prevent 1Pc optimisation
      // res.setForceNotSameRM(true);

      XAResource res2 = new DummyXAResource(true);
      XAResource res3 = new DummyXAResource();
      XAResource res4 = new DummyXAResource();

      Transaction tx = tm.getTransaction();
      tx.enlistResource(res);
      tx.enlistResource(res2);
      tx.enlistResource(res3);
      tx.enlistResource(res4);

      MessageProducer prod = sess.createProducer(null);
      prod.setDeliveryMode(DeliveryMode.NON_PERSISTENT);
      Message m = sess.createTextMessage("XATest1");
      prod.send(queue1, m);
      m = sess.createTextMessage("XATest2");
      prod.send(queue1, m);

      tx.delistResource(res, XAResource.TMSUCCESS);
      tx.delistResource(res2, XAResource.TMSUCCESS);
      tx.delistResource(res3, XAResource.TMSUCCESS);
      tx.delistResource(res4, XAResource.TMSUCCESS);

      try {
         tm.commit();

         Assert.fail("should not get here");
      } catch (Exception e) {
         // We should expect this
      }

      conn2 = cf.createConnection();
      conn2.start();
      Session sessReceiver = conn2.createSession(false, Session.AUTO_ACKNOWLEDGE);
      MessageConsumer cons = sessReceiver.createConsumer(queue1);
      Message m2 = cons.receive(100);
      Assert.assertNull(m2);
   } finally {
      if (conn != null) {
         conn.close();
      }
      if (conn2 != null) {
         conn2.close();
      }
   }
}
 
Example #28
Source File: DtxStartPositiveTestCase.java    From product-ei with Apache License 2.0 4 votes vote down vote up
/**
 * Tests if publishing messages works correctly with session joining. Steps are,
 * 1. Create two distributed transaction sessions and join one session to other.
 * 2. Publish messages using two sessions.
 * 3. Subscribe to the published queue and see if any message is received.
 * 4. Commit the session
 * 5. Subscribe to the queue and see if two messages are received
 */
@Test(groups = { "wso2.mb", "dtx" })
public void xaMultiSessionPublishTestCase()
        throws NamingException, JMSException, XAException, XPathExpressionException {
    String queueName = "DtxStartPositiveTestCaseXaMultiSessionPublishTestCase";

    InitialContext initialContext = JMSClientHelper.createInitialContextBuilder("admin", "admin", "localhost",
                                                                                getAMQPPort()).withQueue(queueName).build();

    XAConnectionFactory xaConnectionFactory = (XAConnectionFactory) initialContext
            .lookup(JMSClientHelper.QUEUE_XA_CONNECTION_FACTORY);

    // Create XA resource one
    XAConnection xaConnectionOne = xaConnectionFactory.createXAConnection();
    xaConnectionOne.start();
    XASession xaSessionOne = xaConnectionOne.createXASession();

    XAResource xaResourceOne = xaSessionOne.getXAResource();
    Session sessionOne = xaSessionOne.getSession();

    Destination xaTestQueue = (Destination) initialContext.lookup(queueName);
    sessionOne.createQueue(queueName);
    MessageProducer producerOne = sessionOne.createProducer(xaTestQueue);

    // Create XA resource two
    XASession xaSessionTwo = xaConnectionOne.createXASession();

    XAResource xaResourceTwo = xaSessionTwo.getXAResource();
    Session sessionTwo = xaSessionTwo.getSession();

    MessageProducer producerTwo = sessionTwo.createProducer(xaTestQueue);

    Xid xid = JMSClientHelper.getNewXid();

    boolean sameRM = xaResourceOne.isSameRM(xaResourceTwo);

    Assert.assertEquals(sameRM, true, "Resource one and resource two are connected to different resource "
            + "managers");

    xaResourceOne.start(xid, XAResource.TMNOFLAGS);
    xaResourceTwo.start(xid, XAResource.TMJOIN);

    producerOne.send(sessionOne.createTextMessage("Test 1"));
    producerTwo.send(sessionTwo.createTextMessage("Test 2"));

    xaResourceOne.end(xid, XAResource.TMSUCCESS);

    // subscribe and see if the message is received
    ConnectionFactory nonXaConnectionFactory = (ConnectionFactory) initialContext
            .lookup(JMSClientHelper.QUEUE_CONNECTION_FACTORY);
    Connection nonXaQueueConnection = nonXaConnectionFactory.createConnection();
    nonXaQueueConnection.start();
    Session nonXaQueueSession = nonXaQueueConnection.createSession(false, Session.AUTO_ACKNOWLEDGE);
    MessageConsumer messageConsumer = nonXaQueueSession.createConsumer(xaTestQueue);

    // wait 5 seconds
    Message receive = messageConsumer.receive(5000);
    Assert.assertNull(receive, "Message received before committing");

    xaResourceOne.prepare(xid);
    xaResourceOne.commit(xid, false);

    xaConnectionOne.close();

    //This is only added to find out the reason for the intermittent failure of this test method. Should be removed
    // once the issue is identified.
    try {
        // Logging in
        LoginLogoutClient loginLogoutClientForAdmin = new LoginLogoutClient(super.automationContext);
        String sessionCookie = loginLogoutClientForAdmin.login();
        AndesAdminClient admin = new AndesAdminClient(super.backendURL, sessionCookie);

        //Check message count in queue
        org.wso2.carbon.andes.stub.admin.types.Message[] queueOneMessages
                = admin.browseQueue(queueName, 0, 10);
        Assert.assertEquals(queueOneMessages.length, 2, "Message not published to queue " + queueName);

        //Logging out
        loginLogoutClientForAdmin.logout();

    } catch (RemoteException | AutomationUtilException | AndesAdminServiceBrokerManagerAdminException
            | LogoutAuthenticationExceptionException e) {
        e.printStackTrace();
    }

    receive = messageConsumer.receive(5000);
    Assert.assertNotNull(receive, "Message not received");

    receive = messageConsumer.receive(5000);
    Assert.assertNotNull(receive, "Message not received");

    nonXaQueueConnection.close();
}
 
Example #29
Source File: TracingXASession.java    From brave with Apache License 2.0 4 votes vote down vote up
static TracingXASession create(XASession delegate, JmsTracing jmsTracing) {
  if (delegate instanceof TracingXASession) return (TracingXASession) delegate;
  return new TracingXASession(delegate, jmsTracing);
}
 
Example #30
Source File: DtxStartPositiveTestCase.java    From product-ei with Apache License 2.0 4 votes vote down vote up
/**
 * Tests if acknowledging a messages works correctly with session joining. Steps are,
 * 1. Publish two messages to two queues using two non-transacted sessions
 * 2. Create two distributed transaction sessions and join one session to other.
 * 3. Receive messages and ack using two sessions.
 * 4. Commit the session
 * 5. Subscribe to the published queue and see if any message is received.
 */
@Test(groups = { "wso2.mb", "dtx" })
public void xaStartJoinMessageAckTestCase()
        throws NamingException, JMSException, XAException, XPathExpressionException {
    String queueNameOne = "DtxStartPositiveTestCaseXaStartJoinMessageAckTestCaseOne";
    String queueNameTwo = "DtxStartPositiveTestCaseXaStartJoinMessageAckTestCaseTwo";

    InitialContext initialContext = JMSClientHelper.createInitialContextBuilder("admin", "admin", "localhost",
            getAMQPPort()).withQueue(queueNameOne).build();

    ConnectionFactory nonXaConnectionFactory = (ConnectionFactory) initialContext
            .lookup(JMSClientHelper.QUEUE_CONNECTION_FACTORY);
    Connection nonXaQueueConnection = nonXaConnectionFactory.createConnection();
    nonXaQueueConnection.start();
    Session nonXaQueueSessionOne = nonXaQueueConnection.createSession(false, Session.AUTO_ACKNOWLEDGE);

    Destination xaTestQueueOne = nonXaQueueSessionOne.createQueue(queueNameOne);
    Destination xaTestQueueTwo = nonXaQueueSessionOne.createQueue(queueNameTwo);

    MessageProducer nonXaQueueSessionProducerOne = nonXaQueueSessionOne.createProducer(xaTestQueueOne);
    MessageProducer nonXaQueueSessionProducerTwo = nonXaQueueSessionOne.createProducer(xaTestQueueTwo);

    nonXaQueueSessionProducerOne.send(nonXaQueueSessionOne.createTextMessage("Message 1"));
    nonXaQueueSessionProducerTwo.send(nonXaQueueSessionOne.createTextMessage("Message 2"));

    nonXaQueueSessionProducerOne.close();
    nonXaQueueSessionProducerTwo.close();

    XAConnectionFactory xaConnectionFactory = (XAConnectionFactory) initialContext
            .lookup(JMSClientHelper.QUEUE_XA_CONNECTION_FACTORY);

    // Create XA resource one
    XAConnection xaConnectionOne = xaConnectionFactory.createXAConnection();
    xaConnectionOne.start();
    XASession xaSessionOne = xaConnectionOne.createXASession();

    XAResource xaResourceOne = xaSessionOne.getXAResource();
    Session sessionOne = xaSessionOne.getSession();

    MessageConsumer xaConsumerOne = sessionOne.createConsumer(xaTestQueueOne);

    // Create XA resource two
    XAConnection xaConnectionTwo = xaConnectionFactory.createXAConnection();
    xaConnectionTwo.start();
    XASession xaSessionTwo = xaConnectionTwo.createXASession();

    XAResource xaResourceTwo = xaSessionTwo.getXAResource();
    Session sessionTwo = xaSessionTwo.getSession();

    MessageConsumer xaConsumerTwo = sessionTwo.createConsumer(xaTestQueueTwo);

    Xid xid = JMSClientHelper.getNewXid();

    boolean sameRM = xaResourceOne.isSameRM(xaResourceTwo);

    Assert.assertEquals(sameRM, true, "Resource one and resource two are connected to different resource "
            + "managers");

    xaResourceOne.start(xid, XAResource.TMNOFLAGS);
    xaResourceTwo.start(xid, XAResource.TMJOIN);

    Message receivedMessageForQueueOne = xaConsumerOne.receive(5000);
    Assert.assertNotNull(receivedMessageForQueueOne, "A message was not received for queue " + queueNameOne);
    Message receivedMessageForQueueTwo = xaConsumerTwo.receive(5000);
    Assert.assertNotNull(receivedMessageForQueueTwo, "A message was not received for queue " + queueNameTwo);

    xaResourceOne.end(xid, XAResource.TMSUCCESS);

    xaResourceOne.prepare(xid);
    xaResourceOne.commit(xid, false);

    xaConnectionOne.close();
    xaConnectionTwo.close();

    // subscribe and see if the message is received
    MessageConsumer nonXaConsumerOne = nonXaQueueSessionOne.createConsumer(xaTestQueueOne);

    Session nonXaQueueSessionTwo = nonXaQueueConnection.createSession(false, Session.AUTO_ACKNOWLEDGE);
    MessageConsumer nonXaConsumerTwo = nonXaQueueSessionTwo.createConsumer(xaTestQueueTwo);

    // wait 3 seconds
    receivedMessageForQueueOne = nonXaConsumerOne.receive(3000);
    Assert.assertNull(receivedMessageForQueueOne, "Message received after committing for queue " + queueNameOne);

    receivedMessageForQueueTwo = nonXaConsumerTwo.receive(3000);
    Assert.assertNull(receivedMessageForQueueTwo, "Message received after committing for queue " + queueNameTwo);

    nonXaQueueConnection.close();
}