Question

Sorry I'am new in the amqp world. I try write simple app:

public class HelloApp {

public static void main(String[] args) {
    ApplicationContext context = new ClassPathXmlApplicationContext("spring-config.xml");

    final TestSender bean = context.getBean(TestSender.class);
    bean.sendMessage();

    }
}



@Component
public class TestSender {

    @Autowired
    private RabbitTemplate template;

    public void sendMessage() {

        final Message message = new Message("Sth".getBytes(), new MessageProperties());

        template.send(message);
        System.out.println("Was sent");
    }

}


@Component
public class MessageReceiver implements ChannelAwareMessageListener{

    @Override
    public void onMessage(Message message, Channel channel) throws Exception {

        System.out.println("RECEIVE "+message.getBody().toString());

    }

And .xml files:

spring-amqp.xml

    <?xml version="1.0" encoding="UTF-8"?>
<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.xsd http://www.springframework.org/schema/rabbit        http://www.springframework.org/schema/rabbit/spring-rabbit.xsd">

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

    <rabbit:template connection-factory="connectionFactory" id="rabbitTemplate" channel-transacted="true"/>
    <rabbit:queue name="userMesssageQueue" />

    <rabbit:listener-container connection-factory="connectionFactory">
        <rabbit:listener ref="lis" queue-names="userMesssageQueue"/>
    </rabbit:listener-container>

    <bean id="transactionManager" class="org.springframework.amqp.rabbit.transaction.RabbitTransactionManager">
        <property name="connectionFactory" ref="connectionFactory"/>
    </bean>
    <bean id="lis" class="foo.bar.MessageReceiver"/>

spring-config.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:context="http://www.springframework.org/schema/context"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">
    <context:annotation-config />
    <context:component-scan base-package="foo.bar"/>


    <import resource="spring-amqp.xml"/>
</beans>

When I run this application I see:

Was sent

But I would like to see:

Was sent
RECEIVE ...

What is wrong??

Was it helpful?

Solution

Your issue here is really around the low knowledge of AMQP. You should understand what is Exchange, routingKey and Binding.

You need to configure:

<rabbit:direct-exchange name="myExchange">
    <rabbit:bindings>
         <rabbit:binding queue="userMesssageQueue" key="userMesssage" />
    </rabbit:bindings>
</rabbit:direct-exchange>

From other side you should send message to concrete exchange using concrete routingKey. In your case:

template.send("myExchange", "userMesssage", message);

Only in this case your message will be placed to the userMesssageQueue.

By default RabbitTemplate uses empty string for exchange (the Default one) and empty string for routingKey. Since your userMesssageQueue isn't bound to that exchange with that routing key, your listener doesn't receive it.

The sending works without errors, because messages are placed to the exchanges and it is enough for Producer (sender).

In the end your message is just dropped on the RabbitMQ broker, because there is no queues bound to the "" routing key.

Please, read more docs on the RabbitMQ site and Spring AMQP.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top