当前位置: 首页 > news >正文

RobbitMQ基本消息队列的消息发送过程

RabbitMQ: One broker to queue them all | RabbitMQ RabbitMQ官网

SpringAmqp的官方地址:Spring AMQP

代码示例:对着代码看应该能看明白

publisher:消息发送者的代码示例

package cn.itcast.mq.helloworld;import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Connection;
import com.rabbitmq.client.ConnectionFactory;
import org.junit.Test;import java.io.IOException;
import java.util.concurrent.TimeoutException;public class PublisherTest {@Testpublic void testSendMessage() throws IOException, TimeoutException {// 1.建立连接ConnectionFactory factory = new ConnectionFactory();// 1.1.设置连接参数,分别是:主机名、端口号、vhost、用户名、密码factory.setHost("192.168.221.131");factory.setPort(5672);factory.setVirtualHost("/");factory.setUsername("itcast");factory.setPassword("123321");// 1.2.建立连接Connection connection = factory.newConnection();// 2.创建通道ChannelChannel channel = connection.createChannel();// 3.创建队列String queueName = "simple.queue";channel.queueDeclare(queueName, false, false, false, null);// 4.发送消息String message = "hello, rabbitmq!";channel.basicPublish("", queueName, null, message.getBytes());System.out.println("发送消息成功:【" + message + "】");// 5.关闭通道和连接channel.close();connection.close();}
}
package cn.itcast.mq.helloworld;import com.rabbitmq.client.*;import java.io.IOException;
import java.util.concurrent.TimeoutException;public class ConsumerTest {public static void main(String[] args) throws IOException, TimeoutException {// 1.建立连接ConnectionFactory factory = new ConnectionFactory();// 1.1.设置连接参数,分别是:主机名、端口号、vhost、用户名、密码factory.setHost("192.168.221.131");factory.setPort(5672);factory.setVirtualHost("/");factory.setUsername("itcast");factory.setPassword("123321");// 1.2.建立连接Connection connection = factory.newConnection();// 2.创建通道ChannelChannel channel = connection.createChannel();// 3.创建队列String queueName = "simple.queue";channel.queueDeclare(queueName, false, false, false, null);// 4.订阅消息channel.basicConsume(queueName, true, new DefaultConsumer(channel){@Overridepublic void handleDelivery(String consumerTag, Envelope envelope,AMQP.BasicProperties properties, byte[] body) throws IOException {// 5.处理消息String message = new String(body);System.out.println("接收到消息:【" + message + "】");}});System.out.println("等待接收消息。。。。");}
}

消息接收者代码示例.

SpringAMQP:

  简单消息队列模型:Basic Queue

什么是SpringAMQP:

SpringAMQP消息的发送案例:

        引入依赖

<!--AMQP依赖,包含RabbitMQ--><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-amqp</artifactId></dependency>

在相应的模块下配置yml文件:

spring:rabbitmq:host: 192.168.221.131port: 5672virtual-host: /username: itcastpassword: 123321

编写测试代码:

@RunWith(SpringRunner.class)
@SpringBootTest
public class test01 {@Autowiredprivate RabbitTemplate rabbitTemplate;//rabbit的工具类@Testpublic void test(){String queueName="simple.queue";//消息队列的名字String message="hello libucun";//发送的消息rabbitTemplate.convertAndSend(queueName,message);//连接并且发送}
}

发送成功后在你虚拟机rabbit映射地址里能看到

注意:如果出现IO异常错误 检查yml文件port地址不要搞错了。如果连接超时,查看虚拟机的端口有没有变,没变的话可以重启虚拟机重新启动mq容器再试试。

http://www.lryc.cn/news/345818.html

相关文章:

  • MongoDB聚合运算符:$topN
  • 什么是顶级域名、二级域名、三级域名?
  • [Android]四大组件简介
  • 一次完整的GC流程
  • GAME101-Lecture06学习
  • 202203青少年软件编程(Python)等级考试试卷(二级)
  • 带有-i选项的sed命令在Linux上执行成功,但在MacOS上失败了
  • [Linux_IMX6ULL驱动开发]-GPIO子系统和Pinctrl子系统
  • Elasticsearch:理解人工智能相似性搜索
  • Mac YOLO V9推理测试(基于ultralytics)
  • OuterClass.this cannot be referenced from a static context
  • CAP与BASE分布式理论
  • JavaScript性能优化策略
  • curl访问流式非流式大模型openai api接口
  • Go 使用 MongoDB
  • 什么是g++-arm-linux-gnueabihf
  • Unity延时触发的几种常规方法
  • CSS文字描边,文字间隔,div自定义形状切割
  • XWiki 服务没有正确部署在tomcat中,如何尝试手动重新部署?
  • 【退役之重学Java】关于 Redis
  • DateKit
  • 百度智能云数据仓库 Palo 实战课程
  • 服务端JavaScript(Node.js)与去IO编程:Node.js的事件驱动和非阻塞IO模型,它是如何使JavaScript走向后端的
  • 一键局域网共享工具
  • python实现把doc文件批量转化为docx
  • WEB基础---反射
  • impdp恢复表后发现比原表多了100多行
  • Jupyter配置远程访问的密码
  • Windows下通过MySQL Installer安装MySQL服务
  • C语言 [力扣]详解环形链表和环形链表II