當前位置: 首頁>>代碼示例>>Java>>正文


Java Transaction.enlistResource方法代碼示例

本文整理匯總了Java中javax.transaction.Transaction.enlistResource方法的典型用法代碼示例。如果您正苦於以下問題:Java Transaction.enlistResource方法的具體用法?Java Transaction.enlistResource怎麽用?Java Transaction.enlistResource使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在javax.transaction.Transaction的用法示例。


在下文中一共展示了Transaction.enlistResource方法的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Java代碼示例。

示例1: registerTranxConnection

import javax.transaction.Transaction; //導入方法依賴的package包/類
/**
 *  
 */
void registerTranxConnection(XAConnection xaConn) throws Exception {
  try {
    synchronized (this) {
      if (transManager == null) {
        transManager = JNDIInvoker.getTransactionManager();
      }
    }
    Transaction txn = transManager.getTransaction();
    if (txn != null) {
      XAResource xar = xaConn.getXAResource();
      txn.enlistResource(xar);
      // Add in the Map after successful registration of XAResource
      this.xaResourcesMap.put(xaConn, xar);
    }
  } catch (Exception ex) {
    Exception e = new Exception(
        LocalizedStrings.GemFireTransactionDataSource_GEMFIRETRANSACTIONDATASOURCEREGISTERTRANXCONNECTION_EXCEPTION_IN_REGISTERING_THE_XARESOURCE_WITH_THE_TRANSACTIONEXCEPTION_OCCURED_0
            .toLocalizedString(ex));
    e.initCause(ex);
    throw e;
  }
}
 
開發者ID:ampool,項目名稱:monarch,代碼行數:26,代碼來源:GemFireTransactionDataSource.java

示例2: enlistResources

import javax.transaction.Transaction; //導入方法依賴的package包/類
private void enlistResources(final Transaction tx) throws Exception {
   if (JMSBridgeImpl.trace) {
      ActiveMQJMSBridgeLogger.LOGGER.trace("Enlisting resources in tx");
   }

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

   tx.enlistResource(resSource);

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

   tx.enlistResource(resDest);

   if (JMSBridgeImpl.trace) {
      ActiveMQJMSBridgeLogger.LOGGER.trace("Enlisted resources in tx");
   }
}
 
開發者ID:apache,項目名稱:activemq-artemis,代碼行數:18,代碼來源:JMSBridgeImpl.java

示例3: startTransaction

import javax.transaction.Transaction; //導入方法依賴的package包/類
/**
 * {@inheritDoc}
 */
@Override
public void startTransaction() throws RollbackException, JMSException {
    try {
        _transactionManager.begin();
        Transaction tx = _transactionManager.getTransaction();
        tx.enlistResource(getSession().getXAResource());
    }
    catch (NotSupportedException | SystemException e) {
        _logger.error("Failed to start transaction", e);
        throw new IllegalStateException("Failed to start transaction", e);
    }
}
 
開發者ID:erik-wramner,項目名稱:JmsTools,代碼行數:16,代碼來源:XAJmsResourceManager.java

示例4: performXATransactedSend

import javax.transaction.Transaction; //導入方法依賴的package包/類
private void performXATransactedSend(JMSClientConnector jmsClientConnector, String xaDestinationName)
        throws JMSConnectorException, SystemException, NotSupportedException, RollbackException,
        HeuristicRollbackException, HeuristicMixedException {

    SessionWrapper sessionWrapper;
    sessionWrapper = jmsClientConnector.acquireSession();

    // Get XAResource and JMS session from the XASession. XAResource is given to
    // the TM and JMS Session is given to the AP.
    XAResource xaResource = ((XASessionWrapper) sessionWrapper).getXASession().getXAResource();

    DistributedTxManagerProvider distributedTxManagerProvider = DistributedTxManagerProvider.getInstance();
    distributedTxManagerProvider.getTransactionManager().begin();

    Transaction transaction = distributedTxManagerProvider.getTransactionManager().getTransaction();
    transaction.enlistResource(xaResource);

    /* Publish 5 messages and rollback and then publish another 5 messages and commit.
       At the end there should only be total of 5 messages in the queue */

    // Send message using the received XASessionsWrapper
    for (int i = 0; i < 5; i++) {
        jmsClientConnector.sendTransactedMessage(jmsClientConnector.createMessage(JMSConstants.TEXT_MESSAGE_TYPE),
                xaDestinationName, sessionWrapper);
    }

    distributedTxManagerProvider.getTransactionManager().rollback();
    distributedTxManagerProvider.getTransactionManager().begin();

    for (int i = 0; i < 5; i++) {
        jmsClientConnector.sendTransactedMessage(jmsClientConnector.createMessage(JMSConstants.TEXT_MESSAGE_TYPE),
                xaDestinationName, sessionWrapper);
    }

    distributedTxManagerProvider.getTransactionManager().commit();
}
 
