RabbitMQ basic usage (annotation method)

RabbitMQ can create queues based on annotations, as follows:

1. Manually create myQueue queue in RabbitMQ management interface

    1. Sender Code:
 @Autowired
    private AmqpTemplate amqpTemplate;

    public void send(){
        String msg = "mqsender send ..." + new Date();
        amqpTemplate.convertAndSend("myQueue", msg);
    }
     2. Receiver code
    /**
     * You need to manually create a queue myQueue in the RabbitMQ management interface under 39... 50:15672 /
     * @param msg
     */
    @RabbitListener(queues = "myQueue")
    public void receive(String msg){
        log.info("mqReceive = {}" , msg );
    }

2. Automatically create myQueue queue through annotation

    1. Sender program is the same as above

    2. The receiver's procedures are as follows:
    /**
     *
     * @param msg
     */
    @RabbitListener(queuesToDeclare = @Queue("myQueue"))
    public void receive(String msg){
        log.info("mqReceive = {}" , msg );
    }

3. Automatic creation, queue and exchange binding

     1. Sender program unchanged

     2. The receiver's procedures are as follows:
     // 3. Automatic creation, queue and exchange binding
    @RabbitListener(bindings = @QueueBinding(
            value = @Queue("myQueue"),
            exchange = @Exchange("myExchange")
    ))
    public void receive(String msg){
        log.info("mqReceive = {}" , msg );
    }

4. Real combat simulation message grouping

    1. Sender:
    /**
     * Analog message packet sender
     */
    public void sendOrder(){
        String msg = "mqsender send ..." + new Date();
        // Parameters: switch, route key, message
        amqpTemplate.convertAndSend("myOrder","computer", msg);
    }
    2. Receiver:
/**----------- Analog message grouping--------------------*/
    /**
     * Digital provider service receive message
     * Messages are sent to the switch, which sends them to different queues according to different key s
     */
    @RabbitListener(bindings = @QueueBinding(
            exchange = @Exchange("myOrder"),
            key = "computer",
            value = @Queue("computerOrder")
    ))
    public void receiveComputer(String msg){
        log.info(" receiveComputer service = {}" , msg );
    }
    /**
     * Fruit supplier service receive message
     */
    @RabbitListener(bindings = @QueueBinding(
            value = @Queue("fruitOrder"),
            key = "fruit",
            exchange = @Exchange("myOrder")
    ))
    public void receiveFruit(String msg){
        log.info(" receiveFruit service = {}" , msg );
    }
    3. Test cases
    @Autowired
    private MQSender sender;

    @Test
    public void sendOrderTest() {
        sender.sendOrder();
    }
    4. results: 
    The message is sent to the switch, and the switch sends it to the corresponding queue through the routing key.
    So the computerOrder queue gets the message, and then receiveComputer() receives the message.

Tags: RabbitMQ

Posted on Wed, 08 Jan 2020 09:57:57 -0800 by g5604