[rabbitmq-discuss] Basic question on message delivery problem

Tom Reineke tom.reineke at googlemail.com
Thu Dec 5 11:02:14 GMT 2013



Thanks to the feedback of Simon and woodslee I found out what the problem 
was. For both localhost and dev-environment we use the same rabbitmq-cloud 
service and therefore I received only every second message in the first 
place. As I extracted the relevant code to a main-method (as you can see 
above) I obviously created yet another consumer, which is why now only 
every third message was displayed. The solution was to create a different 
mail queue for each environment.

Am Mittwoch, 4. Dezember 2013 16:26:47 UTC+1 schrieb Tom Reineke:
>
> 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/eea7c047/attachment.html>


More information about the rabbitmq-discuss mailing list