RabbitMQ代碼操作之AmqpAdmin和RabbitListener


  • AmqpAdmin:RabbitMQ系統管理功能組件(可以創建exchange,queue,Binding)
@Test
    public void createExchange(){
        //創建交換器
        //amqpAdmin.declareExchange(new DirectExchange("amqpadmin.exchange"));
        //創建隊列(如果存在同名,則不創建)
        amqpAdmin.declareQueue(new Queue("amqpadmin.queue",true));
        //創建綁定規則   new Binding(目的地,目的地類型,交換器名字,路由件,參數頭)
        //amqpAdmin.declareBinding(new Binding("amqpadmin.queue", Binding.DestinationType.QUEUE,"amqpadmin.exchange","amqp.haha",null));
        //刪除隊列
        //amqpAdmin.deleteQueue("amqpadmin.queue");
    }
 
        
  • @EnableRabbit+@RabbitListener  監聽消息隊列的內容 
@Service
public class BookService {
    
    @RabbitListener(queues = "springbootTest")
    public void receive(Book book){
        System.out.println("收到的消息為:"+book.toString());
    }

    @RabbitListener(queues = "springbootTest")
    public void receive02(Message msg){
        //字節內容對象
        System.out.println(msg.getBody());
        //頭對象
        System.out.println(msg.getMessageProperties());

    }
}

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM