• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    公众号

Java TransactionRolledBackException类代码示例

原作者: [db:作者] 来自: [db:来源] 收藏 邀请

本文整理汇总了Java中javax.jms.TransactionRolledBackException的典型用法代码示例。如果您正苦于以下问题:Java TransactionRolledBackException类的具体用法?Java TransactionRolledBackException怎么用?Java TransactionRolledBackException使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。



TransactionRolledBackException类属于javax.jms包,在下文中一共展示了TransactionRolledBackException类的20个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于我们的系统推荐出更棒的Java代码示例。

示例1: rollbackOnFailedRecoveryRedelivery

import javax.jms.TransactionRolledBackException; //导入依赖的package包/类
private void rollbackOnFailedRecoveryRedelivery() throws JMSException {
    if (previouslyDeliveredMessages != null) {
        // if any previously delivered messages was not re-delivered, transaction is invalid and must rollback
        // as messages have been dispatched else where.
        int numberNotReplayed = 0;
        for (Entry<MessageId, Boolean> entry: previouslyDeliveredMessages.entrySet()) {
            if (!entry.getValue()) {
                numberNotReplayed++;
                if (LOG.isDebugEnabled()) {
                    LOG.debug("previously delivered message has not been replayed in transaction: "
                            + previouslyDeliveredMessages.transactionId
                            + " , messageId: " + entry.getKey());
                }
            }
        }
        if (numberNotReplayed > 0) {
            String message = "rolling back transaction ("
                + previouslyDeliveredMessages.transactionId + ") post failover recovery. " + numberNotReplayed
                + " previously delivered message(s) not replayed to consumer: " + this.getConsumerId();
            LOG.warn(message);
            throw new TransactionRolledBackException(message);
        }
    }
}
 
开发者ID:DiamondLightSource,项目名称:daq-eclipse,代码行数:25,代码来源:ActiveMQMessageConsumer.java


示例2: testTransactionCommitFails

import javax.jms.TransactionRolledBackException; //导入依赖的package包/类
@Test(timeout=20000)
public void testTransactionCommitFails() throws Exception {
    connection = (JmsConnection) factory.createConnection();
    connection.addConnectionListener(new ConnectionInterruptionListener());
    connection.start();

    Session session = connection.createSession(true, Session.SESSION_TRANSACTED);
    Queue queue = session.createQueue(_testName.getMethodName());
    MessageProducer producer = session.createProducer(queue);
    producer.send(session.createMessage());

    mockPeer.shutdown();
    connectionInterrupted.await(9, TimeUnit.SECONDS);

    try {
        session.commit();
        fail("Should not allow a commit while offline.");
    } catch (TransactionRolledBackException ex) {}

    connection.close();
}
 
开发者ID:apache,项目名称:qpid-jms,代码行数:22,代码来源:FailoverProviderOfflineBehaviorTest.java


示例3: testTransactionRollbackSucceeds

import javax.jms.TransactionRolledBackException; //导入依赖的package包/类
@Test(timeout=20000)
public void testTransactionRollbackSucceeds() throws Exception {
    connection = (JmsConnection) factory.createConnection();
    connection.addConnectionListener(new ConnectionInterruptionListener());
    connection.start();

    Session session = connection.createSession(true, Session.SESSION_TRANSACTED);
    Queue queue = session.createQueue(_testName.getMethodName());
    MessageProducer producer = session.createProducer(queue);
    producer.send(session.createMessage());

    mockPeer.shutdown();
    connectionInterrupted.await(9, TimeUnit.SECONDS);

    try {
        session.rollback();
    } catch (TransactionRolledBackException ex) {
        fail("Should allow a rollback while offline.");
    }

    connection.close();
}
 
开发者ID:apache,项目名称:qpid-jms,代码行数:23,代码来源:FailoverProviderOfflineBehaviorTest.java


示例4: convertToRuntimeException

