RabbitMQ点对点的模式
pom:
<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.toov5</groupId>
<artifactId>rabbitmq</artifactId>
<version>0.0.1-SNAPSHOT</version>
<dependencies>
<dependency>
<groupId>com.rabbitmq</groupId>
<artifactId>amqp-client</artifactId>
<version>3.6.5</version>
</dependency>
</dependencies> </project>
创建连接的工具类:
package com.toov5.utils; import java.io.IOException;
import java.util.concurrent.TimeoutException; import com.rabbitmq.client.Connection;
import com.rabbitmq.client.ConnectionFactory; //没有做成单例的 VirtualHost 需要复用
public class MQConnectionUtils {
//创建新的连接
public static Connection newConnection() throws IOException, TimeoutException {
//创建连接工厂
ConnectionFactory factory= new ConnectionFactory();
//链接地址
factory.setHost("192.168.91.6");
//用户名称
factory.setUsername("admin");
//用户密码
factory.setPassword("admin");
//amqp端口号
factory.setPort(5672);
//连接virtualhost
factory.setVirtualHost("/admin_toov5");
Connection connection = factory.newConnection();
return connection;
} }
Producer类
package com.toov5.Producer; import java.io.IOException;
import java.util.concurrent.TimeoutException; import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Connection;
import com.toov5.utils.MQConnectionUtils; public class Producer {
//队列名称
private static final String UEUE_NAME = "test_queue"; public static void main(String[] args) throws IOException, TimeoutException {
//创建新的连接
Connection connection = MQConnectionUtils.newConnection();
//创建Channel
Channel channel = connection.createChannel();
//创建队列
channel.queueDeclare(UEUE_NAME, false, false, false, null);
//创建message
String msg = "toov5_message";
System.out.println("生产者投递消息"+msg);
//生产者发送消息
channel.basicPublish("",UEUE_NAME, null, msg.getBytes());
//关闭通道和连接
channel.close();
connection.close();
}
}
运行结果,看下这个队列

模拟get message

consumer跟 producer基本类似
package com.toov5.Consumer; import java.io.IOException;
import java.util.concurrent.TimeoutException; import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Connection;
import com.rabbitmq.client.DefaultConsumer;
import com.rabbitmq.client.Envelope;
import com.rabbitmq.client.AMQP.BasicProperties;
import com.toov5.utils.MQConnectionUtils; public class Consumer { //队列名称
private static final String QUEUE_NAME = "test_queue"; public static void main(String[] args) throws IOException, TimeoutException {
System.out.println("消费者启动..........");
//创建新的连接
Connection connection = MQConnectionUtils.newConnection();
//创建Channel
Channel channel = connection.createChannel();
// 消费者关联队列
channel.queueDeclare(QUEUE_NAME, false, false, false, null); DefaultConsumer defaultConsumerr = new DefaultConsumer(channel) {
//监听获取消息
@Override
public void handleDelivery(String consumerTag, Envelope envelope, BasicProperties properties,
byte[] body) throws IOException {
String msg =new String(body,"UTF-8");
System.out.println("消费者获取生产者消息:"+msg);
}
};
//牵手模式设置 默认自动应答模式 true:自动应答模式
channel.basicConsume(QUEUE_NAME, true, defaultConsumerr); // //关闭通道和连接
// channel.close();
// connection.close();
}
}
运行后的结果:

