[rabbitmq-discuss] Basic question on message delivery problem

woodslee woodslee at 126.com
Thu Dec 5 01:47:18 GMT 2013


Hi Tom

I run the test in my computer, and I got all the messages form 0 to 9.
but i comment the statement 

if (!StringUtils.isEmpty(message)) 

Is the static method "isEmpty"  wrong?


----- Original Message ----- 
From: Tom Reineke 
To: rabbitmq-discuss 
Sent: 2013-12-04, 23:26:47
Subject: [rabbitmq-discuss] Basic question on message delivery problem


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/20131205/839c9771/attachment.html>


More information about the rabbitmq-discuss mailing list