List of usage examples for com.rabbitmq.client Channel txCommit
Tx.CommitOk txCommit() throws IOException;
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 w w w . j a v a 2s .co 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.axonframework.eventhandling.amqp.spring.SpringAMQPTerminal.java
License:Apache License
@Override public void publish(EventMessage... events) { final Channel channel = connectionFactory.createConnection().createChannel(isTransactional); try {/*from w w w. ja v a 2 s.c o m*/ if (waitForAck) { channel.confirmSelect(); } for (EventMessage event : events) { AMQPMessage amqpMessage = messageConverter.createAMQPMessage(event); doSendMessage(channel, amqpMessage); } if (CurrentUnitOfWork.isStarted()) { CurrentUnitOfWork.get().registerListener(new ChannelTransactionUnitOfWorkListener(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.internal.transaction.AmqpTransaction.java
License:Open Source License
@Override protected void doCommit() throws TransactionException { if (resource == null) { logger.warn(CoreMessages.commitTxButNoResource(this)); return;/*from w w w. j a va 2 s . co m*/ } final Channel channel = getTransactedChannel(); try { channel.txCommit(); if (logger.isDebugEnabled()) { logger.debug("Committed AMQP transaction on channel: " + channel); } } catch (final IOException ioe) { throw new TransactionException(CoreMessages.transactionCommitFailed(), ioe); } finally { closeChannelIfNeeded(channel); } }
From source file:org.springframework.amqp.rabbit.connection.RabbitResourceHolder.java
License:Apache License
public void commitAll() throws AmqpException { try {/* w w w .j a v a 2s . c o m*/ for (Channel channel : this.channels) { if (deliveryTags.containsKey(channel)) { for (Long deliveryTag : deliveryTags.get(channel)) { channel.basicAck(deliveryTag, false); } } channel.txCommit(); } } catch (IOException e) { throw new AmqpException("failed to commit RabbitMQ transaction", e); } }
From source file:org.springframework.amqp.rabbit.connection.RabbitUtils.java
License:Apache License
/** * Commit the Channel if not within a JTA transaction. * @param channel the RabbitMQ Channel to commit *///from ww w . j a va2s .co m public static void commitIfNecessary(Channel channel) { Assert.notNull(channel, "Channel must not be null"); try { channel.txCommit(); } catch (IOException ex) { throw new AmqpIOException(ex); } }
From source file:org.springframework.amqp.rabbit.core.RabbitTemplate.java
License:Apache License
@Override public Message receive(final String queueName) { return execute(new ChannelCallback<Message>() { @Override//from www . j a v a2 s . c o m public Message doInRabbit(Channel channel) throws IOException { GetResponse response = channel.basicGet(queueName, !isChannelTransacted()); // Response can be null is the case that there is no message on the queue. if (response != null) { long deliveryTag = response.getEnvelope().getDeliveryTag(); if (isChannelLocallyTransacted(channel)) { channel.basicAck(deliveryTag, false); channel.txCommit(); } else if (isChannelTransacted()) { // Not locally transacted but it is transacted so it // could be synchronized with an external transaction ConnectionFactoryUtils.registerDeliveryTag(getConnectionFactory(), channel, deliveryTag); } return RabbitTemplate.this.buildMessageFromResponse(response); } return null; } }); }
From source file:org.springframework.amqp.rabbit.core.RabbitTemplate.java
License:Apache License
@SuppressWarnings("unchecked") private <R, S> boolean doReceiveAndReply(final String queueName, final ReceiveAndReplyCallback<R, S> callback, final ReplyToAddressCallback<S> replyToAddressCallback) throws AmqpException { return this.execute(new ChannelCallback<Boolean>() { @Override/*from w w w . j a va 2 s. c o m*/ public Boolean doInRabbit(Channel channel) throws Exception { boolean channelTransacted = RabbitTemplate.this.isChannelTransacted(); GetResponse response = channel.basicGet(queueName, !channelTransacted); // Response can be null in the case that there is no message on the queue. if (response != null) { long deliveryTag = response.getEnvelope().getDeliveryTag(); boolean channelLocallyTransacted = RabbitTemplate.this.isChannelLocallyTransacted(channel); if (channelLocallyTransacted) { channel.basicAck(deliveryTag, false); } else if (channelTransacted) { // Not locally transacted but it is transacted so it could be synchronized with an external transaction ConnectionFactoryUtils.registerDeliveryTag(RabbitTemplate.this.getConnectionFactory(), channel, deliveryTag); } Message receiveMessage = RabbitTemplate.this.buildMessageFromResponse(response); Object receive = receiveMessage; if (!(ReceiveAndReplyMessageCallback.class.isAssignableFrom(callback.getClass()))) { receive = RabbitTemplate.this.getRequiredMessageConverter().fromMessage(receiveMessage); } S reply; try { reply = callback.handle((R) receive); } catch (ClassCastException e) { StackTraceElement[] trace = e.getStackTrace(); if (trace[0].getMethodName().equals("handle") && trace[1].getFileName().equals("RabbitTemplate.java")) { throw new IllegalArgumentException("ReceiveAndReplyCallback '" + callback + "' can't handle received object '" + receive + "'", e); } else { throw e; } } if (reply != null) { Address replyTo = replyToAddressCallback.getReplyToAddress(receiveMessage, reply); Message replyMessage = RabbitTemplate.this.convertMessageIfNecessary(reply); MessageProperties receiveMessageProperties = receiveMessage.getMessageProperties(); MessageProperties replyMessageProperties = replyMessage.getMessageProperties(); Object correlation = RabbitTemplate.this.correlationKey == null ? receiveMessageProperties.getCorrelationId() : receiveMessageProperties.getHeaders().get(RabbitTemplate.this.correlationKey); if (RabbitTemplate.this.correlationKey == null || correlation == null) { // using standard correlationId property if (correlation == null) { String messageId = receiveMessageProperties.getMessageId(); if (messageId != null) { correlation = messageId.getBytes(RabbitTemplate.this.encoding); } } replyMessageProperties.setCorrelationId((byte[]) correlation); } else { replyMessageProperties.setHeader(RabbitTemplate.this.correlationKey, correlation); } // 'doSend()' takes care about 'channel.txCommit()'. RabbitTemplate.this.doSend(channel, replyTo.getExchangeName(), replyTo.getRoutingKey(), replyMessage, null); } else if (channelLocallyTransacted) { channel.txCommit(); } return true; } return false; } }); }