[rabbitmq-discuss] Basic question on message delivery problem

Tom Reineke tom.reineke at googlemail.com
Wed Dec 4 15:26:47 GMT 2013


Hi,

I seem to have a very basic problem when setting up RabbitMQ and would be 
grateful for any help. Ich have the following class:

public class RabbitTest {

private final static ConnectionFactory factory = new ConnectionFactory();
        private final static String MAIL_QUEUE_NAME = "mailQueue";
        private static Connection connection;
        private static Channel channel;
        private static QueueingConsumer consumer;

public static void main(final String[] args) throws IOException, 
KeyManagementException, NoSuchAlgorithmException,  URISyntaxException, 
InterruptedException {
        factory = new ConnectionFactory();
factory.setHost("localhost");
        connection = factory.newConnection();
        channel = connection.createChannel();
        channel.queueDeclare(MAIL_QUEUE_NAME, true, false, false, null);
        consumer = new QueueingConsumer(channel);
        channel.basicConsume(MAIL_QUEUE_NAME, false, consumer);
        new Thread() {
            @Override
            public void run() {
                while (true) {
                    try {
                        // The processor stops here until something
                        // is in the queue. It does not continue with the 
next
                        final QueueingConsumer.Delivery delivery = consumer
                                .nextDelivery();
                        final String message = new 
String(delivery.getBody());
                        if (!StringUtils.isEmpty(message)) {
                            System.out.println("########## message 
#############");
                            System.out.println(message);
                        }
                    } catch (final Exception e) {

                    }
                }
            }
        }.start();

        for (int i = 0; i < 10; i++) {
            createMailTask("message " + i);
        }

        Thread.sleep(10000);
        channel.close();
        connection.close();
    }

    public static void createMailTask(final String amqpMessage)
            throws IOException {
        final Channel ch = connection.createChannel();
        ch.queueDeclare(MAIL_QUEUE_NAME, true, false, false, null);
        ch.basicPublish("", MAIL_QUEUE_NAME,
                MessageProperties.PERSISTENT_TEXT_PLAIN, 
amqpMessage.getBytes());
        ch.close();
    }
}

I would have assumed that I get a log output for each of the 10 messages; 
instead I only get:

[Thread-0] INFO de.apt.utils.APTMailUtil - ########## message #############
[Thread-0] INFO de.apt.utils.APTMailUtil - message 2
[Thread-0] INFO de.apt.utils.APTMailUtil - ########## message #############
[Thread-0] INFO de.apt.utils.APTMailUtil - message 5
[Thread-0] INFO de.apt.utils.APTMailUtil - ########## message #############
[Thread-0] INFO de.apt.utils.APTMailUtil - message 8

Can you tell me where the problem is in my code?
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.rabbitmq.com/pipermail/rabbitmq-discuss/attachments/20131204/0e9514b7/attachment.html>


More information about the rabbitmq-discuss mailing list