RabbitMq与Spring的整合

首先在pom.xml映入

  <dependency>
         <groupId>org.springframework.amqp</groupId>
         <artifactId>spring-rabbit</artifactId>
         <version>1.4.0.RELEASE</version>
      </dependency>

然后在xml文件中进行MQ配置

<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/rabbit
   http://www.springframework.org/schema/rabbit/spring-rabbit-1.4.xsd
   http://www.springframework.org/schema/beans
   http://www.springframework.org/schema/beans/spring-beans-4.1.xsd">
 	<!--创建链接  -->
 	<rabbit:connection-factory id="rabbitConnectionFactory" host="localhost" port="5672" username="admin" password="admin" virtual-host="testhost"/>
 	<!-- 管理queue、exchange -->
 	<rabbit:admin connection-factory="rabbitConnectionFactory"/>
 	<!--创建fanOutTemplate 声明exchange -->
 	<rabbit:template id="fanOutTemplate" connection-factory="rabbitConnectionFactory" exchange="fanoutexchange"/>
 	<rabbit:template id="topicTemplate" connection-factory="rabbitConnectionFactory" exchange="topicexchange" routing-key="aa.m.z"/>
 	<rabbit:template id="directTemplate" connection-factory="rabbitConnectionFactory" exchange="directexchange" routing-key="find"/>
 	<!-- 声明queue -->
 	<rabbit:queue name="fanoutqueue" auto-declare="true"  />
 	<rabbit:queue name="topicqueue" auto-declare="true"/>
 	<rabbit:queue name="directqueue" auto-declare="true"/>
 	<rabbit:direct-exchange name="directexchange" auto-declare="true" durable="false">
 		<rabbit:bindings>
 			<rabbit:binding queue="directqueue" key="find"></rabbit:binding>
 			<rabbit:binding queue="topicqueue" key="find"></rabbit:binding>
 		</rabbit:bindings>
 	</rabbit:direct-exchange>
 	<rabbit:fanout-exchange name="fanoutexchange" auto-declare="true" durable="false">
 		<rabbit:bindings>
 		<rabbit:binding queue="fanoutqueue"> </rabbit:binding>
 		</rabbit:bindings>
 	</rabbit:fanout-exchange>
 	<rabbit:topic-exchange name="topicexchange" auto-declare="true" durable="false">
 	<rabbit:bindings>
 		<rabbit:binding pattern="aa.#" queue="topicqueue"></rabbit:binding>
 	</rabbit:bindings>
 	</rabbit:topic-exchange>
 	<rabbit:listener-container>
 		<rabbit:listener ref="fanoutListener" queue-names="fanoutqueue" method="listen"/>
 		<rabbit:listener ref="directListener" queue-names="directqueue" method="listen"/>
 		<rabbit:listener ref="topicListener" queue-names="topicqueue" method="listen"/>
 	</rabbit:listener-container>
 	<bean id="fanoutListener" class="com.jshb.testQueue.FanOutListener"/>
 		<bean id="directListener" class="com.jshb.testQueue.DirectListener"/>
 		<bean id="topicListener" class="com.jshb.testQueue.TopicListener"/>
   
</beans>

 

生产者: 负责发送消息到Exchange。

Exchange: 按照一定的策略,负责将消息存入到指定的队列。

队列queue:  负责保存消息。

消费者: 负责从队列中提取消息。

binding: 负责Exchange和队列的关联映射,Exchange和queue是多对多的关系

其中 exchange有3中模式 

1、Direct:

所有发送到Direct Exchange的消息被转发到RouteKey中指定的Queue。

  Direct模式,可以使用rabbitMQ自带的Exchange:default Exchange 。所以不需要将Exchange进行任何绑定(binding)操作 。消息传递时,RouteKey必须完全匹配,才会被队列接收,否则该消息会被抛弃。

2、:fanout

所有发送到Fanout Exchange的消息都会被转发到与该Exchange 绑定(Binding)的所有Queue上。

  Fanout Exchange  不需要处理RouteKey 。只需要简单的将队列绑定到exchange 上。这样发送到exchange的消息都会被转发到与该交换机绑定的所有队列上。类似子网广播,每台子网内的主机都获得了一份复制的消息。

  所以,Fanout Exchange 转发消息是最快的。

3、topic 通配模式

 所有发送到Topic Exchange的消息被转发到所有关心RouteKey中指定Topic的Queue上,

  Exchange 将RouteKey 和某Topic 进行模糊匹配。此时队列需要绑定一个Topic。可以使用通配符进行模糊匹配,符号“#”匹配一个或多个词,符号“*”匹配不多不少一个词。因此“log.#”能够匹配到“log.info.oa”,但是“log.*” 只会匹配到“log.error”。

  所以,Topic Exchange 使用非常灵活。

其中对应的消费者代码

package com.jshb.testQueue;


public class DirectListener {
		
	
	public void listen(String in){
	
		System.out.println(in);
		
	}
}
package com.jshb.testQueue;

import java.io.FileOutputStream;
import java.io.OutputStream;
import java.util.List;

public class TopicListener {
	
	
	public void listen(String msg){
		System.out.println("topic=="+msg);
	}
}
package com.jshb.testQueue;

import java.io.InputStream;

public class FanOutListener {
	
	
	public void listen(String msg){
		System.out.println(":fanout=="+msg);
	}
}

生产者

package com.jshb.testQueue;

import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;

import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.context.support.AbstractApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class TestSend {
	public static void main(String[] args) {
		 AbstractApplicationContext ctx = new ClassPathXmlApplicationContext(
	                "classpath:rabbit-amqp.xml");
		RabbitTemplate fanOutTemplate=(RabbitTemplate) ctx.getBean("fanOutTemplate");
		RabbitTemplate topicTemplate=(RabbitTemplate) ctx.getBean("topicTemplate");
		RabbitTemplate directTemplate=(RabbitTemplate) ctx.getBean("directTemplate");
		fanOutTemplate.convertAndSend("fanOutTemplate");
		topicTemplate.convertAndSend("topic");
		directTemplate.convertAndSend("directTemplate");
		
	}
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值