開發者ID:wso2,項目名稱:carbon-transports,代碼行數:37,代碼來源:QueueTopicXATransactedSenderTestCase.java

示例5: testOneSessionTwoTransactionsCommitAcknowledge

import javax.transaction.Transaction; //導入方法依賴的package包/類
@Test
public void testOneSessionTwoTransactionsCommitAcknowledge() 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();

      // Create a session
      XASession sess1 = conn.createXASession();
      XAResource res1 = sess1.getXAResource();

      conn.start();
      MessageConsumer cons1 = sess1.createConsumer(queue1);

      tm.begin();

      Transaction tx1 = tm.getTransaction();
      tx1.enlistResource(res1);

      // Receive one message in one tx

      TextMessage r1 = (TextMessage) cons1.receive(5000);
      Assert.assertNotNull(r1);
      Assert.assertEquals("jellyfish1", r1.getText());

      tx1.delistResource(res1, XAResource.TMSUCCESS);

      // suspend the tx
      Transaction suspended = tm.suspend();

      tm.begin();

      Transaction tx2 = tm.getTransaction();
      tx2.enlistResource(res1);

      // Receive 2nd message in a different tx
      TextMessage r2 = (TextMessage) cons1.receive(5000);
      Assert.assertNotNull(r2);
      Assert.assertEquals("jellyfish2", r2.getText());

      tx2.delistResource(res1, XAResource.TMSUCCESS);

      // commit this transaction
      tm.commit();

      // verify that no messages are available
      conn2.close();
      conn2 = cf.createConnection();
      Session sess = conn2.createSession(false, Session.AUTO_ACKNOWLEDGE);
      conn2.start();
      MessageConsumer cons = sess.createConsumer(queue1);
      TextMessage r3 = (TextMessage) cons.receive(100);
      Assert.assertNull(r3);

      // now resume the first tx and then commit it
      tm.resume(suspended);

      tm.commit();
   } finally {
      if (conn != null) {
         conn.close();
      }
      if (conn2 != null) {
         conn2.close();
      }
   }
}
 
開發者ID:apache,項目名稱:activemq-artemis,代碼行數:79,代碼來源:XATest.java

示例6: test2PCSendCommit1PCOptimization

import javax.transaction.Transaction; //導入方法依賴的package包/類
@Test
public void test2PCSendCommit1PCOptimization() throws Exception {
   // Since both resources have same RM, TM will probably use 1PC optimization

   XAConnection conn = null;
   Connection conn2 = null;

   try {
      conn = xacf.createXAConnection();

      tm.begin();

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

      XAResource res2 = new DummyXAResource();

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

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

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

      tm.commit();

      conn2 = cf.createConnection();
      conn2.start();
      Session sessReceiver = conn2.createSession(false, Session.AUTO_ACKNOWLEDGE);
      MessageConsumer cons = sessReceiver.createConsumer(queue1);
      TextMessage m2 = (TextMessage) cons.receive(1000);
      Assert.assertNotNull(m2);
      Assert.assertEquals("XATest1", m2.getText());
      m2 = (TextMessage) cons.receive(1000);
      Assert.assertNotNull(m2);
      Assert.assertEquals("XATest2", m2.getText());
   } finally {
      if (conn != null) {
         conn.close();
      }
      if (conn2 != null) {
         conn2.close();
      }
   }
}
 
