spring amqp rabbitmq fanout配置

news/2024/7/7 18:55:14

基于spring amqp rabbitmq fanout配置如下:

发布端

<rabbit:connection-factory id="rabbitConnectionFactory" username="guest" password="guest" host="localhost" port="5672"/>
<rabbit:template id="amqpTemplate" connection-factory="rabbitConnectionFactory" exchange="fanout-mq-exchange" message-converter="jsonMessageConverter"/>
<rabbit:admin id="admin" connection-factory="rabbitConnectionFactory"/>
<bean id="jsonMessageConverter" class="org.springframework.amqp.support.converter.Jackson2JsonMessageConverter" />

<rabbit:queue id="fanout_queue" name="fanout_queue" durable="true" auto-delete="false" exclusive="false" />
<rabbit:queue id="fanout_queue1" name="fanout_queue1" durable="true" auto-delete="false" exclusive="false" />
<rabbit:queue id="fanout_queue2" name="fanout_queue2" durable="true" auto-delete="false" exclusive="false" />

<rabbit:fanout-exchange id="fanout-mq-exchange" name="fanout-mq-exchange" durable="true" auto-delete="false">
<rabbit:bindings>
<rabbit:binding queue="fanout_queue"/>
<rabbit:binding queue="fanout_queue1"/>
<rabbit:binding queue="fanout_queue2"/>
</rabbit:bindings>
</rabbit:fanout-exchange>

/**
* @Title: MQProducerImpl.java
* @Package com.cyl.rabbitmq
* @Description: TODO(用一句话描述该文件做什么)
* @author zjhua@hundsun.com
* @date 2016年4月25日 下午1:12:46
* @version V1.0
*/
package com.cyl.rabbitmq;

import org.springframework.amqp.core.AmqpTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
* @author zjhua
*
*/
@Service
public class MQProducerImpl implements MQProducer {

@Autowired
private AmqpTemplate amqpTemplate;

/* (non-Javadoc)
* @see com.cyl.rabbitmq.MQProducer#sendDataToQueue(java.lang.String, java.lang.Object)
*/
@Override
public void sendDataToQueue(String queueKey, Object object) {
try {
amqpTemplate.convertAndSend("fanout-mq-exchange",null,object);
} catch (Exception e) {
e.printStackTrace();
}
}
}

消费端

<rabbit:connection-factory id="rabbitConnectionFactory" username="guest" password="guest" host="localhost" port="5672"/>
<rabbit:template id="amqpTemplate" connection-factory="rabbitConnectionFactory" exchange="fanout-mq-exchange" message-converter="jsonMessageConverter"/>
<rabbit:admin id="admin" connection-factory="rabbitConnectionFactory"/>
<bean id="jsonMessageConverter" class="org.springframework.amqp.support.converter.Jackson2JsonMessageConverter" />

<!-- <rabbit:queue id="fanout_queue" name="fanout_queue" durable="true" auto-delete="false" exclusive="false" />
<rabbit:queue id="fanout_queue1" name="fanout_queue1" durable="true" auto-delete="false" exclusive="false" />
--><rabbit:queue id="fanout_queue2" name="fanout_queue2" durable="true" auto-delete="false" exclusive="false" />

<rabbit:fanout-exchange id="fanout-mq-exchange" name="fanout-mq-exchange" durable="true" auto-delete="false">
<rabbit:bindings>
<!-- <rabbit:binding queue="fanout_queue"/>
<rabbit:binding queue="fanout_queue1"/> -->
<rabbit:binding queue="fanout_queue2"/>

</rabbit:bindings>
</rabbit:fanout-exchange>
<!--
<rabbit:topic-exchange name="myExchange">
<rabbit:bindings>
<rabbit:binding queue="myQueue" pattern="foo.*" />
</rabbit:bindings>
</rabbit:topic-exchange>

<rabbit:listener-container id="fanout1" connection-factory="rabbitConnectionFactory" acknowledge="auto">
<rabbit:listener queues="fanout_queue1" ref="queueListener1"/>
</rabbit:listener-container>
-->
<rabbit:listener-container id="fanout2" connection-factory="rabbitConnectionFactory" acknowledge="auto">
<rabbit:listener queues="fanout_queue2" ref="queueListener2"/>
</rabbit:listener-container>

<!-- <bean id="mqProducer" class="com.cyl.rabbitmq.MQProducerImpl"></bean>
<bean id="queueListener1" class="com.cyl.rabbitmq.QueueListenter"></bean> -->
<bean id="queueListener2" class="com.cyl.rabbitmq.QueueListenter"></bean>

/**
* @Title: QueueListenter.java
* @Package com.cyl.rabbitmq
* @Description: TODO(用一句话描述该文件做什么)
* @author zjhua@hundsun.com
* @date 2016年4月25日 下午1:15:31
* @version V1.0
*/
package com.cyl.rabbitmq;

import org.springframework.amqp.core.Message;
import org.springframework.amqp.core.MessageListener;

/**
* @author zjhua
*
*/
public class QueueListenter implements MessageListener {

/* (non-Javadoc)
* @see org.springframework.amqp.core.MessageListener#onMessage(org.springframework.amqp.core.Message)
*/
@Override
public void onMessage(Message msg) {
System.out.println(new String(msg.getBody()));
}
}

事实上这种配置仅适用于中小型规模、服务器数量能够提前预估的环境,对于服务器数量不可提前确定或者经常可能发生变化的环境,并不适合采用配置式,而是在容器启动时进行动态注册。并且queue的属性应该为exclusive+auto-delete。


http://lihuaxi.xjx100.cn/news/243178.html

相关文章

UI设计工作好找吗?有哪些面试技巧?

最近有很多学习UI设计的学员&#xff0c;想要了解UI设计学成之后是否好找工作?对于后期的面试有哪些技巧?下面小编整理的这些希望可以帮助到大家&#xff0c;来看看下面的详细介绍。 UI设计工作好找吗?有哪些面试技巧? 作品&#xff1a;很多初级小白的问题所在就是缺少大量…

web应用的绝对路径和相对路径

经常写web工程&#xff0c;就会涉及很多路径问题&#xff0c;今天复习下绝对路径和相对路径&#xff0c;以提醒自己下次不要以为路径问题头疼。 1.绝对路径和相对路径 相对路径&#xff1a;helloworld ./helloworld ../helloworld 这样的都是相对路径绝对路径&…

How can I create an Asynchronous function in Javascript?

哈哈&#xff1a;&#xff09;我的codepen 的代码笔记是&#xff1a;http://codepen.io/shinewaker/pen/eBwPxJ ------------------------------------------------------- 84down votefavorite 39I mean, check it out this code : <a href"#" id"link"…

SSL/TLS原理详解

本文大部分整理自网络&#xff0c;相关文章请见文后参考。 关于证书授权中心CA以及数字证书等概念&#xff0c;请移步 OpenSSL 与 SSL 数字证书概念贴 &#xff0c;如果你想快速自建CA然后签发数字证书&#xff0c;请移步 基于OpenSSL自建CA和颁发SSL证书 。 SSL/TLS作为一种互…

APP自动化测试过程概述

对于Android App的自动化测试框架的使用&#xff0c;其实在很多书上面都会有说明&#xff0c;我们可以先来看一个常用的自动化测试实例&#xff0c;先不说框架&#xff0c;主要是测试用户操作的模拟、执行结果的判断&#xff0c;以便获得对测试自动化的理解与认识。 案例需求如…

jdk7新特性学习笔记

jdk7新特性学习笔记 从网络down了视频看&#xff0c;记录下学过的东西。1.二进制字面量 JDK7开始,可以用二进制来表示整数&#xff08;byte,short,int和long&#xff09;&#xff0c;语法&#xff1a;在二进制数值前面加 0b或者0B例如&#xff1a;int x 0b11112.数字字面量可以…

Linux进程ID号--Linux进程的管理与调度(三)

进程ID概述 进程ID类型 要想了解内核如何来组织和管理进程ID&#xff0c;先要知道进程ID的类型&#xff1a; 内核中进程ID的类型用pid_type来描述,它被定义在include/linux/pid.h中 enum pid_type {PIDTYPE_PID,PIDTYPE_PGID,PIDTYPE_SID,PIDTYPE_MAX };12345671234567PID 内核…

如何理解JS的单线程?

JS本质是单线程的。也就是说&#xff0c;它并不能像JAVA语言那样&#xff0c;两个线程并发执行。 但我们平时看到的JS&#xff0c;分明是可以同时运作很多任务的&#xff0c;这又是怎么回事呢? 首先&#xff0c;JS的代码&#xff0c;大致分为两类&#xff0c;同步代码和异步代…