一 思路总结

1 主要用spring-boot-starter-amqp来整合RabbitMQ和SpringBoot

2 使用spring-boot-starter-test来进行单元测试

3编写配置文件application.yml

spring:
application:
name: rabbitmq-hello
rabbitmq:
host: localhost
port: 5672
username: guest
password: guest
4 编写主类Application.class
5 编写Sender并自动扫面,以备自动注入
@Component
public class Sender {
@Autowired
private AmqpTemplate amqpTemplate; public void send() throws Exception {
String context = "hello" + new Date();
System.out.println("Sender:"+context);
this.amqpTemplate.convertAndSend("hello",context);
}
}
6 编写Reveiver并自动扫面同时监听队列hello,并用RabbitHandler来处理请求。
@Component
@RabbitListener(queues = "hello")
public class Receiver { @RabbitHandler
public void process(String hello){
System.out.println("Receiver:"+hello);
}
}
7 编写刚刚用到的hello队列的配置类
@Configuration
public class RabbitConfig {
@Bean
public Queue helloQueue() {
return new Queue("hello");
}
}
8 编写单元测试类Test,调用Sender的方法发送message,这样Receiver就能自动监听并在主类哪里输出了
@SpringBootTest(classes = Application.class)
@RunWith(SpringJUnit4ClassRunner.class)
public class Test { @Autowired
private Sender sender; @org.junit.Test
public void hello() throws Exception {
sender.send();
}
}
9 结果

												

RabbitMQ和SpringBoot的简单整合列子的更多相关文章

  1. RabbitMQ(三):RabbitMQ与Spring Boot简单整合

    RabbitMQ是目前非常热门的一款消息中间件,不管是互联网大厂还是中小企业都在大量使用.Spring Boot的兴起,极大地简化了Spring的开发,本文将使用Spring Boot与RabbitM ...

  2. springboot vue简单整合

    1.vue项目 (1)修改config/index.js (2)执行 npm run build 生成静态文件,在dist目录 2.springboot项目 (1)在src/main/resource ...

  3. 快速搭建springboot框架以及整合ssm+shiro+安装Rabbitmq和Erlang、Mysql下载与配置

    1.快速搭建springboot框架(在idea中): file–>new project–>Spring Initializr–>next–>然后一直下一步. 然后复制一下代 ...

  4. Springboot与MyBatis简单整合

    之前搭传统的ssm框架,配置文件很多,看了几天文档才把那些xml的逻辑关系搞得七七八八,搭起来也是很麻烦,那时我完全按网上那个demo的版本要求(jdk和tomcat),所以最后是各种问题没成功跑起来 ...

  5. SpringBoot学习之整合Druid的简单应用

    一.Druid介绍 Druid简介 Druid是目前Java语言中最好的数据库连接池之一.结合了 C3P0.DBCP 等 DB 池的优点,同时加入了日志监控.Druid 是一个分布式的.支持实时多维 ...

  6. SpringBoot简单整合redis

    Jedis和Lettuce Lettuce 和 Jedis 的定位都是Redis的client,所以他们当然可以直接连接redis server. Jedis在实现上是直接连接的redis serve ...

  7. 【spring boot】SpringBoot初学(8)– 简单整合redis

    前言 到目前为止,把项目中需要用到的:properties读取.数据源配置.整合mybatis/JdbcTemplate.AOP.WebService.redis.filter.interceptor ...

  8. 带着新人学springboot的应用08(springboot+jpa的整合)

    这一节的内容比较简单,是springboot和jpa的简单整合,jpa默认使用hibernate,所以本质就是springboot和hibernate的整合. 说实话,听别人都说spring data ...

  9. RabbitMQ与Spring的框架整合之Spring Cloud Stream实战

    1.RabbitMQ与Spring Cloud Stream整合实战.SpringCloud Stream整体结构核心概念图,如下所示: 图示解释:Outputs输出,即消息的发送端.Inputs输入 ...

随机推荐

  1. pb9常见错误及含义

    1. by zero  发生被0除错误 2. Null object reference  空对象引用 3. Array boundary exceeded  数组越界 4. Enumerated v ...

  2. haproxy + keepalived 实现网站高可靠

    haproxy 1的配置文件,包括 keepalived 和 haproxy 的配置,分别如下: [haproxy 1的keepalived 配置文件]  /etc/keepalived/keepal ...

  3. 笨鸟先飞之ASP.NET MVC系列之过滤器(04认证过滤器过滤器)

    概念介绍 认证过滤器是MVC5的新特性,它有一个相对复杂的生命周期,它在其他所有过滤器之前运行,我们可以在认证过滤器中创建一个我们定义的认证方法,也可以结合授权过滤器做一个复杂的认证方法,这个方法可以 ...

  4. [置顶] win10 uwp 参考

    态度随意申请专栏,没想到通过 看了我的博客,都是在别的大神博客看到,然后修改他们的 我看到的大神博客 东邪独孤 http://www.cnblogs.com/tcjiaan/ 老周,买了他的<W ...

  5. day38(增强类的实现)

    定义一个接口 package com.baidu.test; public interface Person { public abstract void eat(); public abstract ...

  6. Entity Framework Core 2.0 使用入门

    一.前言 Entity Framework(后面简称EF)作为微软家的ORM,自然而然从.NET Framework延续到了.NET Core.以前我也嫌弃EF太重而不去使用它,但是EF Core(E ...

  7. 笨鸟先飞之ASP.NET MVC系列之过滤器(06异常过滤器)

    概念介绍 异常过滤器主要在我们方法中出现异常的时候触发,一般我们用 异常过滤器 记录日志,或者在产生异常时做友好的处理 如果我们需要创建异常过滤器需要实现IExceptionFilter接口. nam ...

  8. 【计算机网络】 一个小白的DNS学习笔记

    参考书籍 <计算机网络-自顶向下>  作者 James F. Kurose   DNS的作用   DNS是因特网的目录服务 DNS是因特网的目录服务,它提供了主机名到IP地址映射的查询服务 ...

  9. 为什么使用 Bootstrap

    移动设备优先:自 Bootstrap 3 起,框架包含了贯穿于整个库的移动设备优先的样式 浏览器支持:所有的主流浏览器都支持 Bootstrap 容易上手:只要您具备 HTML 和 CSS 的基础知识 ...

  10. Path.Combine 合并两个路径字符串,会出现的问题

    Path.Combine(path1,path2) 1.如果path2字符串,以 \ 或 / 开头,则直接返回 path2