開發者ID:apache,項目名稱:activemq-artemis,代碼行數:53,代碼來源:XATest.java

示例7: test2PCSendCommit

import javax.transaction.Transaction; //導入方法依賴的package包/類
@Test
public void test2PCSendCommit() throws Exception {
   XAConnection conn = null;
   Connection conn2 = null;

   try {
      conn = xacf.createXAConnection();

      tm.begin();

      XASession sess = conn.createXASession();

      XAResource res = sess.getXAResource();
      XAResource res2 = new DummyXAResource();

      // To prevent 1PC optimization being used
      // res.setForceNotSameRM(true);

      Transaction tx = tm.getTransaction();

      tx.enlistResource(res);

      tx.enlistResource(res2);

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

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

      tm.commit();

      conn2 = cf.createConnection();
      conn2.start();
      Session sessReceiver = conn2.createSession(false, Session.AUTO_ACKNOWLEDGE);
      MessageConsumer cons = sessReceiver.createConsumer(queue1);
      TextMessage m2 = (TextMessage) cons.receive(5000);
      Assert.assertNotNull(m2);
      Assert.assertEquals("XATest1", m2.getText());
      m2 = (TextMessage) cons.receive(5000);
      Assert.assertNotNull(m2);
      Assert.assertEquals("XATest2", m2.getText());
   } finally {
      if (conn != null) {
         conn.close();
      }
      if (conn2 != null) {
         conn2.close();
      }
   }
}
 
開發者ID:apache,項目名稱:activemq-artemis,代碼行數:56,代碼來源:XATest.java

示例8: test2PCSendFailOnPrepare

import javax.transaction.Transaction; //導入方法依賴的package包/類
@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();
      }
   }
}
 
開發者ID:apache,項目名稱:activemq-artemis,代碼行數:61,代碼來源:XATest.java

示例9: test2PCSendRollback

import javax.transaction.Transaction; //導入方法依賴的package包/類
@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();
      }
   }
}
 
開發者ID:apache,項目名稱:activemq-artemis,代碼行數:50,代碼來源:XATest.java

示例10: test2PCReceiveCommit1PCOptimization

