写在开始

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. linux 使用错误总结

    1.执行./shutdown.sh或./startup.sh命令,报错”Permission denied“(用户没有权限),执行以下语句: chmod u+x *.sh

  2. Java 中 Emoji 的正则表达式

    一.emoji 的范围 查阅维基百科中 emoji 的说明 1. 杂项符号及图形 杂项符号及图形一共有768个字符,范围为: U+1F300 - U+1F5FF,在 Java 中正则表达式为: &qu ...

  3. java.sql.SQLException: Incorrect string value: '\xF0\x9F\x98\x8E' for column 'nick' at row 1

    java.sql.SQLException: Incorrect string value: '\xF0\x9F\x98\x8E' for column 'nick' at row 1 mysql报错 ...

  4. 原生JavaScript技巧大收集

    原生JavaScript技巧大收集 地址:http://itindex.net/detail/47244-javascript

  5. RadioButtonFor值为false.默认选中的问题

    (自己看了下.图片有点宽.显示的不全.可以右键新标签查看) 作为一个新手.今天又开始了mvc的学习之旅.然而学习过程中又遇到了一个奇妙的问题.... 一切按部就班到了这里.注册界面. 一眼看上去就不对 ...

  6. #leetcode刷题之路39-组合总和

    给定一个无重复元素的数组 candidates 和一个目标数 target ,找出 candidates 中所有可以使数字和为 target 的组合.candidates 中的数字可以无限制重复被选取 ...

  7. 使用JQ实现统计剩余字数

    JQ实现统计文本框剩余字数 效果图: 代码如下,复制即可使用: <html lang="en"> <head> <meta charset=" ...

  8. django中使用tinymce 富文本

    django后台集成富文本编辑器Tinymce  安装方式一: 1.首先去python的模块包的网站下载一个django-tinymce的包 https://pypi.python.org/pypi/ ...

  9. golang 杂思

    正文 这里给大家总结一些 Go player 开发小技巧. 欢迎批评和交流, 望大家喜欢. 1. 配置管理 推荐一种简单粗暴的配置管理方式 [配置 映射 内部结构]. 例如有个配置文件 config. ...

  10. MYSQL注入天书之盲注讲解

    Background-2 盲注的讲解 何为盲注?盲注就是在sql注入过程中,sql语句执行的选择后,选择的数据不能回显到前端页面.此时,我们需要利用一些方法进行判断或者尝试,这个过程称之为盲注.从ba ...