1. 程式人生 > >web專案中spring如何整合RabbitMQ?

web專案中spring如何整合RabbitMQ?

如何在Windows下安裝rabbitMQ?這個問題已在我的上一篇文章中講解清楚,不清楚的同學可以看看我的上一篇文章。 

接下來講解如何在web專案中配合spring來使用rabbitMQ?

一、若是maven工程,在pom.xml檔案中新增依賴: 若不是maven工程,需自己找jar包加入工程。
<!-- RabbitMQ -->
<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>

二、專案中新建配置檔案 spring-rabbit.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" xmlns:tx="http://www.springframework.org/schema/tx" xmlns:
util="http://www.springframework.org/schema/util" xsi:schemaLocation="http://www.springframework.org/schema/rabbit http://www.springframework.org/schema/rabbit/spring-rabbit-1.5.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd"> <context:property-placeholder location="classpath*:/META-INF/env/*.properties" /> <!--<util:properties id="appConfig" location="classpath:/META-INF/env/rabbit.properties"></util:properties>--> <!--配置connection-factory,指定連線rabbit server引數--> <rabbit:connection-factory id="connectionFactory" username="guest" password="guest" host="localhost" port="5672" virtual-host="/"/> <!-- virtual-host="/"是預設的虛擬機器路徑--> <!--通過指定下面的admin資訊,當前producer中的exchange和queue會在rabbitmq伺服器上自動生成--> <rabbit:admin connection-factory="connectionFactory"/> <!--定義queue--> <rabbit:queue id="com.mj.test" name="com.mj.test" durable="true" auto-delete="false" exclusive="false"/> <!-- 定義direct exchange,繫結com.mj.test queue --> <rabbit:direct-exchange name="myChange" durable="true" auto-delete="false"> <rabbit:bindings> <rabbit:binding queue="com.mj.test" key="testKey"></rabbit:binding> </rabbit:bindings> </rabbit:direct-exchange> <!--定義rabbit template用於資料的接收和傳送--> <rabbit:template id="amqpTemplate" connection-factory="connectionFactory" exchange="myChange" /> <bean id="messageReceiver" class="com.hp.netpro.rf.web.utils.RabbitMQConsumer"/> <rabbit:listener-container connection-factory="connectionFactory"> <rabbit:listener queues="com.mj.test" ref="messageReceiver"/> </rabbit:listener-container> </beans>
   

三、在工程的配置檔案applicationContext.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:aop="http://www.springframework.org/schema/aop"
xsi:schemaLocation="http://www.springframework.org/schema/beans
       http://www.springframework.org/schema/beans/spring-beans.xsd
       http://www.springframework.org/schema/context
       http://www.springframework.org/schema/context/spring-context.xsd
       http://www.springframework.org/schema/aop
       http://www.springframework.org/schema/aop/spring-aop.xsd">

    <import resource="classpath*:/META-INF/spring/spring-api-mybatis.xml" />
    <import resource="classpath*:/META-INF/spring/spring-rabbit.xml" />
    <!--<import resource="/META-INF/spring/spring-rabbit.xml"/>-->
    <!-- 使能AOP-->
<aop:aspectj-autoproxy/>
    <!-- 自動裝載bean使能-->
<context:component-scan base-package="com.hp.netpro.rf.web.utils"/>
    <context:annotation-config/>

    <context:property-placeholder location="classpath*:/META-INF/env/*.properties" />
    <!--<util:properties id="appConfig" location="classpath:/META-INF/env/rabbit.properties"></util:properties>-->

    <!-- 啟用annotation功能 -->
<context:spring-configured />

</beans>

四、新建訊息生產者類 RabbitMQProducer.java

