1. 程式人生 > >Spring整合rabbitmq---訊息接收

Spring整合rabbitmq---訊息接收

這兩天一直在看rabbimq訊息接收的設定。看到網上也有很多的例子,但是發現很多別人可以,到本地自己就不可以,也是比較讓人苦惱,在前面的一篇文章中我們介紹了Spring-rabbitmq訊息的傳送,繼續上一節中我們繼續看訊息的接收。

首先來看rabbitmq-receiver.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"
	xmlns:context="http://www.springframework.org/schema/context"
	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.6.xsd
    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
    http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task-3.1.xsd">

	<description>rabbitmq 連線服務配置</description>

	<!-- 基於註解的根包掃描路徑 -->
	<!-- <context:component-scan base-package="com.wdg.amqp" /> -->


	<!-- 訊息物件json轉換類 -->
	<bean id="jsonMessageConverter"
		class="org.springframework.amqp.support.converter.Jackson2JsonMessageConverter"></bean>

	<!-- 連線配置 -->
	<!-- 定義RabbitMQ的連線工廠 -->
	<rabbit:connection-factory id="connectionFactory"
		host="127.0.0.1" port="5672" username="guest" password="guest"
		virtual-host="/" />

	<rabbit:admin connection-factory="connectionFactory" />

	<!-- spring template宣告 -->
	<rabbit:template id="amqpTemplate" exchange="wdgexchange"
		connection-factory="connectionFactory" message-converter="jsonMessageConverter" />

	<!-- 宣告一個Que -->
	<!-- durable:是否持久化 ; exclusive: 僅建立者可以使用的私有佇列,斷開後自動刪除; auto_delete: 當所有消費客戶端連線斷開後,是否自動刪除佇列 -->
	<rabbit:queue id="wdgqueue" name="wdgqueue" durable="true"
		auto-delete="false" exclusive="false" />

	<!-- 宣告一個Exchange -->
	<rabbit:direct-exchange name="wdgexchange"
		durable="true" auto-delete="false" id="wdgexchange">
		<rabbit:bindings>
			<rabbit:binding queue="wdgqueue" key="*" />
		</rabbit:bindings>
	</rabbit:direct-exchange>
	
	<rabbit:annotation-driven />  
	
	<!-- 訊息接受者 -->
	<bean id="receiverListen" class="com.wdg.amqp.AmqpReceiver"></bean>
	<!-- 用於訊息的監聽的代理類MessageListenerAdapter -->    
    <bean id="receiveListenerAdapter"    
        class="org.springframework.amqp.rabbit.listener.adapter.MessageListenerAdapter">    
         <constructor-arg ref="receiverListen" />  
        <property name="defaultListenerMethod" value="handleMessage"></property>    
        <property name="messageConverter" ref="jsonMessageConverter"></property>    
    </bean>   
	
	
	 <bean id="listenerContainer"    
        class="org.springframework.amqp.rabbit.listener.SimpleMessageListenerContainer"> 
        <property name="queueNames" value="wdgqueue"></property>   
        <property name="connectionFactory" ref="connectionFactory"></property>    
        <property name="messageListener" ref="receiveListenerAdapter"></property>    
    </bean>
	
	


</beans>

這個是我們的xml的配置,我們來看看訊息接收器的java程式碼:

package com.wdg.amqp;

import org.springframework.stereotype.Component;
import org.springframework.stereotype.Service;

@Component
@Service
public class AmqpReceiver{

	
	public void handleMessage(String message) {
		System.out.println(message.toString());
		
	}
	

}

消費者啟動:

package com.wdg.amqp;

import org.springframework.context.support.ClassPathXmlApplicationContext;

public class RabbitMqClient {

	public static void main(String[] args) {
           new ClassPathXmlApplicationContext("rabbitmq-receiver.xml");

	}
}

上面就是整個接收器的過程,下面我們在rabbimq中推送一個訊息:

 

我們看控制檯:

上面就是rabbitmq訊息的接收了,希望對你有所幫助,接下來我們會繼續講在配置中的一些細節,如果感覺還不錯,可以掃一下紅包哦