• Spring AMQP:RabbitAdmin&&声明


    一.pom.xml

    spring boot项目

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.3.0.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.wj</groupId>
    <artifactId>spring-amqp</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>spring-amqp</name>
    <description>Demo project for Spring Boot</description>
    
    <properties>
        <java.version>1.8</java.version>
    </properties>
    
    <dependencies>
        <dependency>
            <groupId>com.rabbitmq</groupId>
            <artifactId>amqp-client</artifactId>
            <version>5.7.3</version>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-amqp</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter</artifactId>
        </dependency>
    
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
            <exclusions>
                <exclusion>
                    <groupId>org.junit.vintage</groupId>
                    <artifactId>junit-vintage-engine</artifactId>
                </exclusion>
            </exclusions>
        </dependency>
    </dependencies>
    View Code 

    二.配置类

    @Configuration
    public class RabbitMQConfig {
        @Bean
        public ConnectionFactory connectionFactory(){
            CachingConnectionFactory connectionFactory = new CachingConnectionFactory();
            connectionFactory.setAddresses("192.168.10.132:5672");
            connectionFactory.setUsername("guest");
            connectionFactory.setPassword("guest");
            connectionFactory.setVirtualHost("/");
            return connectionFactory;
        }
    
        @Bean
        public RabbitAdmin rabbitAdmin(ConnectionFactory connectionFactory){
            RabbitAdmin rabbitAdmin = new RabbitAdmin(connectionFactory);
            //autoStartup必须要设置为true,否则Spring容器不会加载RabbitAdmin类
            rabbitAdmin.setAutoStartup(true);
            return rabbitAdmin;
        }
    }
    

    三.RabbitAdmin测试

    @SpringBootTest
    class SpringAmqpApplicationTests {
    
        @Autowired
        private RabbitAdmin rabbitAdmin;
    
        @Test
        public void testAdmin(){
            //声明交换机
            rabbitAdmin.declareExchange(new DirectExchange("admin.direct",false,false));
            //声明队列
            rabbitAdmin.declareQueue(new Queue("admin.queue",false));
            rabbitAdmin.declareExchange(new DirectExchange("admin.topic",false,false));
            rabbitAdmin.declareQueue(new Queue("admin.topic.queue",false));
            //队列与交换机绑定
            rabbitAdmin.declareBinding(
                    new Binding("admin.queue",//Queue名
                            Binding.DestinationType.QUEUE,//QUEUE绑定
                            "admin.direct",//exchange名
                            "direct", //路由
                            new HashMap<>()));//参数
            //链式编程
            rabbitAdmin.declareBinding(
                    BindingBuilder.bind(new Queue("admin.topic.queue",false))
                    .to(new TopicExchange("admin.topic",false,false))
                    .with("topic.#")
            );
            //清空队列
            rabbitAdmin.purgeQueue("admin.topic.queue",false);
        }
    }
    

     四.声明

     使用Spring AMQP可以使用如下方式去声明Exchange、queue。

     在配置类中加入Bean的方式。

        @Bean
        public TopicExchange topicExchange(){
            return new TopicExchange("amqp.bean.topic",true,false);
        }
    
        @Bean
        public Queue queue(){
            return new Queue("queue",true) ;
        }
    
        @Bean
        public Binding binding(){
            return BindingBuilder.bind(this.queue()).to(topicExchange()).with("amqp.#");
        }
    

      运行springboot test方法,会发现控制台以及将exchange和queue创建完成。

  • 相关阅读:
    函数式编程
    JSONP
    用javascript实现base64编码器
    图片Ping
    CORS
    深入理解ajax系列第五篇——进度事件
    文件File
    深入理解ajax系列第四篇——FormData
    Blob
    深入理解ajax系列第三篇——响应解码
  • 原文地址:https://www.cnblogs.com/wwjj4811/p/13032793.html
Copyright © 2020-2023  润新知