web項目中spring如何整合RabbitMQ?__web

來源:互聯網
上載者:User
如何在Windows下安裝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:
 
*/
@Service
public classRabbitMQProducer {

    @Resource
    privateAmqpTemplateamqpTemplate;

    public voidsendMessage(Object message){
        amqpTemplate.convertAndSend("testKey",message);//testKey為設定檔中queue對應的key,指明發送給哪個queue。
  

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.