Note that there are some explanatory texts on larger screens.

plurals
  1. POConsumer is not receiving message from MQ when message is sent before consumer is listening
    primarykey
    data
    text
    <p>I am using MQs for the first time and attempting to implement a logging system with RabbitMQ. My implementation involves a 'sender'</p> <pre><code>/* * This class sends messages over MQ */ public class MQSender { private final static String EXCHANGE_NAME = "mm_exchange"; private final static String[] LOG_LEVELS = {"green", "orange", "red", "black"}; public static void main(String[] args) throws IOException, ShutdownSignalException, ConsumerCancelledException, InterruptedException { /* * Boilerplate stuff */ ConnectionFactory factory = new ConnectionFactory(); factory.setHost("localhost"); Connection connection = factory.newConnection(); Channel channel = connection.createChannel(); //declare the exchange that messages pass through, type=direct channel.exchangeDeclare(EXCHANGE_NAME, "direct"); String[] levels = {"green", "orange", "red", "black"}; for (String log_level : levels) { String message = "This is a " + log_level + " message"; System.out.println("Sending " + log_level + " message"); //publish the message with each of the bindings in levels channel.basicPublish(EXCHANGE_NAME, log_level, null, message.getBytes()); } channel.close(); connection.close(); } } </code></pre> <p>Which sends one message for each of my colors to the exchange, where the color will be used as bindings. And it involves a 'receiver' </p> <pre><code>public class MQReceiver { private final static String EXCHANGE_NAME = "mm_exchange"; private final static String[] LOG_LEVELS = {"green", "orange", "red", "black"}; public static void main(String[] args) throws IOException, ShutdownSignalException, ConsumerCancelledException, InterruptedException { receiveMessagesFromQueue(2); } public static void receiveMessagesFromQueue(int maxLevel) throws IOException, ShutdownSignalException, ConsumerCancelledException, InterruptedException { /* * Boilerplate stuff */ ConnectionFactory factory = new ConnectionFactory(); factory.setHost("localhost"); Connection connection = factory.newConnection(); Channel channel = connection.createChannel(); //declare the exchange that messages pass through, type=direct channel.exchangeDeclare(EXCHANGE_NAME, "direct"); //generate random queue String queueName = channel.queueDeclare().getQueue(); //set bindings from 0 to maxLevel for the queue for (int level = 0; level &lt;= maxLevel; level++) { channel.queueBind(queueName, EXCHANGE_NAME, LOG_LEVELS[level]); } QueueingConsumer consumer = new QueueingConsumer(channel); channel.basicConsume(queueName, true, consumer); while(true) { //waits until a message is delivered then gets that message QueueingConsumer.Delivery delivery = consumer.nextDelivery(); String message = new String(delivery.getBody()); String routingKey = delivery.getEnvelope().getRoutingKey(); System.out.println(" [x] Received '" + routingKey + "':'" + message + "'"); } } } </code></pre> <p>which is given as a parameter a number representing which color bindings I would like it to be fed from the exchange. </p> <p>In my implementation, and in RabbitMQ in general, it seems like messages are stored in the exchange until the <code>Consumer</code> asks for them, at which point they are distributed to their respective queues and then sent one at a time to the client (or consumer in MQ lingo). My problem is that when I run the <code>MQSender</code> class before running the <code>MQReceiver</code> class the messages never get delivered. But when I run the <code>MQReceiver</code> class first the messages are received. From my understanding of MQ I would think that the messages should be stored on the server until the <code>MQReceiver</code> class is run, then the messages should be delivered to their consumers, however this is not what is happening. My main question is whether these messages can be stored in an exchange and if not, where should they be stored so that they will be delivered once a consumer (i.e. my <code>MQReceiver</code> class) is called?</p> <p>Thanks for your help!</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload