Example usage for com.rabbitmq.client Channel txRollback

List of usage examples for com.rabbitmq.client Channel txRollback

Introduction

In this page you can find the example usage for com.rabbitmq.client Channel txRollback.

Prototype

Tx.RollbackOk txRollback() throws IOException;

Source Link

Document

Rolls back a TX transaction on this channel.

Usage

From source file:org.axonframework.amqp.eventhandling.spring.SpringAMQPPublisher.java

License:Apache License

/**
 * Sends the given {@code events} to the configured AMQP Exchange. It takes the current Unit of Work into account
 * when available. Otherwise, it simply publishes directly.
 *
 * @param events the events to publish on the AMQP Message Broker
 *///from ww w.ja  v  a 2s.c  om
protected void send(List<? extends EventMessage<?>> events) {
    Channel channel = connectionFactory.createConnection().createChannel(isTransactional);
    try {
        if (isTransactional) {
            channel.txSelect();
        } else if (waitForAck) {
            channel.confirmSelect();
        }
        for (EventMessage event : events) {
            AMQPMessage amqpMessage = messageConverter.createAMQPMessage(event);
            doSendMessage(channel, amqpMessage);
        }
        if (CurrentUnitOfWork.isStarted()) {
            UnitOfWork<?> unitOfWork = CurrentUnitOfWork.get();
            unitOfWork.onCommit(u -> {
                if ((isTransactional || waitForAck) && !channel.isOpen()) {
                    throw new EventPublicationFailedException(
                            "Unable to Commit UnitOfWork changes to AMQP: Channel is closed.",
                            channel.getCloseReason());
                }
            });
            unitOfWork.afterCommit(u -> {
                try {
                    if (isTransactional) {
                        channel.txCommit();
                    } else if (waitForAck) {
                        try {
                            channel.waitForConfirmsOrDie(publisherAckTimeout);
                        } catch (IOException ex) {
                            throw new EventPublicationFailedException(
                                    "Failed to receive acknowledgements for all events", ex);
                        } catch (TimeoutException ex) {
                            throw new EventPublicationFailedException(
                                    "Timeout while waiting for publisher acknowledgements", ex);
                        }
                    }
                } catch (IOException e) {
                    logger.warn("Unable to commit transaction on channel.", e);
                } catch (InterruptedException e) {
                    logger.warn("Interrupt received when waiting for message confirms.");
                    Thread.currentThread().interrupt();
                }
                tryClose(channel);
            });
            unitOfWork.onRollback(u -> {
                try {
                    if (isTransactional) {
                        channel.txRollback();
                    }
                } catch (IOException ex) {
                    logger.warn("Unable to rollback transaction on channel.", ex);
                }
                tryClose(channel);
            });
        } else if (isTransactional) {
            channel.txCommit();
        } else if (waitForAck) {
            channel.waitForConfirmsOrDie();
        }
    } catch (IOException e) {
        if (isTransactional) {
            tryRollback(channel);
        }
        throw new EventPublicationFailedException("Failed to dispatch Events to the Message Broker.", e);
    } catch (ShutdownSignalException e) {
        throw new EventPublicationFailedException("Failed to dispatch Events to the Message Broker.", e);
    } catch (InterruptedException e) {
        logger.warn("Interrupt received when waiting for message confirms.");
        Thread.currentThread().interrupt();
    } finally {
        if (!CurrentUnitOfWork.isStarted()) {
            tryClose(channel);
        }
    }
}

From source file:org.axonframework.amqp.eventhandling.spring.SpringAMQPPublisher.java

License:Apache License

private void tryRollback(Channel channel) {
    try {/*from  w  ww .ja v  a 2s  .co m*/
        channel.txRollback();
    } catch (IOException e) {
        logger.debug("Unable to rollback. The underlying channel might already be closed.", e);
    }
}

From source file:org.axonframework.eventhandling.amqp.spring.SpringAMQPEventBus.java

License:Apache License

@Override
protected void prepareCommit(List<? extends EventMessage<?>> events) {
    Channel channel = connectionFactory.createConnection().createChannel(isTransactional);
    try {/*from  ww w  .jav  a  2  s  .  c o  m*/
        if (isTransactional) {
            channel.txSelect();
        } else if (waitForAck) {
            channel.confirmSelect();
        }
        for (EventMessage event : events) {
            AMQPMessage amqpMessage = messageConverter.createAMQPMessage(event);
            doSendMessage(channel, amqpMessage);
        }
        if (CurrentUnitOfWork.isStarted()) {
            CurrentUnitOfWork.get().onCommit(u -> {
                if ((isTransactional || waitForAck) && !channel.isOpen()) {
                    throw new EventPublicationFailedException(
                            "Unable to Commit UnitOfWork changes to AMQP: Channel is closed.",
                            channel.getCloseReason());
                }
            });
            CurrentUnitOfWork.get().afterCommit(u -> {
                try {
                    if (isTransactional) {
                        channel.txCommit();
                    } else if (waitForAck) {
                        try {
                            channel.waitForConfirmsOrDie(publisherAckTimeout);
                        } catch (IOException ex) {
                            throw new EventPublicationFailedException(
                                    "Failed to receive acknowledgements for all events", ex);
                        } catch (TimeoutException ex) {
                            throw new EventPublicationFailedException(
                                    "Timeout while waiting for publisher acknowledgements", ex);
                        }
                    }
                } catch (IOException e) {
                    logger.warn("Unable to commit transaction on channel.", e);
                } catch (InterruptedException e) {
                    logger.warn("Interrupt received when waiting for message confirms.");
                    Thread.currentThread().interrupt();
                }
                tryClose(channel);
            });
            CurrentUnitOfWork.get().onRollback(u -> {
                try {
                    if (isTransactional) {
                        channel.txRollback();
                    }
                } catch (IOException ex) {
                    logger.warn("Unable to rollback transaction on channel.", ex);
                }
                tryClose(channel);
            });
        } else if (isTransactional) {
            channel.txCommit();
        } else if (waitForAck) {
            channel.waitForConfirmsOrDie();
        }
    } catch (IOException e) {
        if (isTransactional) {
            tryRollback(channel);
        }
        throw new EventPublicationFailedException("Failed to dispatch Events to the Message Broker.", e);
    } catch (ShutdownSignalException e) {
        throw new EventPublicationFailedException("Failed to dispatch Events to the Message Broker.", e);
    } catch (InterruptedException e) {
        logger.warn("Interrupt received when waiting for message confirms.");
        Thread.currentThread().interrupt();
    } finally {
        if (!CurrentUnitOfWork.isStarted()) {
            tryClose(channel);
        }
    }
}

From source file:org.mule.transport.amqp.AmqpTransaction.java

License:Open Source License

@Override
protected void doRollback() throws TransactionException {
    if (resource == null) {
        logger.warn(CoreMessages.rollbackTxButNoResource(this));
        return;//from   w  w w  .  j  a  v  a2  s  .c o  m
    }

    final Channel channel = getTransactedChannel();

    try {
        channel.txRollback();
    } catch (final IOException ioe) {
        throw new TransactionException(CoreMessages.transactionRollbackFailed(), ioe);
    }

    try {
        channel.txRollback();
        switch (recoverStrategy) {
        case NONE:
            // NOOP
            break;
        case NO_REQUEUE:
            channel.basicRecover(false);
            break;
        case REQUEUE:
            channel.basicRecover(true);
            break;
        }
    } catch (final IOException ioe) {
        logger.warn("Failed to recover channel " + channel + " after rollback (recoverStrategy is "
                + recoverStrategy + ")");
    }
}

From source file:org.mule.transport.amqp.internal.transaction.AmqpTransaction.java

License:Open Source License

@Override
protected void doRollback() throws TransactionException {
    if (resource == null) {
        logger.warn(CoreMessages.rollbackTxButNoResource(this));
        return;/*www .j  a  va2  s.  c  om*/
    }

    final Channel channel = getTransactedChannel();

    try {
        try {
            channel.txRollback();

            if (logger.isDebugEnabled()) {
                logger.debug("Rolled back AMQP transaction (" + recoverStrategy + ") on channel: " + channel);
            }
        } catch (final IOException ioe) {
            throw new TransactionException(CoreMessages.transactionRollbackFailed(), ioe);
        }

        applyRecoverStrategy(channel);
    } finally {
        closeChannelIfNeeded(channel);
    }
}

From source file:org.springframework.amqp.rabbit.connection.CachingConnectionFactoryIntegrationTests.java

License:Apache License

@Test
public void testMixTransactionalAndNonTransactional() throws Exception {

    RabbitTemplate template1 = new RabbitTemplate(connectionFactory);
    RabbitTemplate template2 = new RabbitTemplate(connectionFactory);
    template1.setChannelTransacted(true);

    RabbitAdmin admin = new RabbitAdmin(connectionFactory);
    Queue queue = admin.declareQueue();

    template1.convertAndSend(queue.getName(), "message");
    String result = (String) template2.receiveAndConvert(queue.getName());
    assertEquals("message", result);

    // The channel is not transactional
    exception.expect(AmqpIOException.class);

    template2.execute(new ChannelCallback<Void>() {
        @Override//  w w w .  j a  v a2s  .c  om
        public Void doInRabbit(Channel channel) throws Exception {
            // Should be an exception because the channel is not transactional
            channel.txRollback();
            return null;
        }
    });

}

From source file:org.springframework.amqp.rabbit.connection.RabbitUtils.java

License:Apache License

public static void rollbackIfNecessary(Channel channel) {
    Assert.notNull(channel, "Channel must not be null");
    try {/*from   w  w  w .j a v  a  2  s.  co m*/
        channel.txRollback();
    } catch (IOException ex) {
        throw new AmqpIOException(ex);
    }
}