rabbitmq学习2-springboot和简单队列

接下来要学习的五种队列

一、springboot项目mq配置:

@Configuration
public class SimpleMqConfig {
    //简单队列配置开始
    private final static String workQunne = "helloWorld";

    @Bean
    public Queue helloWorld() {
        return new Queue(workQunne);
    }
}

二、生产者

如下同时有传递string及一个对象到队列

@Controller
@RequestMapping("simpleMq")
public class Producer {

    private Logger logger= LoggerFactory.getLogger(Producer.class);


    @Autowired
    private AmqpTemplate rabbitTemplate;

    @RequestMapping("/sendMessage")
    public void send() {
        String context = "hello " + new Date();
        logger.info("Sender : " + context);
        //发送字符串
        this.rabbitTemplate.convertAndSend("helloWorld", context);
        //发送json对象
        So so=new So();
        so.setOrderNum("1111");
        so.setCount(10);
        so.setStatus(1);
        logger.info("Sender----so : " + so);
        this.rabbitTemplate.convertAndSend("helloWorld", so);
    }
}

三、消费者

@Component
public class Customer {

    private Logger logger= LoggerFactory.getLogger(Customer.class);

    @RabbitListener(queues="helloWorld")
    public void processA(String msg) {
        logger.info("ReceiveA:"+msg);
    }

    @RabbitListener(queues="helloWorld")
    public void processA(So so) {
        logger.info("ReceiveB:"+so);
    }
}

测试:

利用postman去请求来发送消息,运行结果

总结:

在上面的一个队列中,发送了两种数据结果,一种是string,一种是对象,消费者中的两个同名方法(参数不一样)分别对其进行了处理

 

参考博客:https://blog.youkuaiyun.com/hellozpc/article/details/81436980#55_500

 

源码地址:

链接:https://pan.baidu.com/s/1NOUG32bGEQyUPgsV-LJ1Uw 
提取码:ur0v 
 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值