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(""));

    }            
原文地址:https://www.cnblogs.com/xujishou/p/6214726.html