• spring-amqp 动态创建queue、exchange、binding


    pom.xml

    <!-- mq 依赖 -->
            <dependency>
                <groupId>com.rabbitmq</groupId>
                <artifactId>amqp-client</artifactId>
                <version>3.6.2</version>
            </dependency>
            <dependency>
                <groupId>org.springframework.amqp</groupId>
                <artifactId>spring-amqp</artifactId>
                <version>1.6.0.RELEASE</version>
                <scope>compile</scope>
                <exclusions>
                    <exclusion>
                        <groupId>org.springframework</groupId>
                        <artifactId>spring-core</artifactId>
                    </exclusion>
                </exclusions>
            </dependency>
            <dependency>
                <groupId>org.springframework.amqp</groupId>
                <artifactId>spring-rabbit</artifactId>
                <version>1.6.0.RELEASE</version>
            </dependency>

    配置连接池

    spring-bean.xml

    <bean id="connectionFactory" class="org.springframework.amqp.rabbit.connection.CachingConnectionFactory">
            <!-- <property name="addresses" value="192.168.1.237,192.168.1.239,192.1.168.240" /> -->
            <property name="addresses" value="192.168.1.33" />
            <property name="username" value="mq"/>
            <property name="password" value="mq"/>
            <property name="channelCacheSize" value="50"/>
            <property name="publisherConfirms" value="true" />
        </bean>

    实现代码

    @Resource
        private ConnectionFactory connectionFactory;
    
    public void createMq(String msgQueue, String exchage) throws Exception {
            // set up the queue, exchange, binding on the broker
            RabbitAdmin admin = new RabbitAdmin(connectionFactory);
            Queue queue = new Queue(msgQueue);
            //queue
            admin.declareQueue(queue);
            //exchange 
            DirectExchange exchange = new DirectExchange(exchage);
            admin.declareExchange(exchange);
            //binding
            admin.declareBinding(
                BindingBuilder.bind(queue).to(exchange).with(""));
    
        }            
  • 相关阅读:
    学习JNA,Jnative
    JNative用法注意事项
    使用JNA替代JNI调用本地方法
    傅盛读书笔记:下一个Moonshot是什么?
    华为内部狂转好文:有关大数据,看这一篇就够了
    ws2_32.dll的妙用与删除 (禁网)
    保护颈椎重点按这三大穴位(图)
    在java中调用python方法
    在Windows中实现Java调用DLL(转载)
    java程序员,英语那点事
  • 原文地址:https://www.cnblogs.com/xujishou/p/6214726.html
Copyright © 2020-2023  润新知