RabbitMQ点对点的模式的更多相关文章
- RabbitMQ六种队列模式-简单队列模式
前言 RabbitMQ六种队列模式-简单队列 [本文]RabbitMQ六种队列模式-工作队列RabbitMQ六种队列模式-发布订阅RabbitMQ六种队列模式-路由模式RabbitMQ六种队列模式-主 ...
- RabbitMQ六种队列模式-工作队列模式
前言 RabbitMQ六种队列模式-简单队列RabbitMQ六种队列模式-工作队列 [本文]RabbitMQ六种队列模式-发布订阅RabbitMQ六种队列模式-路由模式RabbitMQ六种队列模式-主 ...
- RabbitMQ六种队列模式-发布订阅模式
前言 RabbitMQ六种队列模式-简单队列RabbitMQ六种队列模式-工作队列RabbitMQ六种队列模式-发布订阅 [本文]RabbitMQ六种队列模式-路由模式RabbitMQ六种队列模式-主 ...
- RabbitMQ六种队列模式-路由模式
前言 RabbitMQ六种队列模式-简单队列RabbitMQ六种队列模式-工作队列RabbitMQ六种队列模式-发布订阅RabbitMQ六种队列模式-路由模式 [本文]RabbitMQ六种队列模式-主 ...
- RabbitMQ六种队列模式-主题模式
前言 RabbitMQ六种队列模式-简单队列RabbitMQ六种队列模式-工作队列RabbitMQ六种队列模式-发布订阅RabbitMQ六种队列模式-路由模式RabbitMQ六种队列模式-主题模式 [ ...
- RabbitMQ之消息模式(下)
目的: RabbitMQ之消息模式(上):https://www.cnblogs.com/huangting/p/11994539.html 消费端限流 消息的ACK与重回队列 TTL消息 死信队列 ...
- 【RabbitMQ】六种模式与SpringBoot整合
添加rabbitmq的依赖 <dependency> <groupId>org.springframework.boot</groupId> <artifac ...
- RabbitMQ六种工作模式有哪些?怎样用SpringBoot整合RabbitMQ
目录 一.RabbitMQ入门程序 二.Work queues 工作模式 三.Publish / Subscribe 发布/订阅模式 四.Routing 路由模式 五.Topics 六.Header ...
- java面试一日一题:rabbitMQ的工作模式
问题:请讲下rabbitMQ的工作模式 分析:该问题纯属概念题,需要掌握rabbtiMQ的基础知识,同时该题也是切入MQ的一个引子: 回答要点: 主要从以下几点去考虑, 1.rabbitMQ的基本概念 ...
随机推荐
- GLSL经典新手教程汇总
权威官方文档:https://www.opengl.org/documentation/glsl/ 权威民间金典新手教程:http://blog.csdn.net/racehorse 一个具体完整的G ...
- springboot整合docker部署(两种构建Docker镜像方式)
项目结构 package hello; import org.springframework.boot.SpringApplication; import org.springframework.bo ...
- Android环境变量的设置(详细图解版)
分类: Android初学学习笔记2011-07-10 09:47 99479人阅读 评论(0) 收藏 举报 androidtoolspathcmd 查阅了网上很多的资料但是对于环境变量设置介绍的不够 ...
- poj 2367
Genealogical tree Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 3658 Accepted: 2433 ...
- 详细的linux目录结构详细介绍
详细的linux目录结构详细介绍 --树状目录结构图 下面红色字体为比较重要的目录 1./目录 目录 描述 / 第一层次结构的根,整个文件系统层次结构的根目录 /bin/ 需要在单用户模式可用的必要命 ...
- 苹果mac shell 终端 命令行快捷键——行首行尾
ctrl+a //移到行首 ctrl+e //移到行尾 http://blog.csdn.net/hherima/article/details/47083739
- php 判断数组中是否有重复的值
$input = array(4, "4", "3", 4, 3, "3"); $result = array_unique($input) ...
- 通俗的解释下音视频同步里pcr作用
PCR同步在非硬件精确时钟源的情况还是谨慎使用,gstreamer里面采用PCR同步,但是发现好多ffmpeg转的片儿,或者是CP方的片源,pcr打得很粗糙的,老是有跳帧等现象.音视频同步,有三种方法 ...
- 使用Eclipse自带的Maven插件创建Web项目时报错:
问题描述: 使用Eclipse自带的Maven插件创建Web项目时报错: Could not resolve archetype org.apache.maven.archetypes:maven-a ...
- EasyNVR无插件H5/HLS/m3u8直播解决方案中Windows系统服务启动错误问题的修复:EasyNVR_Service 服务因 函数不正确。 服务特定错误而停止。
最近在做某地市移动公司景观直播的项目时,遇到一个问题,当我们部署EasyNVR为系统服务后,居然出现了无法启动服务的现象,表面上看,提示是系统服务启动失败,实际通过查看windows 系统日志: 查找 ...