import javax.jms.TransactionRolledBackException; //导入依赖的package包/类
public static JMSRuntimeException convertToRuntimeException(JMSException e) {
    if (e instanceof javax.jms.IllegalStateException) {
        return new IllegalStateRuntimeException(e.getMessage(), e.getErrorCode(), e);
    }
    if (e instanceof InvalidClientIDException) {
        return new InvalidClientIDRuntimeException(e.getMessage(), e.getErrorCode(), e);
    }
    if (e instanceof InvalidDestinationException) {
        return new InvalidDestinationRuntimeException(e.getMessage(), e.getErrorCode(), e);
    }
    if (e instanceof InvalidSelectorException) {
        return new InvalidSelectorRuntimeException(e.getMessage(), e.getErrorCode(), e);
    }
    if (e instanceof JMSSecurityException) {
        return new JMSSecurityRuntimeException(e.getMessage(), e.getErrorCode(), e);
    }
    if (e instanceof MessageFormatException) {
        return new MessageFormatRuntimeException(e.getMessage(), e.getErrorCode(), e);
    }
    if (e instanceof MessageNotWriteableException) {
        return new MessageNotWriteableRuntimeException(e.getMessage(), e.getErrorCode(), e);
    }
    if (e instanceof ResourceAllocationException) {
        return new ResourceAllocationRuntimeException(e.getMessage(), e.getErrorCode(), e);
    }
    if (e instanceof TransactionInProgressException) {
        return new TransactionInProgressRuntimeException(e.getMessage(), e.getErrorCode(), e);
    }
    if (e instanceof TransactionRolledBackException) {
        return new TransactionRolledBackRuntimeException(e.getMessage(), e.getErrorCode(), e);
    }
    return new JMSRuntimeException(e.getMessage(), e.getErrorCode(), e);
}
 
开发者ID:ops4j,项目名称:org.ops4j.pax.transx,代码行数:34,代码来源:Utils.java


示例5: rollback

import javax.jms.TransactionRolledBackException; //导入依赖的package包/类
/**
 * Rolls back any work done in this transaction and releases any locks
 * currently held.
 *
 * @throws JMSException if the JMS provider fails to roll back the
 *                 transaction due to some internal error.
 * @throws javax.jms.IllegalStateException if the method is not called by a
 *                 transacted session.
 */
public void rollback() throws JMSException {
    if (isInXATransaction()) {
        throw new TransactionInProgressException("Cannot rollback() if an XA transaction is already in progress ");
    }

    try {
        beforeEnd();
    } catch (TransactionRolledBackException canOcurrOnFailover) {
        LOG.warn("rollback processing error", canOcurrOnFailover);
    }
    if (transactionId != null) {
        if (LOG.isDebugEnabled()) {
            LOG.debug("Rollback: "  + transactionId
            + " syncCount: "
            + (synchronizations != null ? synchronizations.size() : 0));
        }

        TransactionInfo info = new TransactionInfo(getConnectionId(), transactionId, TransactionInfo.ROLLBACK);
        this.transactionId = null;
        //make this synchronous - see https://issues.apache.org/activemq/browse/AMQ-2364
        this.connection.syncSendPacket(info);
        // Notify the listener that the tx was rolled back
        if (localTransactionEventListener != null) {
            localTransactionEventListener.rollbackEvent();
        }
    }

    afterRollback();
}
 
开发者ID:DiamondLightSource,项目名称:daq-eclipse,代码行数:39,代码来源:TransactionContext.java


示例6: testAutoRollbackWithMissingRedeliveries

import javax.jms.TransactionRolledBackException; //导入依赖的package包/类
@Test
public void testAutoRollbackWithMissingRedeliveries() throws Exception {
   LOG.info(this + " running test testAutoRollbackWithMissingRedeliveries");
   broker = createBroker();
   broker.start();
   ActiveMQConnectionFactory cf = new ActiveMQConnectionFactory("failover:(" + url + ")");
   configureConnectionFactory(cf);
   Connection connection = cf.createConnection();
   try {
      connection.start();
      final Session producerSession = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
      final Queue destination = producerSession.createQueue(QUEUE_NAME + "?consumer.prefetchSize=1");
      final Session consumerSession = connection.createSession(true, Session.SESSION_TRANSACTED);
      MessageConsumer consumer = consumerSession.createConsumer(destination);

      produceMessage(producerSession, destination);

      Message msg = consumer.receive(20000);
      Assert.assertNotNull(msg);

      broker.stop();
      broker = createBroker();
      // use empty jdbc store so that default wait(0) for redeliveries will timeout after failover
      broker.start();

      try {
         consumerSession.commit();
         Assert.fail("expected transaction rolledback ex");
      } catch (TransactionRolledBackException expected) {
      }

      broker.stop();
      broker = createBroker();
      broker.start();

      Assert.assertNotNull("should get rolledback message from original restarted broker", consumer.receive(20000));
   } finally {
      connection.close();
   }
}
 
开发者ID:apache,项目名称:activemq-artemis,代码行数:41,代码来源:FailoverTransactionTest.java


示例7: convertToRuntimeException

import javax.jms.TransactionRolledBackException; //导入依赖的package包/类
/**
 * Converts instances of sub-classes of {@link JMSException} into the corresponding sub-class of
 * {@link JMSRuntimeException}.
 *
 * @param e
 * @return
 */
public static JMSRuntimeException convertToRuntimeException(JMSException e) {
   if (e instanceof javax.jms.IllegalStateException) {
      return new IllegalStateRuntimeException(e.getMessage(), e.getErrorCode(), e);
   }
   if (e instanceof InvalidClientIDException) {
      return new InvalidClientIDRuntimeException(e.getMessage(), e.getErrorCode(), e);
   }
   if (e instanceof InvalidDestinationException) {
      return new InvalidDestinationRuntimeException(e.getMessage(), e.getErrorCode(), e);
   }
   if (e instanceof InvalidSelectorException) {
      return new InvalidSelectorRuntimeException(e.getMessage(), e.getErrorCode(), e);
   }
   if (e instanceof JMSSecurityException) {
      return new JMSSecurityRuntimeException(e.getMessage(), e.getErrorCode(), e);
   }
   if (e instanceof MessageFormatException) {
      return new MessageFormatRuntimeException(e.getMessage(), e.getErrorCode(), e);
   }
   if (e instanceof MessageNotWriteableException) {
      return new MessageNotWriteableRuntimeException(e.getMessage(), e.getErrorCode(), e);
   }
   if (e instanceof ResourceAllocationException) {
      return new ResourceAllocationRuntimeException(e.getMessage(), e.getErrorCode(), e);
   }
   if (e instanceof TransactionInProgressException) {
      return new TransactionInProgressRuntimeException(e.getMessage(), e.getErrorCode(), e);
   }
   if (e instanceof TransactionRolledBackException) {
      return new TransactionRolledBackRuntimeException(e.getMessage(), e.getErrorCode(), e);
   }
   return new JMSRuntimeException(e.getMessage(), e.getErrorCode(), e);
}
 
开发者ID:apache,项目名称:activemq-artemis,代码行数:41,代码来源:JmsExceptionUtils.java


示例8: commit

import javax.jms.TransactionRolledBackException; //导入依赖的package包/类
@Override
public void commit(final JmsTransactionInfo transactionInfo, JmsTransactionInfo nextTransactionInfo, AsyncResult request) throws IOException, JMSException, UnsupportedOperationException {
    checkClosed();
    final FailoverRequest pending = new FailoverRequest(request, requestTimeout) {
        @Override
        public void doTask() throws Exception {
            provider.commit(transactionInfo, nextTransactionInfo, this);
        }

        @Override
        public boolean failureWhenOffline() {
            return true;
        }

        @Override
        public String toString() {
            return "TX commit -> " + transactionInfo.getId();
        }

        @Override
        protected Exception createOfflineFailureException(IOException error) {
            Exception ex = new TransactionRolledBackException("Commit failed, connection offline: " + error.getMessage());
            ex.initCause(error);
            return ex;
        }
    };

    serializer.execute(pending);
}
 
开发者ID:apache,项目名称:qpid-jms,代码行数:30,代码来源:FailoverProvider.java


示例9: commit

import javax.jms.TransactionRolledBackException; //导入依赖的package包/类
public void commit(final JmsTransactionInfo transactionInfo, JmsTransactionInfo nextTransactionInfo, final AsyncResult request) throws Exception {
    if (!transactionInfo.getId().equals(current)) {
        if (!transactionInfo.isInDoubt() && current == null) {
            throw new IllegalStateException("Commit called with no active Transaction.");
        } else if (!transactionInfo.isInDoubt() && current != null) {
            throw new IllegalStateException("Attempt to Commit a transaction other than the current one");
        } else {
            throw new TransactionRolledBackException("Transaction in doubt and cannot be committed.");
        }
    }

    preCommit();

    LOG.trace("TX Context[{}] committing current TX[[]]", this, current);

    DischargeCompletion completion = new DischargeCompletion(request, nextTransactionInfo, true);

    coordinator.discharge(current, completion);
    current = null;

    if (completion.isPipelined()) {
        // If the discharge completed abnormally then we don't bother creating a new TX as the
        // caller will determine how to recover.
        if (!completion.isComplete()) {
            begin(nextTransactionInfo.getId(), completion.getDeclareCompletion());
        } else {
            completion.getDeclareCompletion().onFailure(completion.getFailureCause());
        }
    }
}
 
开发者ID:apache,项目名称:qpid-jms,代码行数:31,代码来源:AmqpTransactionContext.java


