在下图中,“P”是我们的生产者,“C”是我们的消费者。中间的框是队列 - RabbitMQ代表消费者的消息缓冲区。

本例使用maven构建项目,在pom.xml中添加一下依赖

<dependencies>
<dependency>
<groupId>com.rabbitmq</groupId>
<artifactId>amqp-client</artifactId>
<version>4.1.</version>
</dependency> <dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
<version>1.7.</version>
</dependency> <dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-simple</artifactId>
<version>1.7.</version>
</dependency> </dependencies>

接下来通过生产者叫rabbitMQ服务器发送一个消息,这个消息被缓存在了rabbitMQ的队列中,生产者相当于发件人,rabbitMQ相当于申通快递,消费者相当于接收人

定义一个发件人Send.java

 package com.rabbitMQ;

 import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Connection;
import com.rabbitmq.client.ConnectionFactory; /**
* 发送消息后,如果服务器的队列中没有可发现的消费者,那么就会放在队列中等待消费者
* 可以使用rabbitmqctl.bat list_queues命令查看某个队列的消息个数
* @author may
*
*/
public class Send { private final static String QUEUE_NAME = "hello"; public static void main(String[] args) throws Exception {
//创建连接工厂
ConnectionFactory factory = new ConnectionFactory();
//设置连接rabbitMQ服务器的ip
factory.setHost("localhost");
// factory.setPort(5672);
//创建一个连接到服务器的链接
Connection connection = factory.newConnection();
//创建连接通道
Channel channel = connection.createChannel(); /**
* 定义一个队列
* queueDeclare(String queue,//队列的名字
* boolean durable, //定义一个耐用队列,即持久化,如果RabbitMQ服务挂机,重启后还能恢复这个队列。
* boolean exclusive, //排他队列,只能在当前链接中可用,如果这个连接关闭,那么也就无效了。
* boolean autoDelete,//在连接断开后自动删除队列。
* Map<String, Object> arguments)
*/
channel.queueDeclare(QUEUE_NAME, false, false, false, null); // channel.queueDeclareNoWait(queue, durable, exclusive, autoDelete, arguments); String message = "Hello World!";
/**
* Parameters:
* exchange: the exchange to publish the message to 转发器的名字
* routingKey: the routing key 路由key,这里就是队列的名字,表示要发送到这个队列上
* props: other properties for the message - routing headers etc//这个信息的属性
* body: the message body 要发送的信息
*/
channel.basicPublish("", QUEUE_NAME, null, message.getBytes()); System.out.println(" [x] Sent '" + message + "'"); channel.close(); connection.close();
} }

接收消息,消费者从队列中获取消息

 
定义一个Recv.java
 
package com.rabbitMQ;

import java.io.IOException;

import com.rabbitmq.client.AMQP;
import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Connection;
import com.rabbitmq.client.ConnectionFactory;
import com.rabbitmq.client.Consumer;
import com.rabbitmq.client.DefaultConsumer;
import com.rabbitmq.client.Envelope; public class Recv {
//队列名要和发送者定义的队列一样的名字,它会告诉rabbitMQ它要再哪个队列中获取消息
private final static String QUEUE_NAME = "hello"; public static void main(String[] argv) throws Exception {
ConnectionFactory factory = new ConnectionFactory();
factory.setHost("localhost");
Connection connection = factory.newConnection();
Channel channel = connection.createChannel();
/**
* queue the name of the queue
durable true if we are declaring a durable queue (the queue will survive a server restart)
exclusive true if we are declaring an exclusive queue (restricted to this connection)
autoDelete true if we are declaring an autodelete queue (server will delete it when no longer in use)
arguments other properties (construction arguments) for the queue
*/
channel.queueDeclare(QUEUE_NAME, false, false, false, null);
System.out.println(" [*] Waiting for messages. To exit press CTRL+C");
//定义一个消费者
Consumer consumer = new DefaultConsumer(channel) {
@Override
public void handleDelivery(String consumerTag, Envelope envelope, AMQP.BasicProperties properties,
byte[] body) throws IOException {
String message = new String(body, "UTF-8");
System.out.println(" [x] Received '" + message + "'");
}
};
//异步
/**
* queue the name of the queue 队列名
* 如果autoAck设置为true,这个Consumer在收到消息之后会马上返回ack。服务器将立即删除在内存中排队的消息
* false 在消息的任务处理完之后再手动ack,如果正在处理的时候,消费者发生异常,就不能返回ack,那么就不会删除这个消息,等待发现其他消费者发送给他们
autoAck true if the server should consider messages acknowledged once delivered; false if the server should expect explicit acknowledgements
callback an interface to the consumer object
*/
channel.basicConsume(QUEUE_NAME, true, consumer); //rabbitmqctl.bat list_queues 命令可以列出当前有多少个队列
}
}

运行Send,向rabbitMQ发送了一个消息,我们可以通过rabbitMQ的一个工具查看当前的队列中有多少个消息在排队。

hello是队列的名字,1表示当前的队列中存在一个消息等待着被消费者处理

这个时候,启动消费者去取快递,启动后输出一下内容,成功打印出了Hello World

[*] Waiting for messages. To exit press CTRL+C
[x] Received 'Hello World!'

再次查看rabbitMQ中hello队列中的消息个数

消息个数为零。

rabbitMQ_helloworld(一)的更多相关文章

随机推荐

  1. 3023Java_控制语句

    控制语句 0.前定义 语句块(有时叫做复合语句),是用花括号扩起的任意数量的简单Java语句. 块确定了局部变量的作用域.块中的程序代码,作为一个整体,是要被一起执行的. 块可以被嵌套在另一个块中,但 ...

  2. Nginx+Keepalived部署流程

    环境介绍 1)LB01 Hostname:lb01.example.com VIP:192.168.3.33(eth0:0) IP:192.168.3.31(eth0) OS:Centos 7 2)L ...

  3. React躬行记(4)——生命周期

    组件的生命周期(Life Cycle)包含三个阶段:挂载(Mounting).更新(Updating)和卸载(Unmounting),在每个阶段都会有相应的回调方法(也叫钩子)可供选择,从而能更好的控 ...

  4. Spring-Boot + MyBatis-Plus 踩坑记录

    这两天在学SpringBoot+MyBatis的开发,配置开发环境和DEMO的过程中踩了很多坑,在这里记录一下. 我的开发环境是idea + JDK 1.8.0.211. 首先展示一下demo的项目整 ...

  5. HBase 学习之路(三)—— HBase基本环境搭建

    一.安装前置条件说明 1.1 JDK版本说明 HBase 需要依赖JDK环境,同时HBase 2.0+ 以上版本不再支持JDK 1.7 ,需要安装JDK 1.8+ .JDK 安装方式见本仓库: Lin ...

  6. vagramt中同步文件,webpack不热加载

    这是一篇参考文章:https://webpack.js.org/guides/development-vagrant/ 在使用vue-cli+webpack构建的项目中,如何使用vagrant文件同步 ...

  7. ajax:error:function (XMLHttpRequest, textStatus, errorThrown) 中status、readyState和textStatus状态意义

    textStatus: "timeout", 超时 "error", 出错 "notmodified" , 未修改 "parser ...

  8. python爬虫之快速对js内容进行破解

    python爬虫之快速对js内容进行破解 今天介绍下数据被js加密后的破解方法.距离上次发文已经过去半个多月了,我写文章的主要目的是把从其它地方学到的东西做个记录顺便分享给大家,我承认自己是个懒猪.不 ...

  9. 17.Linux命令

    1.网络配置 setup       配置 ifup eth0  启动网卡 文件保存在  /etc/sysconfig/network-scripts/ifcfg-eth0,修改ifcfg-eth0配 ...

  10. springboot不同环境打包

    1. 场景描述 springboot+maven打包,项目中经常用到不同的环境下打包不同的配置文件,比如连接的数据库.配置文件.日志文件级别等都不一样. 2. 解决方案 在pom.xml文件中定义 2 ...