3
votes

I am trying to test & benchmark spring-amqp for RabbitMQ with multiple queues so I was creating rabbit template for each queue and using it to send message. The message sent is successful and I can see a message published in the exchange but I don't see anything in the queue. I am guessing it's very minor setting but can't figure it out.

This is my applicationContext.xml

<bean id="banchmarkConnectionFactory" class="org.springframework.amqp.rabbit.connection.CachingConnectionFactory">
        <constructor-arg ref="benchmarkAmqpHost"/>
        <property name="username" ref="benchmarkAmqpUser"/>
        <property name="password" ref="benchmarkAmqpPass"/>
        <property name="virtualHost" ref="benchmarkAmqpVHost"/>
        <property name="channelCacheSize" value="10"/>
</bean>
<rabbit:template id="benchmarkAmqpTemplate"
        connection-factory="banchmarkConnectionFactory"
        exchange="my_exchange"
        queue="BenchmarkQueue"
        routing-key="BenchmarkQueue" />
<rabbit:admin connection-factory="banchmarkConnectionFactory"/>
<rabbit:queue name="BenchmarkQueue" auto-delete="true" durable="false" auto-declare="true"/>

This is my code which uses the benchmarkAmqpTemplate to publish to the queue.

public class publishMessage {
    @Autowired
    private RabbitTemplate benchmarkAmqpTemplate;

    protected void publish(String payload) {
        benchmarkAmqpTemplate.setQueue("BenchmarkQueue");
        benchmarkAmqpTemplate.convertAndSend("my_exchange", "BenchmarkQueue", payload);

    }
}

When I used the HelloWorld example it did publish a message in the queue so was wondering if I am doing something wrong. UPDATE I was able to solve this by adding direct-exchange tag in my context xml. My full xml looks like this:

    <beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:rabbit="http://www.springframework.org/schema/rabbit"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsdhttp://www.springframework.org/schema/rabbit http://www.springframework.org/schema/rabbit/spring-rabbit.xsd">

<bean id="banchmarkConnectionFactory" class="org.springframework.amqp.rabbit.connection.CachingConnectionFactory">
            <constructor-arg ref="benchmarkAmqpHost"/>
            <property name="username" ref="benchmarkAmqpUser"/>
            <property name="password" ref="benchmarkAmqpPass"/>
            <property name="virtualHost" ref="benchmarkAmqpVHost"/>
            <property name="channelCacheSize" value="10"/>
    </bean>
    <rabbit:template id="benchmarkAmqpTemplate"
            connection-factory="banchmarkConnectionFactory"
            exchange="my_exchange"
            queue="BenchmarkQueue"
            routing-key="BenchmarkQueue" />
    <rabbit:admin connection-factory="banchmarkConnectionFactory"/>
    <rabbit:queue name="BenchmarkQueue" auto-delete="true" durable="false" auto-declare="true"/>
    <rabbit:direct-exchange name="my_exchange">
        <rabbit:bindings>
            <rabbit:binding queue="BenchmarkQueue" key="BenchmarkQueue" />
        </rabbit:bindings>
    </rabbit:direct-exchange>
</beans>
1

1 Answers

8
votes

Sorry, but it looks like you misunderstood AMQP protocol a bit.

The message is published to the Exchange with the proper routingKey. The publisher (RabbitTemplate) doesn't need to know about queues at all.

The queue is a part of receiver, subscriber to the queue.

There is one more feature in between - binding. The queue is bound to the Exchange under the appropriate routingKey. One queue can be bound to several exchanges with different routing keys. By default all queues are bound to the default exchange ("") with routingKeys equal to their names.

Please, refer for more info to the RabbitMQ site.