您好,登錄后才能下訂單哦!
怎么進(jìn)行消息隊(duì)列RabbitMQ與Spring集成的分析,針對(duì)這個(gè)問題,這篇文章詳細(xì)介紹了相對(duì)應(yīng)的分析和解答,希望可以幫助更多想解決這個(gè)問題的小伙伴找到更簡(jiǎn)單易行的方法。
RabbitMQ是流行的開源消息隊(duì)列系統(tǒng),用erlang語言開發(fā)。RabbitMQ是AMQP(高級(jí)消息隊(duì)列協(xié)議)的標(biāo)準(zhǔn)實(shí)現(xiàn)。
//pom.xml <dependency> <groupId>com.rabbitmq</groupId> <artifactId>amqp-client</artifactId> <version>3.5.1</version> </dependency> <dependency> <groupId>org.springframework.amqp</groupId> <artifactId>spring-rabbit</artifactId> <version>1.4.5.RELEASE</version> </dependency>
//rabbitmq-config.properties mq.host=127.0.0.1 mq.username=test mq.password=123456 mq.port=5672 mq.vhost=testmq
//application-mq.xml <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/rabbit http://www.springframework.org/schema/rabbit/spring-rabbit-1.0.xsd" > <description>rabbitmq 連接服務(wù)配置</description> <!-- 連接配置 --> <rabbit:connection-factory id="connectionFactory" host="${mq.host}" username="${mq.username}" password="${mq.password}" port="${mq.port}" virtual-host="${mq.vhost}"/> <rabbit:admin connection-factory="connectionFactory"/> <!-- spring template聲明--> <rabbit:template exchange="amqpExchange" id="amqpTemplate" connection-factory="connectionFactory" message-converter="jsonMessageConverter" /> <!-- 消息對(duì)象json轉(zhuǎn)換類 --> <bean id="jsonMessageConverter" class="org.springframework.amqp.support.converter.Jackson2JsonMessageConverter" /> </beans>
//application-mq.xml <rabbit:queue id="test_queue_key" name="test_queue_key" durable="true" auto-delete="false" exclusive="false" />
說明:
durable:是否持久化
exclusive: 僅創(chuàng)建者可以使用的私有隊(duì)列,斷開后自動(dòng)刪除
auto_delete: 當(dāng)所有消費(fèi)客戶端連接斷開后,是否自動(dòng)刪除隊(duì)列
//application-mq.xml <rabbit:direct-exchange name="test-mq-exchange" durable="true" auto-delete="false" id="test-mq-exchange"> <rabbit:bindings> <rabbit:binding queue="test_queue_key" key="test_queue_key"/> </rabbit:bindings> </rabbit:direct-exchange>
說明:
rabbit:direct-exchange:定義exchange模式為direct,意思就是消息與一個(gè)特定的路由鍵完全匹配,才會(huì)轉(zhuǎn)發(fā)。
rabbit:binding:設(shè)置消息queue匹配的key
//MQProducer.java public interface MQProducer { /** * 發(fā)送消息到指定隊(duì)列 * @param queueKey * @param object */ public void sendDataToQueue(String queueKey, Object object); }
@Service public class MQProducerImpl implements MQProducer { @Autowired private AmqpTemplate amqpTemplate; private final static Logger LOGGER = Logger.getLogger(MQProducerImpl.class); /* (non-Javadoc) * @see com.stnts.tita.rm.api.mq.MQProducer#sendDataToQueue(java.lang.String, java.lang.Object) */ @Override public void sendDataToQueue(String queueKey, Object object) { try { amqpTemplate.convertAndSend(queueKey, object); } catch (Exception e) { LOGGER.error(e); } } }
說明:
convertAndSend:將Java對(duì)象轉(zhuǎn)換為消息發(fā)送到匹配Key的交換機(jī)中Exchange,由于配置了JSON轉(zhuǎn)換,這里是將Java對(duì)象轉(zhuǎn)換成JSON字符串的形式。
原文:Convert a Java object to an Amqp Message and send it to a default exchange with a specific routing key.
定義監(jiān)聽器
//QueueListenter.java @Component public class QueueListenter implements MessageListener { @Override public void onMessage(Message msg) { try{ System.out.print(msg.toString()); }catch(Exception e){ e.printStackTrace(); } } }
監(jiān)聽配置
//application-mq.xml <rabbit:listener-container connection-factory="connectionFactory" acknowledge="auto"> <rabbit:listener queues="test_queue_key" ref="queueListenter"/> </rabbit:listener-container>
說明:
queues:監(jiān)聽的隊(duì)列,多個(gè)的話用逗號(hào)(,)分隔
ref:監(jiān)聽器
//TestQueue.java @RunWith(value = SpringJUnit4ClassRunner.class) @ContextConfiguration(locations = { "classpath:/ApplicationContext/ApplicationContext-mq.xml"}) public class TestQueue{ @Autowired MQProducer mqProducer; final String queue_key = "test_queue_key"; @Test public void send(){ Map<String,Object> msg = new HashMap()<>; msg.put("data","hello,rabbmitmq!"); mqProducer.sendDataToQueue(query_key,msg); } }
運(yùn)行測(cè)試程序,Run with JUnit,會(huì)發(fā)送一條消息到test_queue,監(jiān)聽器監(jiān)聽到消息后,打印出消息。
至此,已經(jīng)完成了Spring和RabbmitMQ集成,配置,和使用。
關(guān)于怎么進(jìn)行消息隊(duì)列RabbitMQ與Spring集成的分析問題的解答就分享到這里了,希望以上內(nèi)容可以對(duì)大家有一定的幫助,如果你還有很多疑惑沒有解開,可以關(guān)注億速云行業(yè)資訊頻道了解更多相關(guān)知識(shí)。
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。