Results 1 to 5 of 5

Thread: Cannot send message to RabbitMQ

  1. #1
    Join Date
    Dec 2005
    Posts
    10

    Default 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.

    Thanks,

    Erik

    epub-organizer-amqp-rabbitmq.zip

  2. #2
    Join Date
    Oct 2005
    Location
    Boston, MA
    Posts
    2,844

    Default

    In your Producer, it looks like you are trying a few different things. I believe this is the one you want:
    Code:
    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. #3
    Join Date
    Dec 2005
    Posts
    10

    Default

    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:
    Code:
    ./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!

    Erik

  4. #4
    Join Date
    Dec 2005
    Posts
    10

    Default

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

    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();
            factory.setHost("localhost");
            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 + "'");
    
            channel.close();
            System.out.println("Close channel");
            connection.close();
            System.out.println("Close connection");
    
            System.out.println("Exit");
        }
    }
    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...

    Erik

  5. #5
    Join Date
    Dec 2005
    Posts
    10

    Default

    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!!!!!!

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •