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.csdn.net/hellozpc/article/details/81436980#55_500

 

源碼地址:

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

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章