RabbitMQ:RabbitAdmin 與 RabbitTempla

disable發表於2021-09-09

RabbitAdmin

  • 該類封裝了對 RabbitMQ 的管理操作

@Beanpublic RabbitAdmin rabbitAdmin(ConnectionFactory connectionFactory){    return new RabbitAdmin(connectionFactory);
}@Autowiredprivate RabbitAdmin rabbitAdmin;
  • Exchange 操作

//建立四種型別的 Exchange,均為持久化,不自動刪除rabbitAdmin.declareExchange(new DirectExchange("direct.exchange",true,false));
rabbitAdmin.declareExchange(new TopicExchange("topic.exchange",true,false));
rabbitAdmin.declareExchange(new FanoutExchange("fanout.exchange",true,false));
rabbitAdmin.declareExchange(new HeadersExchange("header.exchange",true,false));//刪除 ExchangerabbitAdmin.deleteExchange("header.exchange");
  • Queue 操作

//定義佇列,均為持久化rabbitAdmin.declareQueue(new Queue("debug",true));
rabbitAdmin.declareQueue(new Queue("info",true));
rabbitAdmin.declareQueue(new Queue("error",true));//刪除佇列      rabbitAdmin.deleteQueue("debug");//將佇列中的訊息全消費掉rabbitAdmin.purgeQueue("info",false);
  • Binding 繫結

//繫結佇列到交換器,透過路由鍵rabbitAdmin.declareBinding(new Binding("debug",Binding.DestinationType.QUEUE,        "direct.exchange","key.1",new HashMap()));

rabbitAdmin.declareBinding(new Binding("info",Binding.DestinationType.QUEUE,        "direct.exchange","key.2",new HashMap()));

rabbitAdmin.declareBinding(new Binding("error",Binding.DestinationType.QUEUE,        "direct.exchange","key.3",new HashMap()));//進行解綁rabbitAdmin.removeBinding(BindingBuilder.bind(new Queue("info")).
        to(new TopicExchange("direct.exchange")).with("key.2"));//使用BindingBuilder進行繫結rabbitAdmin.declareBinding(BindingBuilder.bind(new Queue("info")).
        to(new TopicExchange("topic.exchange")).with("key.#"));//宣告topic型別的exchangerabbitAdmin.declareExchange(new TopicExchange("exchange1",true,false));
rabbitAdmin.declareExchange(new TopicExchange("exchange2",true,false));//exchange與exchange繫結rabbitAdmin.declareBinding(new Binding("exchange1",Binding.DestinationType.EXCHANGE,        "exchange2","key.4",new HashMap()));

RabbitTemplate

  • Spring AMQP 提供了 RabbitTemplate 來簡化 RabbitMQ 傳送和接收訊息操作

@Autowiredprivate RabbitAdmin rabbitAdmin;

RabbitTemplate 初始化

  • 設定 RabbitTemplate 的預設交換器、預設路由鍵、預設佇列

傳送訊息

  • send (自定義訊息 Message)

Message message = new Message("hello".getBytes(),new MessageProperties());// 傳送訊息到預設的交換器,預設的路由鍵rabbitTemplate.send(message);// 傳送訊息到指定的交換器,指定的路由鍵rabbitTemplate.send("direct.exchange","key.1",message);// 傳送訊息到指定的交換器,指定的路由鍵rabbitTemplate.send("direct.exchange","key.1",message,new CorrelationData(UUID.randomUUID().toString()));
  • convertAndSend(自動 Java 物件包裝成 Message 物件,Java 物件需要實現 Serializable 序列化介面)

User user = new User("linyuan");// 傳送訊息到預設的交換器,預設的路由鍵rabbitTemplate.convertAndSend(user);// 傳送訊息到指定的交換器,指定的路由鍵,設定訊息 IDrabbitTemplate.convertAndSend("direct.exchange","key.1",user,new CorrelationData(UUID.randomUUID().toString()));// 傳送訊息到指定的交換器,指定的路由鍵,在訊息轉換完成後,透過 MessagePostProcessor 來新增屬性rabbitTemplate.convertAndSend("direct.exchange","key.1",user,mes -> {
    mes.getMessageProperties().setDeliveryMode(MessageDeliveryMode.NON_PERSISTENT);        return mes;
});

接收訊息

  • receive(返回 Message 物件)

// 接收來自指定佇列的訊息,並設定超時時間Message msg = rabbitTemplate.receive("debug",2000l);
  • receiveAndConvert(將返回 Message 轉換成 Java 物件)

User user = (User) rabbitTemplate.receiveAndConvert();

------- 待完善 -------



作者:林塬
連結:


來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/200/viewspace-2820328/,如需轉載,請註明出處,否則將追究法律責任。

相關文章