zoukankan      html  css  js  c++  java
  • 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(""));
    
        }            
  • 相关阅读:
    转载viewstate(一) 太经典的东西 不得不转载保存下来
    Asp.Net中HttpModule的研究(转)
    linux学习笔记(9)
    老手经验谈:Linux驱动程序开发学习步骤(转)
    浅谈C中的malloc和free(1)
    ASP.NET的HttpModule和HttpHandler(转)
    C语言学习笔记(1)
    xcode 4 code sense
    不是bug 是教训
    win7 管理员 开关命令
  • 原文地址:https://www.cnblogs.com/xujishou/p/6214726.html
Copyright © 2011-2022 走看看