https://www.rabbitmq.com/getstarted.html

官网文档

我们将呼叫我们的消息发布者(发送者)发送和我们的消息消费者(接收者) Recv。发布者将连接到RabbitMQ,发送单个消息,然后退出

创建一个Send.java 根据 springboot 配置文件配置

@Component
public class Send {
//设置类命名队列
private final static String QUEUE_NAME = "hello"; @Value("${spring.rabbitmq.host}")
private String host;
@Value("${spring.rabbitmq.port}")
private int port;
@Value("${spring.rabbitmq.username}")
private String username;
@Value("${spring.rabbitmq.password}")
private String password; public void send(){
//船舰到服务器的链接
ConnectionFactory factory = new ConnectionFactory();
factory.setPort(port);
factory.setPassword(password);
factory.setUsername(username);
factory.setHost(host);
Connection connection = null;
Channel channel = null;
try{
connection = factory.newConnection();
channel = connection.createChannel() ;
channel.queueDeclare(QUEUE_NAME,false,false,false,null);
for(int i = 0;i<5;i++) {
String value = "hello··········";
channel.basicPublish("", QUEUE_NAME, null, value.getBytes());
}
}catch (Exception e){ }finally {
try {
//关闭链接 不然一直消耗
channel.close();
connection.close();
}catch (Exception e){
e.printStackTrace();
}
} }
}

配置文件

########### rabbitmq  ############
spring.rabbitmq.host=XXX.xxx.xxx.xx
spring.rabbitmq.port=xxx
spring.rabbitmq.username=xxx
spring.rabbitmq.password=xxx

启动类

    @Override
public void run(String... args) throws Exception {
Send.init(); }

项目启动后 到

 查看

发送的信息

消费端接收

public static void main (String [] argv) throws Exception{
//创建服务器的链接
ConnectionFactory factory = new ConnectionFactory();
//创建
factory.setHost("xxxx.xxxx.xxxx.xxxx");
factory.setPassword("guest");
factory.setUsername("guest");
factory.setPort(xxxx);
Connection connection = factory.newConnection();
Channel channel = connection.createChannel() ;
channel.queueDeclare(QUEUE_NAME,false,false,false,null);
DefaultConsumer defaultConsumer = new DefaultConsumer(channel){
@Override
public void handleDelivery(String consumerTag, Envelope envelope, AMQP.BasicProperties properties, byte[] body) throws IOException {
String s = new String(body);
System.out.println("接受到的消息:"+s);
}
};
channel.basicConsume(QUEUE_NAME,true,defaultConsumer); }

RabbitMQ + Springboot +“Hello Word”的更多相关文章

  1. 【MQ中间件】RabbitMQ -- SpringBoot整合RabbitMQ(3)

    1.前言说明 前面一篇博客中提到了使用原生java代码进行测试RabbitMQ实现多种交换机类型的队列场景.但是在项目中我们一般使用SpringBoot项目,而且RabbitMQ天生对于Spring的 ...

  2. springboot中word转pdf,加盖电子印章

    概述 在开发过程中,word转pdf的方式有很多种有jar包的方式,有安装openoffice的方式,但是使用有的jar包有license认证,不然会生成水印,综合几种方法我采用了libreoffic ...

  3. RabbitMQ入门到进阶(Spring整合RabbitMQ&SpringBoot整合RabbitMQ)

    1.MQ简介 MQ 全称为 Message Queue,是在消息的传输过程中保存消息的容器.多用于分布式系统 之间进行通信. 2.为什么要用 MQ 1.流量消峰 没使用MQ 使用了MQ 2.应用解耦 ...

  4. RabbitMQ与SpringBoot整合

    RabbitMQ  SpringBoot  一.RabbitMQ的介绍 二.Direct模式 三.Topic转发模式 四.Fanout Exchange形式 原文地址: https://www.cnb ...

  5. rabbitmq学习(七) —— springboot下的可靠使用

    前面的学习都是基于原生的api,下面我们使用spingboot来整合rabbitmq springboot对rabbitmq提供了友好支持,极大的简化了开发流程 引入maven <depende ...

  6. SpringBoot ( 八 ) :RabbitMQ 详解

    原文出处: 纯洁的微笑 RabbitMQ 即一个消息队列,主要是用来实现应用程序的异步和解耦,同时也能起到消息缓冲,消息分发的作用. 消息中间件在互联网公司的使用中越来越多,刚才还看到新闻阿里将Roc ...

  7. springboot(四) rabbitMQ demo

    RabbitMQ 即一个消息队列,主要是用来实现应用程序的异步和解耦,同时也能起到消息缓冲,消息分发的作用. 消息中间件在互联网公司的使用中越来越多,刚才还看到新闻阿里将RocketMQ捐献给了apa ...

  8. SpringBoot集成文件 - 如何使用POI导出Word文档?

    前文我们介绍了通过Apache POI导出excel,而Apache POI包含是操作Office Open XML(OOXML)标准和微软的OLE 2复合文档格式(OLE2)的Java API.所以 ...

  9. RabbitMQ由浅入深入门全总结(一)

    写在最前面 距离上一次发文章已经很久了,其实这段时间一直也没有停笔,只不过在忙着找工作还有学校结课的事情,重新弄了一下博客,后面也会陆陆续续会把文章最近更新出来~ 这篇文章有点长,就分了两篇Q PS: ...

