SpringBoot日記——MQ訊息佇列整合(二)

來源:互聯網
上載者:User

標籤:override   一起   java   null   text   array   web管理   hashmap   自訂   

  基於第一篇文章搭建好環境以後,我們這篇文章繼續介紹如何在springboot中使用RabbitMQ。

  1)、單播:添加好pom檔案和自訂配置後,來看:

    @Autowired    RabbitTemplate rabbitTemplate;    @Test    public void contextLoads() {        // 對象被預設JAVA序列化發送,參數:Exchange,routingKey,訊息        rabbitTemplate.convertAndSend("exchange.direct", "iceoooodin.news", "瓦爾克莉");    }

  來看看我們發送的訊息是否成功了:

  ,成功擷取了~。

  同樣,使用代碼來直接擷取:

    @Test    public void receive() {        // 接受資料        Object o = rabbitTemplate.receiveAndConvert("iceoooodin.news");        System.out.println(o.getClass());        System.out.println(o);    }

  另外,我們除了str類型,還是發送map、對象等等,比如:

    @Test    public void contextLoads() {        Map<String, Object> map = new HashMap<>();        map.put("msg", "第一個資料");        map.put("data", Arrays.asList("helloworld", 123, true));        // 對象被預設JAVA序列化發送        rabbitTemplate.convertAndSend("exchange.direct", "iceoooodin.news", map);    }
@Test    public void contextLoads() {        // 對象被預設JAVA序列化發送        rabbitTemplate.convertAndSend("exchange.direct", "iceoooodin.news", new Book("金瓶M", "em.."));    }
public class Book {    private String bookName;    private String author;    @Override    public String toString() {        return "Book{" +                "bookName=‘" + bookName + ‘\‘‘ +                ", author=‘" + author + ‘\‘‘ +                ‘}‘;    }    public Book(String bookName, String author) {        this.bookName = bookName;        this.author = author;    }    public Book() {    }    public String getBookName() {        return bookName;    }    public void setBookName(String bookName) {        this.bookName = bookName;    }    public String getAuthor() {        return author;    }    public void setAuthor(String author) {        this.author = author;    }}
Book.java

   2)、然後我們來看廣播,也就是發送一堆訊息是如何處理的:

@Test    public void sendMst() {        rabbitTemplate.convertAndSend("exchange.fanout", "", new Book("紅樓", "草 "));    }

  如果發送的是廣播,可以發現,訊息會被分別發送到匹配的全部訊息佇列中:

  3)、我們學會發送和接收了,再看看如何建立Exchange或者Queue吧,我們建立和綁定寫在了一起,根據需要自己拆:

    @Autowired    AmqpAdmin amqpAdmin;  //這個amqpadmin是用來管理QP的,可以建立、刪除等操作;    @Test    public void creatExchange() {        // 建立Exchange        amqpAdmin.declareExchange(new DirectExchange("amqpadmin.exchange"));        System.out.println("建立exchange完成");        // 建立Queue        amqpAdmin.declareQueue(new Queue("amqpadmin.queue", true));
     System.out.println("建立Queue完成"); // 綁定 amqpAdmin.declareBinding(new Binding("amqpadmin.queue", Binding.DestinationType.QUEUE, "amqpadmin.exchange", "amqp.haha", null)); }

   結果就懶得寫了,大家自己實驗看一下就知道了,到web管理介面看看是否有正確添加和綁定~

SpringBoot日記——MQ訊息佇列整合(二)

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.