Spring web集成rabbitmq代碼實例
這篇文章主要介紹了Spring web集成rabbitmq代碼實例,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友可以參考下
引入java包:
本項目中僅引入了四個java包:amqp-client-5.7.3.jar,spring-rabbit-2.2.2.RELEASE.jar,spring-retry-1.2.4.RELEASE.jar,spring-amqp-2.2.2.RELEASE.jar
spring-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.xsd http://www.springframework.org/schema/rabbit http://www.springframework.org/schema/rabbit/spring-rabbit.xsd">
<!--配置connection-factory,指定連接rabbit server參數(shù) -->
<rabbit:connection-factory id="connectionFactory"
username="guest" password="guest" host="localhost" port="5672" />
<!--定義rabbit template用于數(shù)據(jù)的發(fā)送 -->
<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="club.codeapes.web.core.rabbitmq.RabbitMqMessageConsumer"></bean>
<!-- queue litener 觀察 監(jiān)聽模式 當有消息到達時會通知監(jiān)聽在對應的隊列上的監(jiān)聽對象-->
<rabbit:listener-container connection-factory="connectionFactory">
<rabbit:listener queues="queueTest" ref="messageReceiver"/>
</rabbit:listener-container>
</beans>
spring中需要引入這個xml, 主要在總spring.xml?;蛘遷eb.xml中需要引入下。
RabbitMqMessageConsumer
package club.codeapes.web.core.rabbitmq;
import club.codeapes.common.date.DateUtil;
import com.alibaba.fastjson.JSON;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.core.MessageListener;
public class RabbitMqMessageConsumer implements MessageListener {
@Override
public void onMessage(Message message) {
System.out.println("消費信息," + DateUtil.getNow("yyyy-MM-dd HH:mm:ss") + "---->" + message);
}
}
RabbitMqMessageProducer:
package club.codeapes.web.core.rabbitmq;
import org.springframework.amqp.core.AmqpTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;
@Repository
public class RabbitMqMessageProducer{
@Autowired
private AmqpTemplate amqpTemplate;
public void sendMessage(Object message) {
System.out.println("to send message:" + message);
amqpTemplate.convertAndSend("queueTestKey", message);
}
}
以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。
相關文章
Struts2中validate數(shù)據(jù)校驗的兩種方法詳解附Struts2常用校驗器
這篇文章主要介紹了Struts2中validate數(shù)據(jù)校驗的兩種方法及Struts2常用校驗器,本文介紹的非常詳細,具有參考借鑒價值,感興趣的朋友一起看看吧2016-09-09
java中split()方法以及常見算法經(jīng)典案例
這篇文章主要介紹了java中split()方法以及常見算法的相關資料,split()方法可以根據(jù)指定的正則表達式將字符串分割成多個子字符串,并返回一個字符串數(shù)組,文中通過代碼介紹的非常詳細,需要的朋友可以參考下2025-04-04
Spring中bean的生命周期之getSingleton方法
今天給大家?guī)淼氖顷P于Spring的相關知識,文章圍繞著Spring中bean的生命周期之getSingleton方法展開,文中有非常詳細的介紹及代碼示例,需要的朋友可以參考下2021-06-06
Java foreach循環(huán)是否可以修改數(shù)據(jù)的值問題解決方法
最近在做項目的時候,需要修改一個數(shù)組里面各個元素的值,foreach循環(huán)迭代數(shù)組元素時,不能改變數(shù)組元素的值,這篇文章給大家介紹Java foreach循環(huán)是否可以修改數(shù)據(jù)的值的問題及解決方法,感興趣的朋友一起看看吧2024-02-02

