Announcement Announcement Module
No announcement yet.
Cannot send message to RabbitMQ Page Title Module
Move Remove Collapse
Conversation Detail Module
  • Filter
  • Time
  • Show
Clear All
new posts

  • Cannot send message to RabbitMQ

    Hello all,

    I'm trying a trivial RabbitMQ example, and everything works (eg, I can receive messages using Spring/Java), plus I can send messages using the RabbitMQ cli and web interface. The only thing which doesn't work is sending messages using the Java client (no error message, or anything), and it's driving me nuts :-)

    Anyone any idea what I'm missing here? I've attached a sample project below.



    Attached Files

  • #2
    In your Producer, it looks like you are trying a few different things. I believe this is the one you want:
    amqpTemplate.convertAndSend("helloworld.queue", "Hello World 2");
    That is sending a message whose content is "Hello World 2" along with a *routing-key* of "helloworld.queue". Since your RabbitTemplate instance is being created without any default exchange explicitly provided, it will send to the *no-name* exchange. It is a direct exchange and every queue is bound to that exchange with the queue's name being a binding key. Try commenting out the other 2 send method calls for now.

    I assume you are starting the consumer and letting it run while you run the producer?


    • #3
      Hi Mark,

      Thanks for the quick response. I was indeed trying multiple things here. I started with the code above, but it didn't work; no message was received.

      I usually start the producer, and then the consumer, but I also tried the other way around, etc. That's also why the loops are there. But just running the producer does't do anything, ie when I check the RabbitMQ web console, no messages are put on the queue.

      However, when I run the following:
      ./rabbitmqadmin publish routing_key=helloworld.queue payload="hello, world"
      I see the message being put on the queue, plus it's also picked up by the consumer. So I'm a bit at a loss here why my producer doesn't do anything. I'm sure I'm overlooking something trivial here, but it would be nice to know what I'm missing here. If you or anyone else has some suggestions, that would be appreciated!



      • #4
        I tried some more stuff, and I came up with this code:

        package nl.jworks.rabbitmq;
        import com.rabbitmq.client.ConnectionFactory;
        import com.rabbitmq.client.Connection;
        import com.rabbitmq.client.Channel;
        public class Send {
            private final static String QUEUE_NAME = "newQueueName";
            public static void main(String[] argv) throws Exception {
                ConnectionFactory factory = new ConnectionFactory();
                Connection connection = factory.newConnection();
                Channel channel = connection.createChannel();
                channel.queueDeclare(QUEUE_NAME, false, false, false, null);
                String message = "Hello World!";
                channel.basicPublish("", QUEUE_NAME, null, message.getBytes());
                System.out.println(" [x] Sent '" + message + "'");
                System.out.println("Close channel");
                System.out.println("Close connection");
        To send a message to RabbitMQ. This works a little bit: when I run the following, the queue name is created, and the message is sent, but the application hangs at the 'channel.close()'. The 'close channel' message is never printed to the console. I'm still trying to figure out why the Java version doesn't work, and the command line does...



        • #5
          Yeah! It works! I was running RabbitMQ 2.8.2, and I uninstalled it, upgraded to 2.8.4, and now it works!!!! Nice! Thanks for the help!!!!!!