用 原始AmqpTemplate连接rabbitmq ,自动创建queue,自动绑定change

myQueue2 是消息队列

 

发送方

 

package com.lm.test.controller;

import com.lm.test.message.StreamClient;
import org.springframework.amqp.core.AmqpTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.messaging.support.MessageBuilder;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Date;

//发送MQ测试
@RestController
public class SendMessage1Controller {

    @Autowired
    private AmqpTemplate amqpTemplate;

    @GetMapping("/sendMessage1")
    public void process(){
        String message = "发送的消息:"+new Date();
        amqpTemplate.convertAndSend("myQueue2",message);

    }
}

 

接收方

 

注意三种接收方式

package com.lm.test.message;


import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.rabbit.annotation.Exchange;
import org.springframework.amqp.rabbit.annotation.Queue;
import org.springframework.amqp.rabbit.annotation.QueueBinding;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Component;


/**
 * 接收方
 */
@Slf4j
@Component
public class MqReceiver {

//    //1.需要手动在 RabbitMQ手动先创建好,否则未创建的异常
//   @RabbitListener(queues = "myQueue2")
//    public void process(String message){
//        log.info("Mq 消息接受者:{}",message);
//    }


//    //2.自动创建队列
//    @RabbitListener(queuesToDeclare = @Queue("myQueue2"))
//    public void process(String message){
//        log.info("Mq 消息接受者:{}",message);
//    }

    //3. 自动创建,Exchage与Queue绑定
    @RabbitListener(bindings = @QueueBinding(
            value = @Queue("myQueue2"),
            exchange = @Exchange("myExchage")
             ))
    public void process(String message){
        log.info("Mq 消息接受者:{}",message);
    }
}

 

 

 

 

 

 

 

 

 

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值