springboot消息之AmqpAdmin管理組件的使用


package com.gong.springbootrabbitmq;

import com.gong.springbootrabbitmq.bean.Book;
import org.junit.After;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.amqp.core.AmqpAdmin;
import org.springframework.amqp.core.Binding;
import org.springframework.amqp.core.DirectExchange;
import org.springframework.amqp.core.Queue;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;

import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;

@RunWith(SpringRunner.class)
@SpringBootTest
public class SpringbootRabbitmqApplicationTests {

    @Autowired
    RabbitTemplate rabbitTemplate;
    
  @Autowired AmqpAdmin amqpAdmin;

    @Test
    public void contextLoads() {
        //點對點消息
        //rabbitTemplate.send(exchange,routeKey,message);message需要自定義消息內容和消息頭
        //rabbitTemplate.convertAndSend(exchange,routeKey,object);主需要傳入要發送的對象,會自動序列化發送給rabbitmq,
        // object默認當成消息體
        Map<String,Object> map = new HashMap<>();
        map.put("msg","這是第一個消息");
        map.put("data", Arrays.asList("hello",123,true));
        rabbitTemplate.convertAndSend("exchange.direct","gong.news",
                new Book("王者榮耀","寒冰"));

    }

    @Test
    public void testRecieve(){
        Object receiveAndConvert = rabbitTemplate.receiveAndConvert("gong.news");
        System.out.println(receiveAndConvert.getClass());
        System.out.println(receiveAndConvert);
    }

    @Test
    public void testMsg(){
        rabbitTemplate.convertAndSend("exchange.fanout","",
                new Book("王者榮耀","寒冰"));
    }
    
    @Test
    public void testAmqpAdmin(){
        //創建交換器
        amqpAdmin.declareExchange(new DirectExchange("amqpAdmin.exchange"));
        //創建隊列
        amqpAdmin.declareQueue(new Queue("amqpAdmin.queue",true));
        //綁定
        amqpAdmin.declareBinding(new Binding("amqpAdmin.queue",Binding.DestinationType.QUEUE,
                "amqpAdmin.exchange","amqp.news",null));
        
    }
}

 


免責聲明!

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



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