packagecom.hp.netpro.rf.web.utils;importorg.springframework.amqp.core.AmqpTemplate;importorg.springframework.stereotype.Service;importjavax.annotation.Resource;/** * @Description:訊息生產 * @Author:*@CreateTime:*/@Servicepublic classRabbitMQProducer {@ResourceprivateAmqpTemplateamqpTemplate;public voidsendMessage(Object message){amqpTemplate.convertAndSend("testKey",message);//testKey為配置檔案中queue對應的key,指明發送給哪個queue。    }}

  五、新建訊息消費者 RabbitMQConsumer.java 

package com.hp.netpro.rf.web.utils;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.core.MessageListener;
import org.springframework.stereotype.Service;
/** * @Description:訊息佇列接收者 * @Author:*@CreateTime:*/@Servicepublic class RabbitMQConsumerimplementsMessageListener {public voidonMessage(Message message) {
       System.out.println("訊息!");
       System.out.println(message);
       System.out.println("訊息getMessageProperties!");
       System.out.println(message.getMessageProperties());
       System.out.println("訊息getBody");
       System.out.println(message.getBody());

    }
}


六、編寫測試方法

/** * @Description:訊息佇列 * @Author:*@CreateTime:

*/

@Resource
private RabbitMQProducer rabbitMQProducer; //注意:這裡必須以這種注入的方式 不能使用new來建立物件,

@ResponseBody@RequestMapping("/sendMessage")publicString testQueue() {try{rabbitMQProducer.sendMessage("I am amq sender");return"傳送完畢";    }catch(Exception e){        e.printStackTrace();return"傳送失敗!!";    }}

接下來啟動RabbitMQ-Server,啟動工程,瀏覽器訪問對應測試方法的RequestMapping。 結果:

相關推薦

web專案spring如何整合RabbitMQ

如何在Windows下安裝rabbitMQ?這個問題已在我的上一篇文章中講解清楚,不清楚的同學可以看看我的上一篇文章。  接下來講解如何在web專案中配合spring來使用rabbitMQ?

Maven專案Spring整合Mybatis

新增jar包依賴 spring需要的jar包依賴 <dependency> <groupId>org.springframework</groupId> <artifactId>

ssm框架web專案spring-mvc.xml解析

spring-mvc.xml配置檔案主要用於配置SpringMVC檢視解析器、控制器、部分靜態資源等。 type:Spring Bean Configuration File <?xml version="1.0" encoding="UTF-8"?> <beans xml

整理不易,且整且珍惜 2.開發環境的搭建 3.Eclipse的相關配置 4.使用maven建立web專案 5.Spring+Mybatis+SpringMVC整合 6.mybatis自動生成程式碼 7.spring與mybatis整合Junit的測試 8.maven專案的啟動 9.Restful

整理不易,且整且珍惜 2.開發環境的搭建 3.Eclipse的相關配置 4.使用maven建立web專案 5.Spring+Mybatis+SpringMVC整合 6.mybatis自動生成程式碼

Axis2與Web專案整合及Axis2在Web專案整合Spring

Axis2簡介:          Axis2是一套嶄新的WebService引擎,該版本是對Axis1.x重新設計的產物。Axis2不僅支援SOAP1.1和SOAP1.2,還集成了非常流行的REST WebService,同時還支援spring、JSON等技術。

Mondrian學習(3):整合spring web專案

    前面講了通過類似jdbc程式設計的方式來使用mondrian多維分析工具,顯然有很多弊端。    1.這種方式通過字串中的key-value把資料庫連線,模型檔案,資料庫驅動都寫死了。並且每次獲取一個連線都要getconnetion一次。    2.一般的java w

webservice之cxf實現[web專案基於maven與spring整合]

webservice現開發的已實現元件不少,使用過的就有xfire及cxf. cxc基於maven與spring整合過程如下: STEP 1. 依賴包新增 在pom.xml檔案的標籤中增加如下(版本號依個人需要調整): <depende

Web專案配置Spring的IOC容器

如果需要在Web專案中使用Spring的IOC容器,可以在Web專案配置檔案web.xml中做出如下配置: <listener> <listener-class>org.springframework.web.context.Contex

如何在Web專案配置Spring的IOC容器?

如何在Web專案中配置Spring的IOC容器? 如果需要在Web專案中使用Spring的IOC容器,可以在Web專案配置檔案web.xml中做出如下配置: <listener> <listener-class>org.springframework.w

如何在spring-boot web專案啟用swagger

swagger的三個專案及其作用 我們開啟swagger的官網,會發現有三個swagger相關的專案,它們分別是 swagger-editor 作用是通過寫程式碼,生成文件描述(一個json檔案或其他格式的api元資料檔案) swagger-ui 通過請求文件描述(一個json檔案)的資料,把a

Spring(1)之 (1.4_Spring WEBWeb 專案使用 Spring

1. Spring負責物件的建立( 控制反轉 IOC),處理物件之間的依賴關係(依賴注入 DI) 2. Spring在 WEB應用中的使用:整合Mybatis、Hibernate、SpringMVC、Struts 3. 使用步驟: 引入 jar包: spring-core

Spring 之在Web專案使用

Spring 在Web中的第一次應用 1、配置Web環境,或者下載有Web外掛的eclipse 2、建立Web專案,選擇Dynamic Web Project(動態Web專案,對應的靜態Web專案 Static Web Project) 3、匯入jar包 commons-log

springweb專案配置druid監控

1.建立spring的web專案 2.配置好spring相關基礎配置,確保服務可正常執行即可   3.在spring配置檔案中配置好druid資料來源,druid的jar包要正常引入 4.在web.xml中加入如下配置     <filter> &n

Spring整合Rabbitmq收集Logback日誌,利用進行Logstash資料整理儲存到Elasticsearch

專案中我們常用的是把Logback列印的日誌儲存到檔案中儲存到硬碟上,這樣不利於日誌的收集和分析。 以下演示在SpringBoot中通過rabbitmq收集logback日誌儲存到Elasticsearch中。 環境準備:安裝RabbitMQ,安裝Elasticsearc

Spring-Web專案的異常處理

前言 異常體系在任何計算機語言中都有著重要的分量,但是對於普通開發者來說總是存在著多多少少的疑問:什麼時候使用異常?什麼時候要對異常進行統一處理?該如何對異常進行統一處理? 這裡,我將把我們後臺系統的異常處理機制的演變過程進行闡釋。 分散式處理 大家

spring web專案使用Apache CXF框架釋出webservice服務

場景: 使用Apache CXF釋出一個查詢城市資訊的服務,       查到資訊是服務端寫的一個JSON格式的靜態資料,使用webservice的最簡流程。 1.建立spring web專案,且確保web專案在Tomcat中能正常啟動。   講解:本例的

spring web專案獲取WEB-INF/classes目錄與WEB-INF/目錄

場景:在spring web專案中獲取WEB-INF/classes目錄與WEB-INF/目錄  1.獲取WEB-INF/classes 目錄  /**1.獲取WEB-INF/classes 目錄 **/ public static String getW

向maven web專案新增spring mvc依賴的jar包

使用maven進行專案管理的優勢之一就是maven可以幫我們管理專案建設中所使用的jar包,我們只需要在maven專案下的pox.xml中向dependencies下新增所需要的jar包資訊(包括groupId、artifactid、version)即可,這

java web專案整合Jetty作為web容器

1、準備好一個非常簡單點的web專案(maven專案) 2、準備好maven環境,並配置pom檔案,關於jetty內容如下: <!-- jetty dependecies begin --&g

Python Web專案使用Jenkins進行持續整合CI

轉載原文:http://www.hustlzp.com/post/2014/08/jenkins 在一個專案的開發過程中,往往會有一些需要反覆執行的操作,比如編譯、測試、部署。具體於Flask專案,我一般使用nose執行單元測試、fabric進行部署、pylint執行程式碼質量檢測等。這些頻繁需要執行