示例10: testRollbackErrorCoordinatorClosedOnCommit

import javax.jms.TransactionRolledBackException; //导入依赖的package包/类
@Test(timeout=20000)
public void testRollbackErrorCoordinatorClosedOnCommit() throws Exception {
    try (TestAmqpPeer testPeer = new TestAmqpPeer();) {
        Connection connection = testFixture.establishConnecton(testPeer);
        connection.start();

        testPeer.expectBegin();
        testPeer.expectCoordinatorAttach();

        Binary txnId1 = new Binary(new byte[]{ (byte) 5, (byte) 6, (byte) 7, (byte) 8});
        Binary txnId2 = new Binary(new byte[]{ (byte) 1, (byte) 2, (byte) 3, (byte) 4});

        testPeer.expectDeclare(txnId1);
        testPeer.remotelyCloseLastCoordinatorLinkOnDischarge(txnId1, false, true, txnId2);
        testPeer.expectCoordinatorAttach();
        testPeer.expectDeclare(txnId2);
        testPeer.expectDischarge(txnId2, true);

        Session session = connection.createSession(true, Session.SESSION_TRANSACTED);

        try {
            session.commit();
            fail("Transaction should have rolled back");
        } catch (TransactionRolledBackException ex) {
            LOG.info("Caught expected TransactionRolledBackException");
        }

        testPeer.expectClose();
        connection.close();

        testPeer.waitForAllHandlersToComplete(1000);
    }
}
 
开发者ID:apache,项目名称:qpid-jms,代码行数:34,代码来源:TransactionsIntegrationTest.java


示例11: testRollbackErrorWhenCoordinatorRemotelyClosed

import javax.jms.TransactionRolledBackException; //导入依赖的package包/类
@Test(timeout=20000)
public void testRollbackErrorWhenCoordinatorRemotelyClosed() throws Exception {
    try (TestAmqpPeer testPeer = new TestAmqpPeer();) {
        Connection connection = testFixture.establishConnecton(testPeer);
        connection.start();

        testPeer.expectBegin();
        testPeer.expectCoordinatorAttach();

        Binary txnId = new Binary(new byte[]{ (byte) 5, (byte) 6, (byte) 7, (byte) 8});
        testPeer.expectDeclare(txnId);
        testPeer.remotelyCloseLastCoordinatorLink();

        Session session = connection.createSession(true, Session.SESSION_TRANSACTED);

        testPeer.waitForAllHandlersToComplete(2000);

        testPeer.expectCoordinatorAttach();
        testPeer.expectDeclare(txnId);

        testPeer.expectDischarge(txnId, true);

        try {
            session.commit();
            fail("Transaction should have rolled back");
        } catch (TransactionRolledBackException ex) {
            LOG.info("Caught expected TransactionRolledBackException");
        }

        testPeer.expectClose();
        connection.close();

        testPeer.waitForAllHandlersToComplete(1000);
    }
}
 
开发者ID:apache,项目名称:qpid-jms,代码行数:36,代码来源:TransactionsIntegrationTest.java


示例12: testTxConsumerReceiveThenFailoverCommitFails

import javax.jms.TransactionRolledBackException; //导入依赖的package包/类
@Test(timeout=60000)
public void testTxConsumerReceiveThenFailoverCommitFails() throws Exception {
    URI brokerURI = new URI(getAmqpFailoverURI());

    connection = createAmqpConnection(brokerURI);
    connection.start();

    final int MSG_COUNT = 5;
    final Session session = connection.createSession(true, Session.SESSION_TRANSACTED);
    Queue queue = session.createQueue(name.getMethodName());
    final MessageConsumer consumer = session.createConsumer(queue);

    sendMessages(connection, queue, MSG_COUNT);
    QueueViewMBean proxy = getProxyToQueue(name.getMethodName());
    assertEquals(MSG_COUNT, proxy.getQueueSize());

    for (int i = 0; i < MSG_COUNT; ++i) {
        Message received = consumer.receive(3000);
        assertNotNull("Mesage was not expected but not received", received);
    }

    stopPrimaryBroker();
    restartPrimaryBroker();

    proxy = getProxyToQueue(name.getMethodName());
    assertEquals(MSG_COUNT, proxy.getQueueSize());

    try {
        LOG.info("Session commit firing after connection failed.");
        session.commit();
        fail("Session commit should have failed with TX rolled back.");
    } catch (TransactionRolledBackException rb) {
        LOG.info("Transacted commit failed after failover: {}", rb.getMessage());
    }

    assertEquals(MSG_COUNT, proxy.getQueueSize());
}
 
