首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >springboot消息之AmqpAdmin管理组件的使用

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

作者头像
西西嘛呦
发布2020-08-26 15:43:50
6860
发布2020-08-26 15:43:50
举报
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));
        
    }
}
本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2020-02-11 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档