写在开始

rabbitMq 代码按照三部分介绍

第一部分 交换机和队列的创建

第二部分 消息发送

第三部分 消息监听

第一部分

1 建立queue

2 建立exchange

3 exchange绑定queue

建立之前需要配置两样东西

一个是rabbitMq的连接工厂(ConnectionFactory)、另外一个是操作句柄(RabbitAdmin)。可以看到连接工厂是给操作句柄初始化时使用的。

后续创建队列等一系列操作都需要使用到操作句柄,如果没有使用的话操作被视为无效。

   // 初始化连接
@Bean
public ConnectionFactory connectionFactory() {
CachingConnectionFactory connectionFactory = new CachingConnectionFactory("127.0.0.1", 5672);
connectionFactory.setUsername("guest");
connectionFactory.setPassword("guest");
return connectionFactory;
} // 队列操作配置
@Bean
public RabbitAdmin rabbitAdmin(ConnectionFactory connectionFactory) {
return new RabbitAdmin(connectionFactory);
}

开始建立队列

    public final static String QUEUE_NAME = "tianmh-queue";
public final static String QUEUE_NAME2 = "tianmh-queue2"; @Autowired
private RabbitAdmin rabbitAdmin; // 创建队列1
@Bean(value = QUEUE_NAME)
public Queue queue() {
Queue queue = new Queue(QUEUE_NAME, true, true, true);
this.rabbitAdmin.declareQueue(queue);
return queue;
} // 创建队列2
@Bean(value = QUEUE_NAME2)
public Queue queue2() {
Queue queue = new Queue(QUEUE_NAME2, true, true, true);
this.rabbitAdmin.declareQueue(queue);
return queue;
}

建立交换机。下面例子建立的队列为广播类型队列

    // 创建一个 Fanout 类型的交换器
@Bean(value = EXCHANGE_NAME)
public Exchange exchange() {
Exchange exchange = new FanoutExchange(EXCHANGE_NAME, true, true);
this.rabbitAdmin.declareExchange(exchange);
return exchange;
}

交换机绑定队列

   // 使用路由键(routingKey)把队列(Queue)绑定到交换器(Exchange)
@Bean
public Binding binding(@Qualifier(QUEUE_NAME) Queue queue, @Qualifier(EXCHANGE_NAME) Exchange exchange) {
Binding binding = new Binding(queue.getName(), Binding.DestinationType.QUEUE, exchange.getName(), ROUTING_KEY, null);
this.rabbitAdmin.declareBinding(binding);
return binding;
} // 使用路由键(routingKey)把队列(Queue)绑定到交换器(Exchange)
@Bean
public Binding binding2(@Qualifier(QUEUE_NAME2) Queue queue, @Qualifier(EXCHANGE_NAME) Exchange exchange) {
Binding binding = new Binding(queue.getName(), Binding.DestinationType.QUEUE, exchange.getName(), ROUTING_KEY, null);
this.rabbitAdmin.declareBinding(binding);
return binding;
}

第二部分 消息发送

消息发送需指定发送到的exchangeName及routeKey及内容


@Component
public class SenderDemo {
@Autowired
RabbitTemplate rabbitTemplate; @PostConstruct
public void testSender() {
TestCommand command = new TestCommand();
command.setKey("testContent");
byte[] content = JSONObject.toJSONBytes(command);
MessageProperties messageProperties = new MessageProperties();
messageProperties.setTimestamp(new Date());
messageProperties.setContentType(MessageProperties.CONTENT_TYPE_JSON);
Message message = new Message(content, messageProperties);
rabbitTemplate.send(RabbitMqConfig.EXCHANGE_NAME, "log", message);
}
}

第三部分 消息监听

接收消息是通过监听队列实现的

@RabbitListener(queues = RabbitMqConfig.QUEUE_NAME)
public void process(Message message) {
TestCommand command = JSON.parseObject(new String(message.getBody()), TestCommand.class);
logger.info("接收处理队列[{}]的消息[{}]", RabbitMqConfig.QUEUE_NAME, command.toString());
}

就此一个完整的RabbitMqDemo搭建完成

附带项目POM

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion> <groupId>com.example</groupId>
<artifactId>rabbitmq</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging> <name>rabbitmq</name>
<description>Demo project for Spring Boot</description> <parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.1.0.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent> <properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<java.version>1.8</java.version>
</properties> <dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-amqp</artifactId>
</dependency> <dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency> <dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>1.2.47</version>
</dependency> </dependencies> <build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build> </project>