开发者ID:apache,项目名称:qpid-jms,代码行数:38,代码来源:JmsTxConsumerFailoverTest.java


示例13: testTxProducerSendsThenFailoverCommitFails

import javax.jms.TransactionRolledBackException; //导入依赖的package包/类
@Test(timeout=60000)
public void testTxProducerSendsThenFailoverCommitFails() throws Exception {
    URI brokerURI = new URI(getAmqpFailoverURI());

    connection = createAmqpConnection(brokerURI);
    connection.start();

    final int MSG_COUNT = 5;
    final Session session = connection.createSession(true, Session.SESSION_TRANSACTED);
    Queue queue = session.createQueue(name.getMethodName());
    final MessageProducer producer = session.createProducer(queue);
    producer.setDeliveryMode(DeliveryMode.PERSISTENT);

    QueueViewMBean proxy = getProxyToQueue(name.getMethodName());
    assertEquals(0, proxy.getQueueSize());

    for (int i = 0; i < MSG_COUNT; ++i) {
        LOG.debug("Producer sening message #{}", i + 1);
        producer.send(session.createTextMessage("Message: " + i));
    }

    assertEquals(0, proxy.getQueueSize());

    stopPrimaryBroker();
    restartPrimaryBroker();

    proxy = getProxyToQueue(name.getMethodName());
    assertEquals(0, proxy.getQueueSize());

    try {
        session.commit();
        fail("Session commit should have failed with TX rolled back.");
    } catch (TransactionRolledBackException rb) {
        LOG.info("Transacted commit failed after failover: {}", rb.getMessage());
    }

    assertEquals(0, proxy.getQueueSize());
}
 
开发者ID:apache,项目名称:qpid-jms,代码行数:39,代码来源:JmsTxProducerFailoverTest.java


示例14: toRuntimeException

import javax.jms.TransactionRolledBackException; //导入依赖的package包/类
public static JMSRuntimeException toRuntimeException(final JMSException e) {
    if (e instanceof javax.jms.IllegalStateException) {
        return new IllegalStateRuntimeException(e.getMessage(), e.getErrorCode(), e);
    }
    if (e instanceof InvalidClientIDException) {
        return new InvalidClientIDRuntimeException(e.getMessage(), e.getErrorCode(), e);
    }
    if (e instanceof InvalidDestinationException) {
        return new InvalidDestinationRuntimeException(e.getMessage(), e.getErrorCode(), e);
    }
    if (e instanceof InvalidSelectorException) {
        return new InvalidSelectorRuntimeException(e.getMessage(), e.getErrorCode(), e);
    }
    if (e instanceof JMSSecurityException) {
        return new JMSSecurityRuntimeException(e.getMessage(), e.getErrorCode(), e);
    }
    if (e instanceof MessageFormatException) {
        return new MessageFormatRuntimeException(e.getMessage(), e.getErrorCode(), e);
    }
    if (e instanceof MessageNotWriteableException) {
        return new MessageNotWriteableRuntimeException(e.getMessage(), e.getErrorCode(), e);
    }
    if (e instanceof ResourceAllocationException) {
        return new ResourceAllocationRuntimeException(e.getMessage(), e.getErrorCode(), e);
    }
    if (e instanceof TransactionInProgressException) {
        return new TransactionInProgressRuntimeException(e.getMessage(), e.getErrorCode(), e);
    }
    if (e instanceof TransactionRolledBackException) {
        return new TransactionRolledBackRuntimeException(e.getMessage(), e.getErrorCode(), e);
    }
    return new JMSRuntimeException(e.getMessage(), e.getErrorCode(), e);
}
 
开发者ID:apache,项目名称:tomee,代码行数:34,代码来源:JMS2.java


示例15: testConvertsTransactionRolledBackExceptionToTransactionRolledBackRuntimeException

import javax.jms.TransactionRolledBackException; //导入依赖的package包/类
@Test(expected = TransactionRolledBackRuntimeException.class)
public void testConvertsTransactionRolledBackExceptionToTransactionRolledBackRuntimeException() {
    throw JMSExceptionSupport.createRuntimeException(new TransactionRolledBackException("error"));
}
 
开发者ID:messaginghub,项目名称:pooled-jms,代码行数:5,代码来源:JMSExceptionSupportTest.java


示例16: main

