Windows下安装及配置RabbitMQ?
接下来讲解如何在web项目中配合spring来使用rabbitMQ?
<!-- 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> |
<?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> |
<?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> |
packagecom.hp.netpro.rf.web.utils; |
package com.hp.netpro.rf.web.utils; } |
/** */
@Resource private RabbitMQProducer rabbitMQProducer; //注意:这里必须以这种注入的方式 不能使用new来创建对象,
|