【RabbitMQ】spring整合rabbitmq 檔案配置、demo完整示例
阿新 • • 發佈:2019-01-01
之前講了基於java配置的簡單的小demo,現在結合一下spring進行整合。
其實原理跟之前差不多的,只是改成了xml的配置。
1、建立工廠連線
<rabbit:connection-factory id="connectionFactory" host="127.0.0.1" port="5672" username="guest" password="guest"/>
這裡要注意一下,埠號,我們安裝的本地的RabbitMQ訪問的後臺web登陸埠號是15672,http://localhost:15672,但是這裡在java裡面就是5672,不一樣的,要注意一下,不然就會報連線異常的錯。
2、通過指定admin資訊,當前生產的exchange和queue資訊會在admin自動生成
<rabbit:admin connection-factory="connectionFactory"/>
3、建立佇列
<rabbit:queue name="scorpio_queue" auto-delete="false" durable="true" exclusive="false"/>
4、設定轉換器型別,並與佇列進行繫結
<rabbit:topic-exchange name="test_scorpio" durable="true" auto-delete="false">
<rabbit:bindings>
<rabbit:binding pattern="scorpio" queue="scorpio_queue"/>
</rabbit:bindings>
</rabbit:topic-exchange>
5、定義訊息模板,用於訊息的接收和傳送
<rabbit:template id="amqpTemplate" connection-factory="connectionFactory" exchange="test_scorpio" ---轉換器名稱,不是routingKey receive-timeout="10000"/>
至此,生產者配置完畢了。
下面是生產者的實現程式碼:
package com.gao.scorpio.service.mq;
/**
* @description
* @author: gaobh
* @date: 2018/4/26 14:23
* @version: v1.0
*/
public interface MqProviderService {
/**
* 生產者傳送訊息
* @param key
* @param obj
*/
void sendData(String key,Object obj);
}
package com.gao.scorpio.service.mq.impl;
import com.gao.scorpio.service.mq.MqProviderService;
import org.springframework.amqp.core.AmqpTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
/**
* @description 生產者實現類
* @author: gaobh
* @date: 2018/4/26 14:25
* @version: v1.0
*/
@Service
public class MqProviderImplService implements MqProviderService {
@Autowired
private AmqpTemplate amqpTemplate;
/**
* 生產者傳送訊息
*
* @param key
* @param obj
*/
@Override
public void sendData(String key, Object obj) {
amqpTemplate.convertAndSend(key, obj);
}
}
6、消費者配置
<bean id="mqConsumer1Service" class="com.gao.scorpio.service.mq.MqConsumer1Service"/>
<rabbit:listener-container connection-factory="connectionFactory" acknowledge="manual">
<rabbit:listener ref="mqConsumer1Service" method="onMessage" queue-names="scorpio_queue"/>
</rabbit:listener-container>
上面的引數中有一個acknowledge=“manual”,是對應答機制的配置,手動應答。
package com.gao.scorpio.service.mq;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.core.MessageListener;
/**
* @description
* @author: gaobh
* @date: 2018/4/27 16:16
* @version: v1.0
*/
public class MqConsumer1Service implements MessageListener {
@Override
public void onMessage(Message message) {
System.out.println("消費者消費:"+message.toString());
}
}
測試controller:
package com.gao.scorpio.controller.mq;
import com.gao.scorpio.service.mq.MqProviderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
/**
* @description
* @author: gaobh
* @date: 2018/4/27 10:07
* @version: v1.0
*/
@Controller
@RequestMapping("/abc")
public class MqTestController {
@Autowired
private MqProviderService mqProviderService;
/**
* @return
*/
@ResponseBody
@RequestMapping("/sendQueue")
public String testQueue() {
try {
mqProviderService.sendData("scorpio", "hello rabbitmq");
} catch (Exception e) {
e.printStackTrace();
}
return "傳送完畢";
}
}
完整的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.xsd
http://www.springframework.org/schema/rabbit
http://www.springframework.org/schema/rabbit/spring-rabbit.xsd">
<!--建立連線工廠-->
<rabbit:connection-factory id="connectionFactory"
host="127.0.0.1"
port="5672"
username="gaobh"
password="123456"/>
<!--通過指定admin資訊,當前生產的exchange和queue資訊會在admin自動生成-->
<rabbit:admin connection-factory="connectionFactory"/>
<!--定義訊息模板,用於訊息的接收和傳送-->
<rabbit:template id="amqpTemplate"
connection-factory="connectionFactory"
exchange="test_scorpio"
receive-timeout="10000"/>
<!--建立佇列-->
<rabbit:queue name="scorpio_queue" auto-delete="false" durable="true" exclusive="false"/>
<!--設定轉換器型別,並與佇列進行繫結-->
<rabbit:topic-exchange name="test_scorpio" durable="true" auto-delete="false">
<rabbit:bindings>
<rabbit:binding pattern="scorpio" queue="scorpio_queue"/>
</rabbit:bindings>
</rabbit:topic-exchange>
<!--消費者配置-->
<!--<bean id="mqConsumerImplService" class="com.gao.scorpio.service.mq.impl.MqConsumerImplService"/>-->
<bean id="mqConsumer1Service" class="com.gao.scorpio.service.mq.MqConsumer1Service"/>
<rabbit:listener-container connection-factory="connectionFactory" acknowledge="manual">
<rabbit:listener ref="mqConsumer1Service" method="onMessage" queue-names="scorpio_queue"/>
</rabbit:listener-container>
</beans>
在寫配置的時候一定要搞清楚exchange的name,routingKey,佇列這幾個引數的值,對應好別設定錯了,尤其在<rabbit:topic-exchange></rabbit:topic-exchange>這個裡面的配置,要搞清楚這幾個引數所代表的意思。