随机推荐

  1. XLS导出的服务器端配置方式

    IIS支持excel导出: 1.开始—运行,然后键入DCOMCNFG; 2.组件服务—计算机—我的电脑—DCOM配置,这时弹出一个问注册的窗口,确定注册. 这时如果一切恢复正常了,不用往下操作了. 关 ...

  2. linux 获取目录中详细信息 -rw-r--r--详解

    -rw-r–r– 1 root root 1313 Sep 3 14:59 test.log详解 查询目录中的内容命令 ls [选项] [文件或目录] 选项: -a 显示所有文件.包括隐藏文件 -l ...

  3. HNUSTOJ-1253 Babelfish(字典树)

    1253: Problem C: Babelfish 时间限制: 1 Sec  内存限制: 128 MB提交: 14  解决: 3[提交][状态][讨论版] 题目描述 Problem C: Babel ...

  4. 【算法入门】深度优先搜索(DFS)

    深度优先搜索(DFS) [算法入门] 1.前言深度优先搜索(缩写DFS)有点类似广度优先搜索,也是对一个连通图进行遍历的算法.它的思想是从一个顶点V0开始,沿着一条路一直走到底,如果发现不能到达目标解 ...

  5. 在Python中使用protobuf2.6.1 string format utf-8 and unicode error

    版本信息: protobuf: v2.6.1 python: 2.7 关于在Python中使用protobuf时 string格式字段的编码问题 在python中编码格式多采用utf-8格式.而pro ...

  6. axios 文件流下载

    this.axios .post(this.baseUrl+"/exportUser", { admin: "",keys: "",keyw ...

  7. BloomFilter&python支持

    BloomFilter&python支持 BloomFilter 布隆过滤器是一种概率空间高效的数据结构.它与hashmap非常相似,用于检索一个元素是否在一个集合中.它在检索元素是否存在时, ...

  8. 客户端远程连接docker容器中的mysql 报1251错误

    1.启动容器: [root@localhost ~]# docker run -d -e MYSQL_ROOT_PASSWORD=123456 -p 3306:3306 mysql2.进入容器: [r ...

  9. if_else

    //if.......else if......else //object IF_ELSE {// def main(args:Array[String]){// var x=30// if (x== ...

  10. YNOI2016:掉进兔子洞 (莫队+bitset)

    YNOI2016:掉进兔子洞 题意简述: 有 m 个询问,每次询问三个区间,把三个区间中同时出现的数一个一个删掉,问最后三个区间剩下的数的个数和,询问独立. 注意这里删掉指的是一个一个删,不是把等于这 ...