Nihal Sharma
Nihal Sharma

Reputation: 2437

Why is Sender not sending data to the queue in RabbitMQ?

I have implemented RabbitMQ and stuck in one place where it seems as if my sender is not able to send any data to the queue.

My Producer class:

@Service
public class MessageSender {

@Autowired
private AmqpTemplate template;

public void send(String text) {
    template.convertAndSend(text);
 }
}

my spring config file looks like this:

<rabbit:connection-factory id="connectionFactory"
                           addresses="${connectionFactory.addresses}" channel-cache-size="${connectionFactory.channel-cache-size}"
                           virtual-host="${connectionFactory.vhost}" username="${connectionFactory.user}"
                           password="${connectionFactory.password}" />

<rabbit:template id="amqpTemplate" connection-factory="connectionFactory" queue="myQueue" exchange="myExchange" routing-key="dummyKey"/>

<rabbit:queue name="myQueue" />

<rabbit:direct-exchange name="myExchange">
    <rabbit:bindings>
        <rabbit:binding queue="myQueue" />
    </rabbit:bindings>
</rabbit:direct-exchange>

<rabbit:listener-container connection-factory="connectionFactory">
    <rabbit:listener ref="messageHandler" method="onMessage" queue-names="myQueue" />
</rabbit:listener-container>

<rabbit:admin connection-factory="connectionFactory" />

<bean id="messageHandler" class="com.tm.email.sender.spring.MessageHandler" />

I cannot find out what the issue is.

The below mentioned thing works perfectly. I can easily push the messages on to the queue and then my sender class works perfectly fine.

public class Send {
private final static String QUEUE_NAME = "myQueue";
public static void main(String[] argv) throws Exception {
    ConnectionFactory factory = new ConnectionFactory();
    factory.setHost("localhost");
    Connection connection = factory.newConnection();
    Channel channel = connection.createChannel();
    int i = 0;
    while(i < 10000){
        HashMap message = new HashMap();
        message.put("message number", i);
        channel.basicPublish("", QUEUE_NAME, null, SerializationUtils.serialize(message));
        i++;
    }
    channel.close();
    connection.close();
  }
}

Upvotes: 0

Views: 2441

Answers (1)

Artem Bilan
Artem Bilan

Reputation: 121552

You have default routing-key="dummyKey" on the <rabbit:template> definition. That means that your template.convertAndSend(text); will send a Message to the myExchange through the dummyKey routing-key.

Since you don't provide the binding for that myQueue via dummyKey you messages just lost.

From the raw AMQP code you just send message to the default Exchange ("") using a default routing-key for each queue - its name.

For the listener it doesn't matter from where and how the message has appeared in the queue. That's why the second 'send' variant works well.

So, to fix your use-case with AmqpTemplate, you have to add this:

<rabbit:binding queue="myQueue" key="dummyKey"/>

Upvotes: 6

Related Questions