Spring 配置RabbitMQ
Spring 配置RabbitMQ
yan5845hao 发表于7个月前
Spring 配置RabbitMQ
  • 发表于 7个月前
  • 阅读 20
  • 收藏 0
  • 点赞 0
  • 评论 0

腾讯云 技术升级10大核心产品年终让利>>>   

需要使用的包

 <dependency>
     <groupId>org.springframework.amqp</groupId>
     <artifactId>spring-amqp</artifactId>
     <version>1.0.0.RELEASE</version>
</dependency>
<dependency>
       <groupId>org.springframework.amqp</groupId>
       <artifactId>spring-rabbit</artifactId>
       <version>1.0.0.RELEASE</version>
</dependency>

fanout交换器配置

    <!-- 连接rabbitmq-->
    <rabbit:connection-factory id="connectionFactory"
        host="10.211.55.8" port="5672"
        username="uwo" password="yan5845hao"
        virtual-host="/"/>
    
    <rabbit:admin connection-factory="connectionFactory"/>

    <!-- 声明队列-->
    <rabbit:queue name="uwo.rabbitMQ.test" durable="true" auto-delete="false" exclusive="false" />
    <!--Fanout: 不管消息的routing key和参数表的头信息/值是什么, 消息将会路由到该队列上。-->
    <rabbit:fanout-exchange name="uwo.fanout" durable="true" auto-delete="false">
        <rabbit:bindings>
            <rabbit:binding queue="uwo.rabbitMQ.test"/>
        </rabbit:bindings>
    </rabbit:fanout-exchange>

    <!--定义rabbit template用于数据的接收和发送-->
    <rabbit:template id="rabbitTemplate"  exchange="uwo.fanout" connection-factory="connectionFactory"/>

    <!-- 监听的队列-->
    <rabbit:listener-container connection-factory="connectionFactory" acknowledge="auto">
        <rabbit:listener ref="listen"  method="listen" queue-names="uwo.rabbitMQ.test" />
    </rabbit:listener-container>

    <!-- 监听器-->
    <bean id="listen" class="com.uwo.resources.ui.mq.QueueListener"/>

topic交换器配置

    <!-- 连接rabbitmq-->
    <rabbit:connection-factory id="connectionFactory"
        host="10.211.55.8" port="5672"
        username="uwo" password="yan5845hao"
        virtual-host="/"/>
    
    <rabbit:admin connection-factory="connectionFactory"/>

    <!-- 声明队列-->
    <rabbit:queue name="uwo.rabbitMQ.test" durable="true" auto-delete="false" exclusive="false" />
    <!--Fanout: 不管消息的routing key和参数表的头信息/值是什么, 消息将会路由到该队列上。-->
    <rabbit:topic-exchange name="uwo.topic" durable="true" auto-delete="false">
        <rabbit:bindings>
            <rabbit:binding queue="uwo.rabbitMQ.test" pattern="#.asm" />
            <rabbit:binding queue="uwo.rabbitMQ.test" pattern="#.aspectj" />
        </rabbit:bindings>
    </rabbit:topic-exchange>

    <!--定义rabbit template用于数据的接收和发送-->
    <rabbit:template id="rabbitTemplate"  exchange="uwo.topic" connection-factory="connectionFactory"/>

    <!-- 监听的队列-->
    <rabbit:listener-container connection-factory="connectionFactory" acknowledge="auto">
        <rabbit:listener ref="listen"  method="listen" queue-names="uwo.rabbitMQ.test" />
    </rabbit:listener-container>

    <!-- 监听器-->
    <bean id="listen" class="com.uwo.resources.ui.mq.QueueListener"/>

direct交换器配置

    <!-- 连接rabbitmq-->
    <rabbit:connection-factory id="connectionFactory"
        host="10.211.55.8" port="5672"
        username="uwo" password="yan5845hao"
        virtual-host="/"/>
    
    <rabbit:admin connection-factory="connectionFactory"/>

    <!-- 声明队列-->
    <rabbit:queue name="uwo.rabbitMQ.test" durable="true" auto-delete="false" exclusive="false" />
    <!--Fanout: 不管消息的routing key和参数表的头信息/值是什么, 消息将会路由到该队列上。-->
    <rabbit:direct-exchange name="uwo.direct" durable="true" auto-delete="false">
        <rabbit:bindings>
            <rabbit:binding queue="uwo.rabbitMQ.test" key="com.uwo.asm"></rabbit:binding>
            <rabbit:binding queue="uwo.rabbitMQ.test" key="com.uwo.aspectj"></rabbit:binding>
        </rabbit:bindings>
    </rabbit:direct-exchange>

    <!--定义rabbit template用于数据的接收和发送-->
    <rabbit:template id="rabbitTemplate"  exchange="uwo.direct" connection-factory="connectionFactory"/>

    <!-- 监听的队列-->
    <rabbit:listener-container connection-factory="connectionFactory" acknowledge="auto">
        <rabbit:listener ref="listen"  method="listen" queue-names="uwo.rabbitMQ.test" />
    </rabbit:listener-container>

    <!-- 监听器-->
    <bean id="listen" class="com.uwo.resources.ui.mq.QueueListener"/>

headers交换器配置

    <!-- 连接rabbitmq-->
    <rabbit:connection-factory id="connectionFactory"
        host="10.211.55.8" port="5672"
        username="uwo" password="yan5845hao"
        virtual-host="/"/>
    
    <rabbit:admin connection-factory="connectionFactory"/>

    <!-- 声明队列-->
    <rabbit:queue name="uwo.rabbitMQ.test" durable="true" auto-delete="false" exclusive="false" />
    <!--Fanout: 不管消息的routing key和参数表的头信息/值是什么, 消息将会路由到该队列上。-->
    <rabbit:headers-exchange name="uwo.headers" durable="true" auto-delete="false">
        <rabbit:bindings>
            <rabbit:binding queue="uwo.rabbitMQ.test" key="uwo" value="yanhao"></rabbit:binding>
            <rabbit:binding queue="uwo.rabbitMQ.test" key="YH" value="uwowu"></rabbit:binding>
        </rabbit:bindings>
    </rabbit:headers-exchange>

    <!--定义rabbit template用于数据的接收和发送-->
    <rabbit:template id="rabbitTemplate"  exchange="uwo.headers" connection-factory="connectionFactory"/>

    <!-- 监听的队列-->
    <rabbit:listener-container connection-factory="connectionFactory" acknowledge="auto">
        <rabbit:listener ref="listen"  method="listen" queue-names="uwo.rabbitMQ.test" />
    </rabbit:listener-container>

    <!-- 监听器-->
    <bean id="listen" class="com.uwo.resources.ui.mq.QueueListener"/>
共有 人打赏支持
粉丝 7
博文 102
码字总数 20648
×
yan5845hao
如果觉得我的文章对您有用,请随意打赏。您的支持将鼓励我继续创作!
* 金额(元)
¥1 ¥5 ¥10 ¥20 其他金额
打赏人
留言
* 支付类型
微信扫码支付
打赏金额:
已支付成功
打赏金额: