Spring Boot AMQP @RabbitListener not receiving messages

Richard :

I have a Spring Boot application, and I am trying to send and receive messages via RabbitMQ.

Problem

I can send the messages successfully to the queue (i.e. I see them on the queue in the RabbitMQ Manager), however my Receiver does not receive the messages.

I have a RESTful endpoint I call from JUnit that in turn calls the Sender. While this JUnit test is running the Spring context is loaded as expected, and the Sender is invoked that adds the messages to the queue successfully.

Question

Is there something more I need to do in order to get the Receiver to register so that it will listen for messages? (I suspect that because I am just running the JUnit test, it finishes before the Receiver can listen for messages). Is there a way to keep the test up an running so that the Receiver can consume the messages before it ends?

Code

Sender

@Service
public class RabbitMQSender {

    @Autowired
    private AmqpTemplate rabbitTemplate;

    @Value("${rabbitmq.exchangename}")
    private String exchange;

    @Value("${rabbitmq.routingkeyname}")
    private String routingkey;  

    public void send(String uuid) {
        rabbitTemplate.convertAndSend(exchange, routingkey, uuid);
        System.out.println("Send RabbitMQ ("+exchange+" "+routingkey+")  msg = " + uuid);       
    }
}

Receiver

public class RabbitMQReceiver {

    @RabbitListener(queues = "${rabbitmq.queuename}")
    public void receive(String in) {
        System.out.println("Received RabbitMQ  msg = " + in);       
    }
}

Configuration

@Configuration
public class RabbitMQConfig {

    @Value("${rabbitmq.queuename}")
    String queueName;

    @Value("${rabbitmq.exchangename}")
    String exchange;

    @Value("${rabbitmq.routingkeyname}")
    String routingkey;

    @Bean
    Queue queue() {
        return new Queue(queueName, false);
    }

    @Bean
    DirectExchange exchange() {
        return new DirectExchange(exchange);
    }

    @Bean
    Binding binding(Queue queue, DirectExchange exchange) {
        return BindingBuilder.bind(queue).to(exchange).with(routingkey);
    }

    @Profile("receiver")
    @Bean
    public RabbitMQReceiver receiver() {
        return new RabbitMQReceiver();
    }

    @Profile("sender")
    @Bean
    public RabbitMQSender sender() {
        return new RabbitMQSender();
    }
}
Abdelghani Roussi :

Your sender and receiver doesn't belong to the same profile ! You should includes both profiles in your Junit tests using @ActiveProfiles

@ActivesProfiles({"sender", "receiver"})

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=104901&siteId=1