Search in sources :

Example 1 with ConfirmListener

use of com.rabbitmq.client.ConfirmListener in project microservices by pwillhan.

the class ConfirmedPublisher method send.

public void send(String exchange, String key, String message) {
    Connection connection = null;
    Channel channel = null;
    try {
        ConnectionFactory factory = new ConnectionFactory();
        factory.setHost("localhost");
        connection = factory.newConnection();
        channel = connection.createChannel();
        channel.addConfirmListener(new ConfirmListener() {

            public void handleNack(long deliveryTag, boolean multiple) throws IOException {
                LOGGER.warn("Message(s) rejected.");
            }

            public void handleAck(long deliveryTag, boolean multiple) throws IOException {
                LOGGER.warn("Message(s) confirmed.");
            }
        });
        channel.confirmSelect();
        channel.basicPublish(exchange, key, null, message.getBytes());
    } catch (IOException e) {
        LOGGER.error(e.getMessage(), e);
    } finally {
        if (connection != null) {
            try {
                connection.close();
            } catch (IOException e) {
                LOGGER.warn("Failed to close connection: " + e.getMessage(), e);
            }
        }
    }
}
Also used : ConnectionFactory(com.rabbitmq.client.ConnectionFactory) Channel(com.rabbitmq.client.Channel) Connection(com.rabbitmq.client.Connection) IOException(java.io.IOException) ConfirmListener(com.rabbitmq.client.ConfirmListener)

Example 2 with ConfirmListener

use of com.rabbitmq.client.ConfirmListener in project rabbitmq-java-client by rabbitmq.

the class Confirm method waitForConfirms.

@Test
public void waitForConfirms() throws IOException, InterruptedException, TimeoutException {
    final SortedSet<Long> unconfirmedSet = Collections.synchronizedSortedSet(new TreeSet<Long>());
    channel.addConfirmListener(new ConfirmListener() {

        public void handleAck(long seqNo, boolean multiple) {
            if (!unconfirmedSet.contains(seqNo)) {
                fail("got duplicate ack: " + seqNo);
            }
            if (multiple) {
                unconfirmedSet.headSet(seqNo + 1).clear();
            } else {
                unconfirmedSet.remove(seqNo);
            }
        }

        public void handleNack(long seqNo, boolean multiple) {
            fail("got a nack");
        }
    });
    for (long i = 0; i < NUM_MESSAGES; i++) {
        unconfirmedSet.add(channel.getNextPublishSeqNo());
        publish("", "confirm-test", true, false);
    }
    channel.waitForConfirmsOrDie(60000);
    if (!unconfirmedSet.isEmpty()) {
        fail("waitForConfirms returned with unconfirmed messages");
    }
}
Also used : ConfirmListener(com.rabbitmq.client.ConfirmListener) Test(org.junit.Test)

Example 3 with ConfirmListener

use of com.rabbitmq.client.ConfirmListener in project rxrabbit by meltwater.

the class RxRabbitTests method getDroppingAndExceptionThrowingChannelFactory.

private ChannelFactory getDroppingAndExceptionThrowingChannelFactory(final int dropAtMessageN, final int exceptionAtMessageN) {
    return new ChannelFactory() {

        final AtomicInteger publishCount = new AtomicInteger(0);

        @Override
        public ConsumeChannel createConsumeChannel(String queue) throws IOException {
            return null;
        }

        @Override
        public ConsumeChannel createConsumeChannel(String exchange, String routingKey) throws IOException {
            return null;
        }

        @Override
        public PublishChannel createPublishChannel() throws IOException {
            final PublishChannel delegate = channelFactory.createPublishChannel();
            return new PublishChannel() {

                @Override
                public void addConfirmListener(ConfirmListener listener) {
                    delegate.addConfirmListener(listener);
                }

                @Override
                public void basicPublish(String exchange, String routingKey, AMQP.BasicProperties props, byte[] body) throws IOException {
                    log.infoWithParams("Publishing message", "id", props.getMessageId());
                    final int count = publishCount.incrementAndGet();
                    if (count == dropAtMessageN) {
                        log.infoWithParams("Dropping message", "id", props.getMessageId());
                    } else if (count == exceptionAtMessageN) {
                        throw new IOException("expected");
                    } else {
                        delegate.basicPublish(exchange, routingKey, props, body);
                    }
                }

                @Override
                public long getNextPublishSeqNo() {
                    return delegate.getNextPublishSeqNo();
                }

                @Override
                public boolean waitForConfirms() throws InterruptedException {
                    return delegate.waitForConfirms();
                }

                @Override
                public boolean waitForConfirms(long timeout) throws InterruptedException, TimeoutException {
                    return delegate.waitForConfirms(timeout);
                }

                @Override
                public void confirmSelect() throws IOException {
                    delegate.confirmSelect();
                }

                @Override
                public void close() {
                    delegate.close();
                }

                @Override
                public void closeWithError() {
                    delegate.closeWithError();
                }

                @Override
                public boolean isOpen() {
                    return delegate.isOpen();
                }

                @Override
                public int getChannelNumber() {
                    return delegate.getChannelNumber();
                }
            };
        }

        @Override
        public AdminChannel createAdminChannel() throws IOException {
            return null;
        }
    };
}
Also used : AtomicInteger(java.util.concurrent.atomic.AtomicInteger) IOException(java.io.IOException) DefaultChannelFactory(com.meltwater.rxrabbit.impl.DefaultChannelFactory) ConfirmListener(com.rabbitmq.client.ConfirmListener)

Aggregations

ConfirmListener (com.rabbitmq.client.ConfirmListener)3 IOException (java.io.IOException)2 DefaultChannelFactory (com.meltwater.rxrabbit.impl.DefaultChannelFactory)1 Channel (com.rabbitmq.client.Channel)1 Connection (com.rabbitmq.client.Connection)1 ConnectionFactory (com.rabbitmq.client.ConnectionFactory)1 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)1 Test (org.junit.Test)1