生产者:配置文件

<?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"
    xsi:schemaLocation="http://www.springframework.org/schema/beans
     http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
     http://www.springframework.org/schema/beans
     http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
     http://www.springframework.org/schema/rabbit
     http://www.springframework.org/schema/rabbit/spring-rabbit-1.0.xsd">
    <!--配置connection-factory,指定连接rabbit server参数 -->
    <rabbit:connection-factory id="connectionFactory" username="" password="" host="120.24.247.47" port="5672" />
    <!--定义rabbit template用于数据的接收和发送 配置连接工厂 设置任务key 设置交换机名称 -->
    <rabbit:template id="amqpTemplate" connection-factory="connectionFactory" exchange="eOrderWlwFx" routing-key="routing-key" />
    <!--定义queue -->
    <!-- <rabbit:queue name="myQueue0" /> -->
     <rabbit:queue name="myQueue1" />
     <rabbit:queue name="myQueue2" />
     <rabbit:queue name="myQueue3" />
     <rabbit:queue name="myQueue4" />
       
     <!--根据任务key 进行绑定队列 pattern 匹配内容-->
    <rabbit:topic-exchange name="myExchange">
        <rabbit:bindings>
            <rabbit:binding queue="myQueue1" pattern="foo1" />
            <rabbit:binding queue="myQueue2" pattern="foo2" />
            <rabbit:binding queue="myQueue3" pattern="foo3" />
            <rabbit:binding queue="myQueue4" pattern="foo4" />
        </rabbit:bindings>
    </rabbit:topic-exchange>
    <!--通过指定下面的admin信息,当前producer中的exchange和queue会在rabbitmq服务器上自动生成 -->
    <rabbit:admin connection-factory="connectionFactory" />
</beans>

消息发送方法

// exchange 指定交换机  routingKey 设置任务key用于绑定队列  object 任务消息内容

public void sendMessage(String exchange, String routingKey, Object object) {
        amqpTemplate.convertAndSend(exchange, routingKey, object);
    }

消费者

<?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"
    xsi:schemaLocation="http://www.springframework.org/schema/beans
     http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
     http://www.springframework.org/schema/beans
     http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
     http://www.springframework.org/schema/rabbit
     http://www.springframework.org/schema/rabbit/spring-rabbit-1.0.xsd">
      <rabbit:connection-factory id="connectionFactory" username="guest" password="guest" host="127.0.0.1" port="5672" />
   
    <rabbit:template id="amqpTemplate" connection-factory="connectionFactory"/>

<!-- 指定队列执行的方法 这里也可以指定不同的bean -->
<rabbit:listener-container connection-factory="connectionFactory" >
    <rabbit:listener ref="Listener" method="method1" queue-names="myQueue1"  /> 
    <rabbit:listener ref="Listener" method="method2" queue-names="myQueue2"  />
    <rabbit:listener ref="Listener" method="method3" queue-names="myQueue3"  />
    <rabbit:listener ref="Listener" method="method4" queue-names="myQueue4"  />
</rabbit:listener-container>
<bean id="Listener" class="ListenerClass" />
</beans>

<?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"
    xsi:schemaLocation="http://www.springframework.org/schema/beans
     http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
     http://www.springframework.org/schema/beans
     http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
     http://www.springframework.org/schema/rabbit
     http://www.springframework.org/schema/rabbit/spring-rabbit-1.0.xsd">
    <!--配置connection-factory,指定连接rabbit server参数
    <rabbit:connection-factory id="connectionFactory"
        username="" password="" host="localhost" port="5672" />-->
        
    <!--定义rabbit template用于数据的接收和发送
    <rabbit:template id="amqpTemplate"  connection-factory="connectionFactory"
        exchange="exchangeTest" /> -->
        
    <!--通过指定下面的admin信息,当前producer中的exchange和queue会在rabbitmq服务器上自动生成
    <rabbit:admin connection-factory="connectionFactory" />-->

    <!--定义queue
    <rabbit:queue name="queueTest" durable="true" auto-delete="false" exclusive="false" />-->

    <!-- 定义direct exchange,绑定queueTest
    <rabbit:direct-exchange name="exchangeTest" durable="true" auto-delete="false">
        <rabbit:bindings>
            <rabbit:binding queue="queueTest" key="queueTestKey"></rabbit:binding>
        </rabbit:bindings>
    </rabbit:direct-exchange>-->
    
    <!-- 消息接收者
    <bean id="messageReceiver" class="com.lin.consumer.MessageConsumer"></bean> -->
    
    
    <!-- queue litener  观察 监听模式 当有消息到达时会通知监听在对应的队列上的监听对象
    <rabbit:listener-container connection-factory="connectionFactory">
             <rabbit:listener queues="queueTest" ref="messageReceiver"/>
    </rabbit:listener-container>-->
    
    
    
    <!-- <rabbit:connection-factory id="connectionFactory" username="guest" password="guest" host="120.25.106.156" port="5672" />
    -->
      <rabbit:connection-factory id="connectionFactory" username="guest" password="guest" host="120.24.247.47" port="5672" />
    
    <rabbit:template id="amqpTemplate" connection-factory="connectionFactory"/>

    

    
 <!--
<rabbit:topic-exchange name="myExchange5">
    <rabbit:bindings>
        <rabbit:binding queue="myQueue" pattern="foo.*" />
    </rabbit:bindings>
    
    <rabbit:listener ref="orderListener" method="listen" queue-names="queueLywlFx"  />
</rabbit:topic-exchange> -->

 

 

<rabbit:listener-container connection-factory="connectionFactory" >
    <rabbit:listener ref="orderListener" method="DxSingle" queue-names="DxSingle"  />  
    <rabbit:listener ref="orderListener" method="DxRecover" queue-names="DxRecover"  />
    <rabbit:listener ref="orderListener" method="DxUnsub" queue-names="DxUnsub"  />
    <rabbit:listener ref="orderListener" method="DxOrderFlow" queue-names="DxOrderFlow"  />
    <rabbit:listener ref="orderListener" method="DxFlowAlarm" queue-names="DxFlowAlarm"  />
    <rabbit:listener ref="orderListener" method="LtChangeDC" queue-names="ltChangeDC"  />
    <rabbit:listener ref="orderListener" method="LtChangeStatus" queue-names="ltChangeStatus"  />
    <rabbit:listener ref="orderListener" method="LtRecharge" queue-names="ltRecharge"  />
</rabbit:listener-container>


<bean id="orderListener" class="cn.mofiman.lywl.mq.Listener" />


</beans>

posted on 2017-04-01 11:38  秦水坡  阅读(9020)  评论(0编辑  收藏  举报