import javax.jms.TransactionRolledBackException; //导入依赖的package包/类
public static void main(final String[] args) throws Exception {
   final int numMessages = 10;

   Connection connection = null;

   InitialContext initialContext = null;

   try {
      server0 = ServerUtil.startServer(args[0], TransactionFailoverExample.class.getSimpleName() + "0", 0, 5000);
      server1 = ServerUtil.startServer(args[1], TransactionFailoverExample.class.getSimpleName() + "1", 1, 5000);

      // Step 1. Get an initial context for looking up JNDI from the server #1
      initialContext = new InitialContext();

      // Step 2. Look-up the JMS resources from JNDI
      Queue queue = (Queue) initialContext.lookup("queue/exampleQueue");
      ConnectionFactory connectionFactory = (ConnectionFactory) initialContext.lookup("ConnectionFactory");

      // Step 3. We create a JMS Connection
      connection = connectionFactory.createConnection();

      // Step 4. We create a *transacted* JMS Session
      Session session = connection.createSession(true, 0);

      // Step 5. We start the connection to ensure delivery occurs
      connection.start();

      // Step 6. We create a JMS MessageProducer
      MessageProducer producer = session.createProducer(queue);

      // Step 7. We create a JMS MessageConsumer
      MessageConsumer consumer = session.createConsumer(queue);

      // Step 8. We send half of the messages, kill the live server and send the remaining messages
      sendMessages(session, producer, numMessages, true);

      // Step 9. As failover occurred during transaction, the session has been marked for rollback only
      try {
         session.commit();
      } catch (TransactionRolledBackException e) {
         System.err.println("transaction has been rolled back: " + e.getMessage());
      }

      // Step 10. We resend all the messages
      sendMessages(session, producer, numMessages, false);

      // Step 11. We commit the session successfully: the messages will be all delivered to the activated backup
      // server
      session.commit();

      // Step 12. We are now transparently reconnected to server #0, the backup server.
      // We consume the messages sent before the crash of the live server and commit the session.
      for (int i = 0; i < numMessages; i++) {
         TextMessage message0 = (TextMessage) consumer.receive(5000);

         if (message0 == null) {
            throw new IllegalStateException("Example failed - message wasn't received");
         }

         System.out.println("Got message: " + message0.getText());
      }

      session.commit();

      System.out.println("Other message on the server? " + consumer.receive(5000));
   } finally {
      // Step 13. Be sure to close our resources!

      if (connection != null) {
         connection.close();
      }

      if (initialContext != null) {
         initialContext.close();
      }

      ServerUtil.killServer(server0);
      ServerUtil.killServer(server1);
   }
}
 
开发者ID:apache,项目名称:activemq-artemis,代码行数:81,代码来源:TransactionFailoverExample.java


示例17: main

import javax.jms.TransactionRolledBackException; //导入依赖的package包/类
public static void main(final String[] args) throws Exception {
   final int numMessages = 10;

   Connection connection = null;

   InitialContext initialContext = null;

   try {
      server0 = ServerUtil.startServer(args[0], ReplicatedTransactionFailoverExample.class.getSimpleName() + "0", 0, 5000);
      server1 = ServerUtil.startServer(args[1], ReplicatedTransactionFailoverExample.class.getSimpleName() + "1", 1, 5000);

      // Step 1. Get an initial context for looking up JNDI from the server #1
      initialContext = new InitialContext();

      // Step 2. Look-up the JMS resources from JNDI
      Queue queue = (Queue) initialContext.lookup("queue/exampleQueue");
      ConnectionFactory connectionFactory = (ConnectionFactory) initialContext.lookup("ConnectionFactory");

      // Step 3. We create a JMS Connection
      connection = connectionFactory.createConnection();

      // Step 4. We create a *transacted* JMS Session
      Session session = connection.createSession(true, 0);

      // Step 5. We start the connection to ensure delivery occurs
      connection.start();

      // Step 6. We create a JMS MessageProducer
      MessageProducer producer = session.createProducer(queue);

      // Step 7. We create a JMS MessageConsumer
      MessageConsumer consumer = session.createConsumer(queue);

      // Step 8. We send half of the messages, kill the live server and send the remaining messages
      sendMessages(session, producer, numMessages, true);

      // Step 9. As failover occurred during transaction, the session has been marked for rollback only
      try {
         session.commit();
      } catch (TransactionRolledBackException e) {
         System.err.println("transaction has been rolled back: " + e.getMessage());
      }

      // Step 10. We resend all the messages
      sendMessages(session, producer, numMessages, false);

      // Step 11. We commit the session successfully: the messages will be all delivered to the activated backup
      // server
      session.commit();

      // Step 12. We are now transparently reconnected to server #0, the backup server.
      // We consume the messages sent before the crash of the live server and commit the session.
      for (int i = 0; i < numMessages; i++) {
         TextMessage message0 = (TextMessage) consumer.receive(5000);

         if (message0 == null) {
            throw new IllegalStateException("Example failed - message wasn't received");
         }

         System.out.println("Got message: " + message0.getText());
      }

      session.commit();

      System.out.println("Other message on the server? " + consumer.receive(5000));
   } finally {
      // Step 13. Be sure to close our resources!

      if (connection != null) {
         connection.close();
      }

      if (initialContext != null) {
         initialContext.close();
      }

      ServerUtil.killServer(server0);
      ServerUtil.killServer(server1);
   }
}
 
