写在开始

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. 20145203盖泽双 《Java程序设计》第9周学习总结

    20145203盖泽双 <Java程序设计>第9周学习总结 教材学习内容总结 1.撰写应用程序是利用通信协议对数据库进行指令交换,以进行数据的增删查找. 2.JDBC目的:让Java程序设 ...

  2. module object has no attribute dumps的解决方法

    问题产生原因: python的版本过低,其中的json包年久失修,需要更新 解决方法: 删除就的json包 >>> import json >>> print js ...

  3. 一个考验c语言和数据结构功底的小项目

    想测一下自己c语言学习水平的朋友可以做一下这个项目试试,能做出来说明c语言已经入门了   #include<stdio.h> #include<stdlib.h> #inclu ...

  4. Centos7 yum安装Mysql5.7

    1.下载mysql安装源 curl -LO http://dev.mysql.com/get/mysql57-community-release-el7-11.noarch.rpm 2.安装yum源 ...

  5. iOS开发用如何用类"SKStoreProductViewController"跳转AppStore点赞评分?

    大家都知道,评论和评分是决定app在appstore中排名的重要因素,但是大部分用户下载安装APP后却不会去点评,所以添加提示用户去点评的功能是很必要的. 目前,AppStore点赞评分有两种方法,一 ...

  6. go语言的结构体指针

    Go 语言结构体 Go 语言中数组可以存储同一类型的数据,但在结构体中我们可以为不同项定义不同的数据类型.   结构体是由一系列具有相同类型或不同类型的数据构成的数据集合.   结构体表示一项记录,比 ...

  7. Docker:unauthorized: incorrect username or password.

    用VS2017编译DockerCompose项目,显示错误:unauthorized: incorrect username or password. 打开命令行工具,输入docker login命令 ...

  8. tomcat软连接的使用

    软连接说白了就是一个映射.可以映射文件,也可以映射目录.linux和windows都可以做软连接,加入现在把文件A.txt做软连接到B.txt: linux命令如下: ln -s A.txt B.tx ...

  9. 北京Uber优步司机奖励政策(4月19日)

    滴快车单单2.5倍,注册地址:http://www.udache.com/ 如何注册Uber司机(全国版最新最详细注册流程)/月入2万/不用抢单:http://www.cnblogs.com/mfry ...

  10. jQuery学习-基本选择器

    <!DOCTYPE html> <html> <head> <meta charset="utf-8" /> <title&g ...