import javax.transaction.Transaction; //導入方法依賴的package包/類
@Test
public void test2PCReceiveCommit1PCOptimization() throws Exception {
   // Since both resources have some RM, TM will probably use 1PC optimization

   XAConnection conn = null;
   Connection conn2 = null;

   try {
      conn2 = cf.createConnection();
      conn2.start();
      Session sessProducer = conn2.createSession(false, Session.AUTO_ACKNOWLEDGE);
      MessageProducer prod = sessProducer.createProducer(queue1);
      Message m = sessProducer.createTextMessage("XATest1");
      prod.send(m);
      m = sessProducer.createTextMessage("XATest2");
      prod.send(m);

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

      tm.begin();

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

      XAResource res2 = new DummyXAResource();

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

      MessageConsumer cons = sess.createConsumer(queue1);

      TextMessage m2 = (TextMessage) cons.receive(5000);

      Assert.assertNotNull(m2);
      Assert.assertEquals("XATest1", m2.getText());

      m2 = (TextMessage) cons.receive(5000);

      Assert.assertNotNull(m2);
      Assert.assertEquals("XATest2", m2.getText());

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

      tm.commit();

      // New tx
      tm.begin();
      tx = tm.getTransaction();
      tx.enlistResource(res);
      tx.enlistResource(res2);

      Message m3 = cons.receive(100);

      Assert.assertNull(m3);

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

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

}
 
開發者ID:apache,項目名稱:activemq-artemis,代碼行數:73,代碼來源:XATest.java

示例11: test2PCReceiveCommit

import javax.transaction.Transaction; //導入方法依賴的package包/類
@Test
public void test2PCReceiveCommit() throws Exception {
   XAConnection conn = null;
   Connection conn2 = null;

   try {
      conn2 = cf.createConnection();
      conn2.start();
      Session sessProducer = conn2.createSession(false, Session.AUTO_ACKNOWLEDGE);
      MessageProducer prod = sessProducer.createProducer(queue1);
      Message m = sessProducer.createTextMessage("XATest1");
      prod.send(m);
      m = sessProducer.createTextMessage("XATest2");
      prod.send(m);

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

      tm.begin();

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

      XAResource res2 = new DummyXAResource();

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

      MessageConsumer cons = sess.createConsumer(queue1);

      TextMessage m2 = (TextMessage) cons.receive(5000);

      Assert.assertNotNull(m2);
      Assert.assertEquals("XATest1", m2.getText());

      m2 = (TextMessage) cons.receive(5000);

      Assert.assertNotNull(m2);
      Assert.assertEquals("XATest2", m2.getText());

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

      tm.commit();

      // New tx
      tm.begin();
      tx = tm.getTransaction();
      tx.enlistResource(res);
      tx.enlistResource(res2);

      Message m3 = cons.receive(100);

      Assert.assertNull(m3);

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

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

}
 
開發者ID:apache,項目名稱:activemq-artemis,代碼行數:72,代碼來源:XATest.java

示例12: test2PCReceiveRollback1PCOptimization

import javax.transaction.Transaction; //導入方法依賴的package包/類
@Test
public void test2PCReceiveRollback1PCOptimization() throws Exception {
   // Since both resources have some RM, TM will probably use 1PC optimization

   XAConnection conn = null;
   Connection conn2 = null;

   try {
      conn2 = cf.createConnection();
      Session sessProducer = conn2.createSession(false, Session.AUTO_ACKNOWLEDGE);
      MessageProducer prod = sessProducer.createProducer(queue1);
      Message m = sessProducer.createTextMessage("XATest1");
      prod.send(m);

      m = sessProducer.createTextMessage("XATest2");
      prod.send(m);

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

      tm.begin();

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

      XAResource res2 = new DummyXAResource();

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

      MessageConsumer cons = sess.createConsumer(queue1);

      TextMessage m2 = (TextMessage) cons.receive(5000);
      Assert.assertNotNull(m2);
      Assert.assertEquals("XATest1", m2.getText());
      m2 = (TextMessage) cons.receive(5000);
      Assert.assertNotNull(m2);
      Assert.assertEquals("XATest2", m2.getText());

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

      tm.rollback();

      // Message should be redelivered

      // New tx
      tm.begin();
      tx = tm.getTransaction();
      tx.enlistResource(res);
      tx.enlistResource(res2);

      TextMessage m3 = (TextMessage) cons.receive(5000);
      Assert.assertNotNull(m3);
      Assert.assertEquals("XATest1", m3.getText());
      m3 = (TextMessage) cons.receive(5000);
      Assert.assertNotNull(m3);
      Assert.assertEquals("XATest2", m3.getText());

      Assert.assertTrue(m3.getJMSRedelivered());

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

      tm.commit();
   } finally {
      if (conn != null) {
         conn.close();
      }
      if (conn2 != null) {
         conn2.close();
      }
   }
}
 
開發者ID:apache,項目名稱:activemq-artemis,代碼行數:76,代碼來源:XATest.java

示例13: test2PCReceiveRollback

import javax.transaction.Transaction; //導入方法依賴的package包/類
@Test
public void test2PCReceiveRollback() throws Exception {
   XAConnection conn = null;
   Connection conn2 = null;

   try {
      conn2 = cf.createConnection();
      Session sessProducer = conn2.createSession(false, Session.AUTO_ACKNOWLEDGE);
      MessageProducer prod = sessProducer.createProducer(queue1);
      Message m = sessProducer.createTextMessage("XATest1");
      prod.send(m);

      m = sessProducer.createTextMessage("XATest2");
      prod.send(m);

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

      tm.begin();

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

      XAResource res2 = new DummyXAResource();

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

      MessageConsumer cons = sess.createConsumer(queue1);

      TextMessage m2 = (TextMessage) cons.receive(5000);
      Assert.assertNotNull(m2);
      Assert.assertEquals("XATest1", m2.getText());
      m2 = (TextMessage) cons.receive(5000);
      Assert.assertNotNull(m2);
      Assert.assertEquals("XATest2", m2.getText());

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

      tm.rollback();

      // Message should be redelivered

      // New tx
      tm.begin();
      tx = tm.getTransaction();
      tx.enlistResource(res);
      tx.enlistResource(res2);

      TextMessage m3 = (TextMessage) cons.receive(5000);
      Assert.assertNotNull(m3);
      Assert.assertEquals("XATest1", m3.getText());
      m3 = (TextMessage) cons.receive(5000);
      Assert.assertNotNull(m3);
      Assert.assertEquals("XATest2", m3.getText());

      Assert.assertTrue(m3.getJMSRedelivered());

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

      tm.commit();

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

}
 
開發者ID:apache,項目名稱:activemq-artemis,代碼行數:77,代碼來源:XATest.java

示例14: test1PCSendCommit

import javax.transaction.Transaction; //導入方法依賴的package包/類
@Test
public void test1PCSendCommit() throws Exception {
   XAConnection conn = null;
   Connection conn2 = null;

   try {
      conn = xacf.createXAConnection();

      tm.begin();

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

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

      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);

      tm.commit();

      conn2 = cf.createConnection();
      conn2.start();
      Session sessReceiver = conn2.createSession(false, Session.AUTO_ACKNOWLEDGE);
      MessageConsumer cons = sessReceiver.createConsumer(queue1);
      TextMessage m2 = (TextMessage) cons.receive(5000);
      Assert.assertNotNull(m2);
      Assert.assertEquals("XATest1", m2.getText());
      m2 = (TextMessage) cons.receive(5000);
      Assert.assertNotNull(m2);
      Assert.assertEquals("XATest2", m2.getText());
   } finally {
      if (conn != null) {
         conn.close();
      }
      if (conn2 != null) {
         conn2.close();
      }
   }

}
 
開發者ID:apache,項目名稱:activemq-artemis,代碼行數:48,代碼來源:XATest.java

示例15: testOneSessionTwoTransactionsCommitSend

import javax.transaction.Transaction; //導入方法依賴的package包/類
@Test
public void testOneSessionTwoTransactionsCommitSend() throws Exception {
   XAConnection conn = null;

   Connection conn2 = null;

   try {
      conn = xacf.createXAConnection();

      // Create a session
      XASession sess1 = conn.createXASession();
      XAResource res1 = sess1.getXAResource();

      MessageProducer prod1 = sess1.createProducer(queue1);

      tm.begin();

      Transaction tx1 = tm.getTransaction();
      tx1.enlistResource(res1);

      // Send a message
      prod1.send(sess1.createTextMessage("kangaroo1"));

      tx1.delistResource(res1, XAResource.TMSUCCESS);

      // suspend the tx
      Transaction suspended = tm.suspend();

      tm.begin();

      // Send another message in another tx using the same session
      Transaction tx2 = tm.getTransaction();
      tx2.enlistResource(res1);

      // Send a message
      prod1.send(sess1.createTextMessage("kangaroo2"));

      tx2.delistResource(res1, XAResource.TMSUCCESS);

      // commit this transaction
      tm.commit();

      // verify only kangaroo2 message is sent
      conn2 = cf.createConnection();
      Session sess = conn2.createSession(false, Session.AUTO_ACKNOWLEDGE);
      conn2.start();
      MessageConsumer cons = sess.createConsumer(queue1);
      TextMessage r1 = (TextMessage) cons.receive(5000);
      Assert.assertNotNull(r1);
      Assert.assertEquals("kangaroo2", r1.getText());
      TextMessage r2 = (TextMessage) cons.receive(100);
      Assert.assertNull(r2);

      // now resume the first tx and then commit it
      tm.resume(suspended);

      tm.commit();

      // verify that the first text message is received
      TextMessage r3 = (TextMessage) cons.receive(5000);
      Assert.assertNotNull(r3);
      Assert.assertEquals("kangaroo1", r3.getText());

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

   }

}
 
開發者ID:apache,項目名稱:activemq-artemis,代碼行數:75,代碼來源:XATest.java


注:本文中的javax.transaction.Transaction.enlistResource方法示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。