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点对点的模式的更多相关文章

  1. RabbitMQ六种队列模式-简单队列模式

    前言 RabbitMQ六种队列模式-简单队列 [本文]RabbitMQ六种队列模式-工作队列RabbitMQ六种队列模式-发布订阅RabbitMQ六种队列模式-路由模式RabbitMQ六种队列模式-主 ...

  2. RabbitMQ六种队列模式-工作队列模式

    前言 RabbitMQ六种队列模式-简单队列RabbitMQ六种队列模式-工作队列 [本文]RabbitMQ六种队列模式-发布订阅RabbitMQ六种队列模式-路由模式RabbitMQ六种队列模式-主 ...

  3. RabbitMQ六种队列模式-发布订阅模式

    前言 RabbitMQ六种队列模式-简单队列RabbitMQ六种队列模式-工作队列RabbitMQ六种队列模式-发布订阅 [本文]RabbitMQ六种队列模式-路由模式RabbitMQ六种队列模式-主 ...

  4. RabbitMQ六种队列模式-路由模式

    前言 RabbitMQ六种队列模式-简单队列RabbitMQ六种队列模式-工作队列RabbitMQ六种队列模式-发布订阅RabbitMQ六种队列模式-路由模式 [本文]RabbitMQ六种队列模式-主 ...

  5. RabbitMQ六种队列模式-主题模式

    前言 RabbitMQ六种队列模式-简单队列RabbitMQ六种队列模式-工作队列RabbitMQ六种队列模式-发布订阅RabbitMQ六种队列模式-路由模式RabbitMQ六种队列模式-主题模式 [ ...

  6. RabbitMQ之消息模式(下)

    目的: RabbitMQ之消息模式(上):https://www.cnblogs.com/huangting/p/11994539.html 消费端限流 消息的ACK与重回队列 TTL消息 死信队列 ...

  7. 【RabbitMQ】六种模式与SpringBoot整合

    添加rabbitmq的依赖 <dependency> <groupId>org.springframework.boot</groupId> <artifac ...

  8. RabbitMQ六种工作模式有哪些?怎样用SpringBoot整合RabbitMQ

    目录 一.RabbitMQ入门程序 二.Work queues 工作模式 三.Publish / Subscribe 发布/订阅模式 四.Routing 路由模式 五.Topics 六.Header ...

  9. java面试一日一题:rabbitMQ的工作模式

    问题:请讲下rabbitMQ的工作模式 分析:该问题纯属概念题,需要掌握rabbtiMQ的基础知识,同时该题也是切入MQ的一个引子: 回答要点: 主要从以下几点去考虑, 1.rabbitMQ的基本概念 ...

随机推荐

  1. 非常easy学习的JQuery库 : (二) 选择器

    作用 选择器同意您对元素组或单个元素进行操作. 在前面的章节中,我们介绍了一些有关怎样选取 HTML 元素的实例. 关键点是学习 jQuery 选择器是怎样准确地选取您希望应用效果的元素. jQuer ...

  2. IntelliJ IDEA(2017)下载并破解

    idea激活,JetBrain旗下软件激活 我在修改这个博主的文章再添加了code码 http://blog.csdn.net/qq_24504453/article/details/77407329 ...

  3. PHP如何进阶,提升自己

    2017年6月15日14:32:51 今天看今日头条,刷到了一个话题?是:整天增删改查调接口,PHP程序员,如何突破职业瓶颈晋级? 晋级包括:职位晋级:技术能力晋级.当你的技术能力晋级了,职位晋级也就 ...

  4. iPhone缓存网络数据

    本文转载至 http://blog.csdn.net/wwang196988/article/details/7542918   在iPhone应用程序中,我们经常要用去网络下载一些文件,比如xml, ...

  5. task19-21

    [说明]理想是丰满的,现实很骨感,昨天还说今天有望干掉5个小任务,看来是没可能了,兜兜转转地做了一天也才完成下面的这些 一:今日完成 19.学习Spring,配置Spring和Junit 1)先安装一 ...

  6. Gone Fishing(贪心)

    Gone Fishing John is going on a fising trip. He has h hours available (1 ≤ h ≤ 16), and there are n ...

  7. elasticsearch从入门到出门-02-简单的CRUD

    操作背景: 电商网站上面的一个商品的增删改查: es 能接受的都是JSON格式的数据 Es 提供了一套简单的集群信息健康监控的api GET /_cat/health?v   epoch      t ...

  8. Full卷积 Same卷积 Vaild卷积

  9. office2013安装/激活

    ed2k://|file|cn_office_professional_plus_2013_x64_dvd_1134006.iso|914106368|E5FBAE9EE9CB35D5E777EA78 ...

  10. linux shell脚本: 自动监控网站状态并发送提醒邮件

    1.创建监控脚本:$ vi /alidata/shell/webcheck.sh #!/bin/sh weblist="/alidata/shell/weblist.txt" my ...