RabbitMQ (十) Spring整合RabbitMQ(2)
阿新 • • 發佈:2019-02-03
1、訊息生產者
package com.lin.producer; import javax.annotation.Resource; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.amqp.core.AmqpTemplate; import org.springframework.stereotype.Service; /** * 功能概要:訊息產生,提交到佇列中去 * * @author linbingwen * @since 2016年1月15日 */ @Service public class MessageProducer { private Logger logger = LoggerFactory.getLogger(MessageProducer.class); @Resource private AmqpTemplate amqpTemplate; public void sendMessage(Object message){ logger.info("to send message:{}",message); amqpTemplate.convertAndSend("queueTestKey",message); } }
2、訊息消費者
package com.lin.consumer; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.amqp.core.Message; import org.springframework.amqp.core.MessageListener; /** * 功能概要:消費接收 * * @author linbingwen * @since 2016年1月15日 */ public class MessageConsumer implements MessageListener { private Logger logger = LoggerFactory.getLogger(MessageConsumer.class); @Override public void onMessage(Message message) { logger.info("receive message:{}",message); } }
3、rabbitMq.xml配置資訊
<?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="asdf" password="123456" host="10.75.4.25" 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> </beans>
4、spring整合rabbiqMq。application.xml內容如下:
<?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:context="http://www.springframework.org/schema/context"
xmlns:p="http://www.springframework.org/schema/p"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.1.xsd">
<import resource="classpath*:rabbitmq.xml" />
<!-- 掃描指定package下所有帶有如@controller,@services,@resource,@ods並把所註釋的註冊為Spring Beans -->
<context:component-scan base-package="com.lin.consumer,com.lin.producer" />
<!-- 啟用annotation功能 -->
<context:annotation-config />
<!-- 啟用annotation功能 -->
<context:spring-configured />
</beans>