Spring和Springboot中配置rabbitMq配置

本文详细介绍了在Spring和SpringBoot环境中如何配置及使用rabbitMQ,包括Spring中的配置、代码调用,SpringBoot中的配置、声明交换机和队列,以及消息的写入和读取操作。
摘要由CSDN通过智能技术生成

1.Spring中rabbitmq配置

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:context="http://www.springframework.org/schema/context"
       xmlns:rabbit="http://www.springframework.org/schema/rabbit"
       xmlns="http://www.springframework.org/schema/beans"
       xsi:schemaLocation="
            http://www.springframework.org/schema/beans
            http://www.springframework.org/schema/beans/spring-beans.xsd
            http://www.springframework.org/schema/context
            http://www.springframework.org/schema/context/spring-context.xsd
            http://www.springframework.org/schema/rabbit
            http://www.springframework.org/schema/rabbit/spring-rabbit-1.2.xsd">

    <!--连接服务配置-->
    <rabbit:connection-factory id="connectionFactory" addresses="${ramq.host}"
                               username="${ramq.username}" password="${ramq.password}"
                               virtual-host="${ramq.virtualhost}"/>
    <rabbit:admin connection-factory="connectionFactory"/>
    <!--本地配置-->
    <!--<rabbit:connection-factory id="connectionFactory"  addresses="127.0.0.1:5672"-->
    <!--username="guest" password="guest" />-->
    <!--<rabbit:admin connection-factory="connectionFactory" />-->
    <!--声明erp的topic交换机-->
    <rabbit:topic-exchange name="amq.topic">
        <rabbit:bindings>
   
            <!--下单-->
            <rabbit:binding pattern="xxxx.xxxx"  queue="queue.name" ></rabbit:binding>
           
        </rabbit:bindings>
    </rabbit:topic-exchange>

    <!-- 定义消费者监听器 -->
    <!-- 创建一个bean实例,bean实例中声明处理请求的类 -->
    <bean id="consumerLitener" class="xxxx.LoginMqListener"></bean>
 
    <!--下订单消息处理-->
    <bean id="orderMqListener" class="xxxx.OrderMqListener"/>
   
    <!--定义相关queue beg-->
   
    <!--下单-->
    <rabbit:queue name="queue.name"   durable="true" auto-delete="false" exclusive="false"  />
  
    <!--定义相关queue end-->

    <rabbit:listener-container
            connection-factory="connectionFactory" acknowledge="auto" message-converter="jackson2JsonMessageConverter">
        <!-- queues属性从那个队列中接收消息,ref属性是当存在消息是使用哪个类去处理 -->
        <!--<rabbit:listener queues="queue_dengsz1" ref="consumerLitener"/>-->
        <!--区分topic订阅 method="listenOrderMsg"-->
     
        <!--订单事件监听-->
        <rabbit:listener  queue-names="queue.name"
                          ref="orderMqListener"/>
       
    </rabbit:listener-container>
    <!-- 订阅登陆消息队列 end   -->


    <!-- 定义消息转换器 -->
    <bean id="jackson2JsonMessageConverter"
          class="org.springframework.amqp.support.converter.Jackson2JsonMessageConverter"/>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值