RabbitMQ快速上手(三)Spring集成RabbitMQ

我们前两篇文章介绍了RabbitMQ消息的生产与消费,但是难免过于繁琐,Spring提供了对RabbitMQ的集成,我们来说一下

公共部分

1.connectionFactory,其中的属性可以从properties文件读取

  <!-- 连接服务配置 如果MQ服务器在远程服务器上,请新建用户用新建的用户名密码  guest默认不允许远程登录-->
    <rabbit:connection-factory id="connectionFactory"
                               host="localhost" username="guest" password="guest" port="5672"
                               virtual-host="/" channel-cache-size="5" />

    <!-- 配置爱admin,自动根据配置文件生成交换器和队列,无需手动配置 -->
    <rabbit:admin connection-factory="connectionFactory" />

2.声明队列、交换机以及相关绑定规则

  • durable:是否持久化
  • auto-delete:自动删除
  • pattern:过滤规则
  • key:routing Key

direct模式

  <!--定义direct-exchange -->
    <rabbit:direct-exchange name="mq.qwerExChange" durable="true" auto-delete="false">
       <rabbit:bindings>
           <rabbit:binding queue="mq.qwer" key="mq.qwer.send" ></rabbit:binding>
        </rabbit:bindings>

    </rabbit:direct-exchange>

fanout模式

 <!--定义消息队列-->
    <rabbit:queue name="spittle.alert.queue.1" durable="true" auto-delete="false"/>
    <rabbit:queue name="spittle.alert.queue.2" durable="true" auto-delete="false"/>
    <rabbit:queue name="spittle.alert.queue.3" durable="true" auto-delete="false"/>

    <!--绑定队列-->
    <rabbit:fanout-exchange id="spittle.fanout" name="spittle.fanout" durable="true">
        <rabbit:bindings>
            <rabbit:binding queue="spittle.alert.queue.1"></rabbit:binding>
            <rabbit:binding queue="spittle.alert.queue.2"></rabbit:binding>
            <rabbit:binding queue="spittle.alert.queue.3"></rabbit:binding>
        </rabbit:bindings>
    </rabbit:fanout-exchange>

topic模式

 <!--定义queue  说明:durable:是否持久化 exclusive: 仅创建者可以使用的私有队列,断开后自动删除 auto_delete: 当所有消费客户端连接断开后,是否自动删除队列-->
    <rabbit:queue name="mq.asdf" durable="true" auto-delete="false" exclusive="false" />
    <rabbit:queue name="mq.asdf2" durable="true" auto-delete="false" exclusive="false" />
    <rabbit:queue name="mq.qwer" durable="true" auto-delete="false" exclusive="false" />
    <!--定义topic-exchange -->
    <rabbit:topic-exchange name="mq.asdfExChange"
                           durable="true" auto-delete="false">
        <rabbit:bindings>
            <rabbit:binding queue="mq.asdf" pattern="mq.asdf.*"></rabbit:binding>
            <rabbit:binding queue="mq.asdf2" pattern="mq.asdf2.#"></rabbit:binding>
            <rabbit:binding queue="mq.asdf2" pattern="mq.asdf.send"></rabbit:binding>
        </rabbit:bindings>
    </rabbit:topic-exchange>

生产者

配置Template

 <!--创建消息队列模板-->
    <rabbit:template id="rabbitTemplate" connection-factory="connectionFactory"
                     exchange="spittle.fanout" message-converter="jsonMessageConverter">
    </rabbit:template>
   <bean id="jsonMessageConverter" class="org.springframework.amqp.support.converter.JsonMessageConverter"></bean>

消费者

需要指定队列以及队列监听实现类
direct模式

 <bean id="qwerConsumer" class="com.demo.action.QwerConsumer"></bean>
    <rabbit:listener-container connection-factory="connectionFactory" >
        <rabbit:listener  queues="mq.qwer"  ref="qwerConsumer"/>
    </rabbit:listener-container>

fanout模式

  <rabbit:listener-container connection-factory="connectionFactory">
      <rabbit:listener ref="spittleListener" method="onMessage" queues="spittle.alert.queue.1,spittle.alert.queue.3,spittle.alert.queue.2"/>
    </rabbit:listener-container>
    <bean id="spittleListener" class="com.lp.summary.rabbitmq.impl.SpittleAlertHandler"/>

topic模式

  <!-- 消息接收者 -->

    <bean id="asdfConsumer" class="com.demo.action.AsdfConsumer"></bean>
    <bean id="asdfConsumer2" class="com.demo.action.AsdfConsumer2"></bean>

    <!-- queue litener 观察 监听模式 当有消息到达时会通知监听在对应的队列上的监听对象 -->
    <rabbit:listener-container connection-factory="connectionFactory" >
        <rabbit:listener  queues="mq.asdf"  ref="asdfConsumer"/>
    </rabbit:listener-container>

    <rabbit:listener-container connection-factory="connectionFactory" >
        <rabbit:listener  queues="mq.asdf2"  ref="asdfConsumer2"/>
    </rabbit:listener-container>

其中spittleListener是监听的程序,method是执行的方法,queues是我们监听的队列,多个队列可以逗号隔开(因为我们采用的是分发,所以三个队列获取的消息是相同的,这里为了简便我放在一个监听程序中了,其实我们可以写三个消费者,每个消费者监听一个队列)

生产者采用发送convertAndSend消息,其中convertAndSend方法默认第一个参数是交换机名称,第二个参数是路由名称,第三个才是我们发送的数据

    amqpTemplate.convertAndSend("mq.asdfExChange", "mq.asdf.send", "hello world");
    amqpTemplate.convertAndSend("mq.asdfExChange", "mq.asdf2.send", "这个世界很奇妙!!!");
    amqpTemplate.convertAndSend("mq.qwerExChange", "mq.qwer.send", "神奇的世界!!!");

消费者实现MessageListener 接口实现监听

public class SpittleAlertHandler implements MessageListener {
    @Override
    public void onMessage(Message message) {
        try {
            String body=new String(message.getBody(),"UTF-8");
            System.out.println(body);
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
    }
}

一定要注意实现MessageListener,我们只需要获取message的body即可

文章参考
http://www.cnblogs.com/LipeiNet/p/6079427.html
https://www.cnblogs.com/tohxyblog/p/7256554.html
https://blog.csdn.net/u012204058/article/details/54292888
https://blog.csdn.net/xun573017588/article/details/77099535

猜你喜欢

转载自blog.csdn.net/yz357823669/article/details/81507232
今日推荐