开发者ID:apache,项目名称:activemq-artemis,代码行数:81,代码来源:ReplicatedTransactionFailoverExample.java


示例18: testSyncBeforeEndCalledOnceOnRollback

import javax.jms.TransactionRolledBackException; //导入依赖的package包/类
@Test
public void testSyncBeforeEndCalledOnceOnRollback() throws Exception {
   final AtomicInteger beforeEndCountA = new AtomicInteger(0);
   final AtomicInteger beforeEndCountB = new AtomicInteger(0);
   final AtomicInteger rollbackCountA = new AtomicInteger(0);
   final AtomicInteger rollbackCountB = new AtomicInteger(0);
   underTest.addSynchronization(new Synchronization() {
      @Override
      public void beforeEnd() throws Exception {
         if (beforeEndCountA.getAndIncrement() == 0) {
            throw new TransactionRolledBackException("force rollback");
         }
      }

      @Override
      public void afterCommit() throws Exception {
         fail("expected rollback exception");
      }

      @Override
      public void afterRollback() throws Exception {
         rollbackCountA.incrementAndGet();
      }

   });

   underTest.addSynchronization(new Synchronization() {
      @Override
      public void beforeEnd() throws Exception {
         beforeEndCountB.getAndIncrement();
      }

      @Override
      public void afterCommit() throws Exception {
         fail("expected rollback exception");
      }

      @Override
      public void afterRollback() throws Exception {
         rollbackCountB.incrementAndGet();
      }

   });

   try {
      underTest.commit();
      fail("expected rollback exception");
   } catch (TransactionRolledBackException expected) {
   }

   assertEquals("beforeEnd A called once", 1, beforeEndCountA.get());
   assertEquals("beforeEnd B called once", 1, beforeEndCountA.get());
   assertEquals("rollbackCount B 0", 1, rollbackCountB.get());
   assertEquals("rollbackCount A B", rollbackCountA.get(), rollbackCountB.get());
}
 
开发者ID:apache,项目名称:activemq-artemis,代码行数:56,代码来源:TransactionContextTest.java


示例19: consumeMessage

import javax.jms.TransactionRolledBackException; //导入依赖的package包/类
@Override
protected void consumeMessage(Message message, List<Message> messageList) {
   try {
      receiveSession.commit();
      super.consumeMessage(message, messageList);
   } catch (JMSException e) {
      LOG.info("Failed to commit message receipt: " + message, e);
      try {
         receiveSession.rollback();
      } catch (JMSException ignored) {
      }

      if (e instanceof TransactionRolledBackException) {
         TransactionRolledBackException transactionRolledBackException = (TransactionRolledBackException) e;
         if (transactionRolledBackException.getMessage().indexOf("in doubt") != -1) {
            // failover chucked bc there is a missing reply to a commit.
            // failover is involved b/c the store exception is handled broker side and the client just
            // sees a disconnect (socket.close()).
            // If the client needs to be aware of the failure then it should not use IOExceptionHandler
            // so that the exception will propagate back

            // for this test case:
            // the commit may have got there and the reply is lost "or" the commit may be lost.
            // so we may or may not get a resend.
            //
            // At the application level we need to determine if the message is there or not which is not trivial
            // for this test we assert received == sent
            // so we need to know whether the message will be replayed.
            // we can ask the store b/c we know it is jdbc - guess we could go through a destination
            // message store interface also or use jmx
            java.sql.Connection dbConnection = null;
            try {
               ActiveMQMessage mqMessage = (ActiveMQMessage) message;
               MessageId id = mqMessage.getMessageId();
               dbConnection = sharedDs.getConnection();
               PreparedStatement s = dbConnection.prepareStatement(((JDBCPersistenceAdapter) connectedToBroker().getPersistenceAdapter()).getStatements().getFindMessageStatement());
               s.setString(1, id.getProducerId().toString());
               s.setLong(2, id.getProducerSequenceId());
               ResultSet rs = s.executeQuery();

               if (!rs.next()) {
                  // message is gone, so lets count it as consumed
                  LOG.info("On TransactionRolledBackException we know that the ack/commit got there b/c message is gone so we count it: " + mqMessage);
                  super.consumeMessage(message, messageList);
               } else {
                  LOG.info("On TransactionRolledBackException we know that the ack/commit was lost so we expect a replay of: " + mqMessage);
               }
            } catch (Exception dbe) {
               dbe.printStackTrace();
            } finally {
               try {
                  dbConnection.close();
               } catch (SQLException e1) {
                  e1.printStackTrace();
               }
            }
         }
      }
   }
}
 