spring-boot rabbitMq 完整项目搭建,包括创建、发送、监听的更多相关文章

  1. spring原理案例-基本项目搭建 03 创建工程运行测试 spring ioc原理实例示例

    下面开始项目的搭建 使用 Java EE - Eclipse 新建一 Dynamic Web Project Target Runtime 选 Apache Tomcat 7.0(不要选 Apache ...

  2. 基于Spring boot的web项目搭建教程(一)

    前言: 本教程参考了大量前辈的代码,在此不方便一一列举.本教程使用IDEA开发工具搭建项目,对于本人的IDEA已经集成了某些插件,比如Lombok,Thymeleaf,yml等插件,这些插件不在文中提 ...

  3. 一 、Spring Boot 学习之项目搭建

    一.简介 spring 官方网站本身使用Spring 框架开发,随着功能以及业务逻辑的日益复杂,应用伴随着大量的XML配置文件以及复杂的Bean依赖关系. 随着Spring 3.0的发布,Spring ...

  4. spring boot rabbitmq 多MQ配置 自动 创建 队列 RPC

      源码地址:https://github.com/hutuchong518/RabbitmqStudy 需求:   spring boot 整合 rabbitmq rpc功能, 需要将 请求和响应 ...

  5. Spring Boot学习笔记:ApplicationEvent和ApplicationEventListener事件监听

    采用事件监听的好处 以用户注册的业务逻辑为例,用户在填写完信息表单后,提交信息到后台,后台对用户信息进行处理,然后给用户返回处理结果的信息. 如上图所示,用户在注册时,后台需要处理一些系列流程,实际业 ...

  6. spring原理案例-基本项目搭建 02 spring jar包详解 spring jar包的用途

    Spring4 Jar包详解 SpringJava Spring AOP: Spring的面向切面编程,提供AOP(面向切面编程)的实现 Spring Aspects: Spring提供的对Aspec ...

  7. spring原理案例-基本项目搭建 01 spring framework 下载 官网下载spring jar包

    下载spring http://spring.io/ 最重要是在特征下面的这段话,需要注意: All avaible features and modules are described in the ...

  8. Spring Boot 多模块项目创建与配置 (一) (转)

    Spring Boot 多模块项目创建与配置 (一) 最近在负责的是一个比较复杂项目,模块很多,代码中的二级模块就有9个,部分二级模块下面还分了多个模块.代码中的多模块是用maven管理的,每个模块都 ...

  9. Spring Boot 多模块项目创建与配置 (一)

    最近在负责的是一个比较复杂项目,模块很多,代码中的二级模块就有9个,部分二级模块下面还分了多个模块.代码中的多模块是用maven管理的,每个模块都使用spring boot框架.之前有零零散散学过一些 ...

随机推荐

  1. Odoo中的甘特图

    转载请注明原文地址:https://www.cnblogs.com/cnodoo/p/9296922.html  甘特图 用图表来衡量实际与预期生产记录之间关系的方法中所使用的图表,亦称甘特进度表或条 ...

  2. 在centos7上搭建博客之小白教程~

    原理 http使用方法一编译安装,php模块打入方式实现. 软件版本 在本次实验中,我们需要用到的软件版本如下: apr-1.6.2 apr-util-1.6.0 httpd-2.4.28 maria ...

  3. docker 私有仓库 harbor docker-compose

    c创建docker私有仓库 docker pull registry:2.1.1 mkdir /opt/registry#mkdir /var/lib/registry docker run -d - ...

  4. Stm32-uclinux启动后的调试

    Stm32-uclinux启动后的调试 1.  修改__pfn_to_page使得能够启动 根据STM32F103 ucLinux开发之三(内核启动后不正常)的描述,内核无法启动是选择了平板内存模式后 ...

  5. Hive HBase Integration 集成

    官方文档:https://cwiki.apache.org/confluence/display/Hive/HBaseIntegration 1.在hive/lib目录下找到hive-hbase-ha ...

  6. 02_Docker在CentOS 6和CentOS 7下的安装

    CentOS 7 环境下安装docker 安装Docker 检查系统内核是否高于Linux3.10版本 uname -r 使用root权限操作,确保yum包是最新版本 sudo yum update ...

  7. flex 自适应

    flex-grow.flex-shrink.flex-basis这三个属性的作用是:在flex布局中,父元素在不同宽度下,子元素是如何分配父元素的空间的. 其中,这三个属性都是在子元素上设置的. 注: ...

  8. Redis--位图BitMap

    一.BitMap是什么 通过一个bit位来表示某个元素对应的值或者状态,其中的key就是对应元素本身,value对应0或1,我们知道8个bit可以组成一个Byte,所以bitmap本身会极大的节省储存 ...

  9. zlib库的编译及使用

    * 打开网址http://zlib.net/ 下载zlib源码, * 解压压缩包,进入目录:C:\Users\Administrator\Desktop\zlib-1.2.11\zlib-1.2.11 ...

  10. 数据结构与算法之链表(LinkedList)——简单实现

    这一定要mark一下.虽然链表的实现很简单,且本次只实现了一个方法.但关键的是例子:单向链表的反转.这是当年我去H公司面试时,面试官出的的题目,而当时竟然卡壳了.现在回想起来,还是自己的基本功不扎实, ...