大菜菜學習RabbitMQ——第九篇

AK梦發表於2024-04-09

這篇使用較為easy的方式去宣告佇列還有交換機

所以我們先把上篇文章裡面建立的佇列還有交換機刪除

首先我們先把DirectConfiguration這個類的@Configration註解註釋

然後就可以開始進行刪除操作了

然後我們就可以開始在Listener裡面寫程式碼了

@RabbitListener(bindings = @QueueBinding(
            value = @Queue(),
            exchange = @Exchange(),
            key = ""
    ))
    public void listenDirectQueue3(String msg) throws InterruptedException {
        System.out.println("消費者1收到了direct.queue3的訊息:【" + msg + "】");
    }

然後寫完以後就是這個樣子

@RabbitListener(bindings = @QueueBinding(
            value = @Queue(name = "direct.queue3" , durable = "true"),
            exchange = @Exchange(name = "hmall.direct1" , type = ExchangeTypes.DIRECT),
            key = {"red" , "blue"}
    ))
    public void listenDirectQueue3(String msg) throws InterruptedException {
        System.out.println("消費者1收到了direct.queue3的訊息:【" + msg + "】");
    }

    @RabbitListener(bindings = @QueueBinding(
            value = @Queue(name = "direct.queue4" , durable = "true"),
            exchange = @Exchange(name = "hmall.direct1" , type = ExchangeTypes.DIRECT),
            key = {"red" , "yellow"}
    ))
    public void listenDirectQueue4(String msg) throws InterruptedException {
        System.out.println("消費者2收到了direct.queue4的訊息:【" + msg + "】");
    }

最後我們執行,我們就會發現

所有的東西我們都建立好了

相關文章