Example usage for com.rabbitmq.client Channel waitForConfirmsOrDie

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

Introduction

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

Prototype

void waitForConfirmsOrDie(long timeout) throws IOException, InterruptedException, TimeoutException;

Source Link

Document

Wait until all messages published since the last call have been either ack'd or nack'd by the broker; or until timeout elapses.

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   w  w w .  jav  a 2 s.com*/
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.eventhandling.amqp.spring.SpringAMQPEventBus.java

License:Apache License

@Override
protected void prepareCommit(List<? extends EventMessage<?>> events) {
    Channel channel = connectionFactory.createConnection().createChannel(isTransactional);
    try {//w  w w .  ja  v a  2 s.com
        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:uk.ac.sanger.cgp.wwdocker.messages.Messaging.java

License:Open Source License

/**
 * Sends a message to the specified queue
 * @param queue/*from   w w  w. j ava2  s .  c o  m*/
 * @param in
 * @param host
 * @throws IOException
 * @throws InterruptedException 
 * @throws TimeoutException
 */
public void sendMessage(String queue, Object in, String host)
        throws IOException, InterruptedException, TimeoutException {
    String message;
    Builder propBuilder = MessageProperties.MINIMAL_PERSISTENT_BASIC.builder();
    if (in.getClass().equals(String.class)) {
        message = (String) in;
    } else {
        message = Utils.objectToJson(in);
        if (in.getClass().equals(WorkerState.class)) {
            host = ((WorkerState) in).getResource().getHostName();
        }
    }
    if (host != null) {
        Map<String, Object> headers = new HashMap();
        headers.put("host", host);
        propBuilder.headers(headers);
    }
    BasicProperties mProp = propBuilder.build();

    Connection connectionSend = getRmqConn();
    Channel channel = connectionSend.createChannel();
    channel.confirmSelect();
    channel.queueDeclare(queue, true, false, false, null);
    channel.basicPublish("", queue, mProp, message.getBytes());
    channel.waitForConfirmsOrDie(5000);
    logger.info(queue + " sent: " + message);
    channel.close();
    closeRmqConn(connectionSend);
}

From source file:uk.ac.sanger.cgp.wwdocker.messages.Messaging.java

License:Open Source License

public void sendFile(String queue, String host, File f)
        throws IOException, InterruptedException, TimeoutException {
    Map<String, Object> headers = new HashMap();
    headers.put("host", host);
    BasicProperties mProp = MessageProperties.MINIMAL_PERSISTENT_BASIC.builder().headers(headers).build();
    Connection connectionSend = getRmqConn();
    Channel channel = connectionSend.createChannel();
    channel.confirmSelect();/*ww  w . ja v a 2  s. c o m*/
    channel.queueDeclare(queue, true, false, false, null);
    channel.basicPublish("", queue, mProp, Files.readAllBytes(f.toPath()));
    channel.waitForConfirmsOrDie(5000);
    logger.info(queue + " file: " + f.getAbsolutePath());
    channel.close();
    closeRmqConn(connectionSend);
}