生產者:配置文件
<?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>