开发者ID:apache,项目名称:activemq-artemis,代码行数:61,代码来源:DbRestartJDBCQueueMasterSlaveTest.java


示例20: testFailoverCommitReplyLost

import javax.jms.TransactionRolledBackException; //导入依赖的package包/类
@Test
@BMRules(
   rules = {@BMRule(
      name = "set no return response and stop the broker",
      targetClass = "org.apache.activemq.artemis.core.protocol.openwire.OpenWireConnection$CommandProcessor",
      targetMethod = "processCommitTransactionOnePhase",
      targetLocation = "EXIT",
      action = "org.apache.activemq.transport.failover.FailoverTransactionTest.holdResponseAndStopBroker($0)")})
public void testFailoverCommitReplyLost() throws Exception {
   LOG.info(this + " running test testFailoverCommitReplyLost");

   broker = createBroker();
   startBrokerWithDurableQueue();
   doByteman.set(true);

   ActiveMQConnectionFactory cf = new ActiveMQConnectionFactory("failover:(" + url + ")");
   configureConnectionFactory(cf);
   Connection connection = cf.createConnection();
   connection.start();
   final Session session = connection.createSession(true, Session.AUTO_ACKNOWLEDGE);
   Queue destination = session.createQueue(QUEUE_NAME);

   MessageConsumer consumer = session.createConsumer(destination);
   produceMessage(session, destination);

   final CountDownLatch commitDoneLatch = new CountDownLatch(1);
   // broker will die on commit reply so this will hang till restart
   new Thread() {
      @Override
      public void run() {
         LOG.info("doing async commit...");
         try {
            session.commit();
         } catch (JMSException e) {
            Assert.assertTrue(e instanceof TransactionRolledBackException);
            LOG.info("got commit exception: ", e);
         }
         commitDoneLatch.countDown();
         LOG.info("done async commit");
      }
   }.start();

   // will be stopped by the plugin
   brokerStopLatch.await(60, TimeUnit.SECONDS);
   doByteman.set(false);
   broker = createBroker();
   broker.start();

   Assert.assertTrue("tx committed through failover", commitDoneLatch.await(30, TimeUnit.SECONDS));

   // new transaction
   Message msg = consumer.receive(20000);
   LOG.info("Received: " + msg);
   Assert.assertNotNull("we got the message", msg);
   Assert.assertNull("we got just one message", consumer.receive(2000));
   session.commit();
   consumer.close();
   connection.close();

   // ensure no dangling messages with fresh broker etc
   broker.stop();

   LOG.info("Checking for remaining/hung messages..");
   broker = createBroker();
   broker.start();

   // after restart, ensure no dangling messages
   cf = new ActiveMQConnectionFactory("failover:(" + url + ")");
   configureConnectionFactory(cf);
   connection = cf.createConnection();
   connection.start();
   Session session2 = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
   consumer = session2.createConsumer(destination);
   msg = consumer.receive(1000);
   if (msg == null) {
      msg = consumer.receive(5000);
   }
   LOG.info("Received: " + msg);
   Assert.assertNull("no messges left dangling but got: " + msg, msg);
   connection.close();
}
 
开发者ID:apache,项目名称:activemq-artemis,代码行数:82,代码来源:FailoverTransactionTest.java



注:本文中的javax.jms.TransactionRolledBackException类示例整理自Github/MSDocs等源码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。


鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
上一篇:
Java CGraphicsConfig类代码示例发布时间:2022-05-22
下一篇:
Java AdaptiveRecvByteBufAllocator类代码示例发布时间:2022-05-22
热门推荐
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap