zoukankan      html  css  js  c++  java
  • rabbitmq使用心得

      因为公司项目需要使用消息中间件,实现相关业务的异步处理,所有选用了rabbitmq.通过看文档,爬过一个一个坑,终于还是实现了相关功能。

      直接上配置文件:

    <?xml version="1.0" encoding="UTF-8"?>
    <beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:rabbit="http://www.springframework.org/schema/rabbit"
    xmlns:util="http://www.springframework.org/schema/util"
    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
    http://www.springframework.org/schema/rabbit http://www.springframework.org/schema/rabbit/spring-rabbit-1.0.xsd http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util.xsd">
    <bean id="connectionFactory" class="org.springframework.amqp.rabbit.connection.CachingConnectionFactory">
    <property name="username" value="${mq.userName}" />
    <property name="password" value="${mq.password}" />
    <property name="host" value="${mq.host}" />
    <property name="port" value="${mq.port}" />
    <property name="virtualHost" value="${mq.virtualHost}"/>
    <property name="channelCacheSize" value="${mq.cache.size}"/>
    </bean>

    <bean id="rabbitAdmin" class="org.springframework.amqp.rabbit.core.RabbitAdmin">
    <constructor-arg ref="connectionFactory" />
    </bean>
    <bean id="rabbitTemplate" class="org.springframework.amqp.rabbit.core.RabbitTemplate">
    <constructor-arg ref="connectionFactory"></constructor-arg>
    <property name="exchange" value="${mq.exchange}"/>
    <property name="routingKey" value="${mq.routingKey}"/>
    <property name="queue" value="${mq.queue}"/>
    </bean>


    <bean id="serializerMessageConverter" class="org.springframework.amqp.support.converter.SimpleMessageConverter"></bean>


    <bean id="queue" class="org.springframework.amqp.core.Queue">
    <constructor-arg index="0" value="${mq.queue}"></constructor-arg>
    <constructor-arg index="1" value="true"></constructor-arg>
    <constructor-arg index="2" value="false"></constructor-arg>
    <constructor-arg index="3" value="false"></constructor-arg>
    </bean>


    <bean id="directExchange" class="org.springframework.amqp.core.DirectExchange">
    <constructor-arg index="0" value="${mq.routingKey}"></constructor-arg>
    <constructor-arg index="1" value="true"></constructor-arg>
    <constructor-arg index="2" value="false"></constructor-arg>
    </bean>

    <util:map id="arguments">
    </util:map>


    <bean id="binding" class="org.springframework.amqp.core.Binding">
    <constructor-arg index="0" value="${mq.queue}"></constructor-arg>
    <constructor-arg index="1" value="QUEUE"></constructor-arg>
    <constructor-arg index="2" value="${mq.exchange}"></constructor-arg>
    <constructor-arg index="3" value="${mq.routingKey}"></constructor-arg>
    <constructor-arg index="4" value="#{arguments}"></constructor-arg>
    </bean>


    <bean id="rmqConsumer" class="com.tom.rabbitmq.MessageQueueReceiver"></bean>

    <bean id="messageListenerAdapter" class="org.springframework.amqp.rabbit.listener.adapter.MessageListenerAdapter">
    <constructor-arg ref="rmqConsumer" />
    <property name="defaultListenerMethod" value="onMessage"></property>
    <property name="messageConverter" ref="serializerMessageConverter"></property>
    </bean>

    <bean id="listenerContainer" class="org.springframework.amqp.rabbit.listener.SimpleMessageListenerContainer">
    <property name="queues" ref="queue"></property>
    <property name="connectionFactory" ref="connectionFactory"></property>
    <property name="messageListener" ref="messageListenerAdapter"></property>
    </bean>
    </beans>
      在这个配置中,我使用的exchange模式是direct,生产者发送消息到 exchange,exchange根据其生产者的routingkey,找到对应bingkey的队列queue.这样消息就会存到对应的queue中,可以把queue理解为一个存储消息的地方。消费者会从该队列中获取消息,消费消息,返回消息应答,成功,该消息从消息队列中删除。

      注意点:

      配置queue的时候,Durability设为durable,这样在rabbitmq服务端重启的时候,消息队列里面的消息不回丢失。

      配置文件中的如下,配置了在项目启动的时候,会自动去获得exchange,queue,在rabbitmq中注册。

    <bean id="rabbitAdmin"  class="org.springframework.amqp.rabbit.core.RabbitAdmin">
    <constructor-arg ref="connectionFactory" />
    </bean>


    在配置中使用的spring的CachingConnectionFactory,用它来管理rabbit的connectionFactory, 其中可以设置其的channelCacheSize,默认是1.这个相当于建立一个channel的缓存池,
    channel的作用类似于session.一个connection可以创建多个channel.
    在该例子中使用SimpleMessageListenerContainer来管理消费者。
  • 相关阅读:
    WCF 第二章 契约
    WCF 第二章 契约 两个单向契约VS一个双向契约
    WCF 第二章 契约 数据契约等效
    WCF 第二章 契约 双向操作
    WCF 第二章 契约 服务契约
    Java环境变量配置(Mac)
    java使用document解析xml文件
    使用JDOM解析xml文档
    使用SAX解析xml文档
    sharepoint 2007服务器场迁移到 64 位环境 ^
  • 原文地址:https://www.cnblogs.com/tom-plus/p/5796880.html
Copyright © 2011-2022 走看看