内容简介:RabbitMQ是一种消息中间件,它接收和转发消息,可以理解为邮局。只是RabbitMQ接收,处理,转发的是二进制的数据,邮局处理的一般为纸。channel.queueDeclare(String queue, boolean durable, boolean exclusive, boolean autoDelete, Map<String, Object> arguments)生产和消费消息都是通过channel的。channel指定具体为那个queue
RabbitMQ是一种消息中间件,它接收和转发消息,可以理解为邮局。只是RabbitMQ接收,处理,转发的是二进制的数据,邮局处理的一般为纸。
基本概念
- Producer(生产者): 发送消息的程序
- Consumer(消费者):接收消息的程序
- Queue(队列):像邮局的信箱,在RabbitMQ内部,同一个消息流只能存在一个Queue中,队列只受主机内存,磁盘的大小限制。 生产者像Queue中发送消息,消费者从Queue中取出消息
安装
- Mac OS
brew install rabbitmq 复制代码
- CentOS7
cat << EOF > /etc/yum.repos.d/rabbit.repo [rabbitmq-erlang] name=rabbitmq-erlang baseurl=https://dl.bintray.com/rabbitmq/rpm/erlang/21/el/7 gpgcheck=1 gpgkey=https://dl.bintray.com/rabbitmq/Keys/rabbitmq-release-signing-key.asc repo_gpgcheck=0 enabled=1 EOF yum install rabbitmq-server 复制代码
Hello World
- 启动RabbitMQ
rabbitmq-server 复制代码
- 添加 Java 依赖库
<dependency>
<groupId>ch.qos.logback</groupId>
<artifactId>logback-classic</artifactId>
<version>1.2.3</version>
</dependency>
<dependency>
<groupId>com.rabbitmq</groupId>
<artifactId>amqp-client</artifactId>
<version>4.2.0</version>
</dependency>
复制代码
- 编写生产者
/**
* @author aihe 2018/9/6
*/
public class Producer {
private final static String QUEUE_NAME = "hello1";
public static void main(String[] args) throws IOException, TimeoutException {
ConnectionFactory connectionFactory = new ConnectionFactory();
connectionFactory.setHost("127.0.0.1");
Connection connection = connectionFactory.newConnection();
Channel channel = connection.createChannel();
channel.queueDeclare(QUEUE_NAME, false, false, false, null);
String message = "Hello World";
channel.basicPublish("", QUEUE_NAME, null, message.getBytes());
System.out.println("发送消息:" + message);
try {
channel.close();
connection.close();
} catch (TimeoutException e) {
e.printStackTrace();
}
}
}
复制代码
channel.queueDeclare(String queue, boolean durable, boolean exclusive, boolean autoDelete, Map<String, Object> arguments)
- queue 要声明的队列名称
- durable 是否长久保存,true的话,服务器重启的时候,queue仍然存在
- exclusive 是否声明一个排他的队列, 也就是只有当前的channel才能监听这个queue
- autoDelete 当服务器不在使用这个queue的时候,自动删除它
- arguments 其它的属性
生产和消费消息都是通过channel的。channel指定具体为那个queue
- 编写消费者
/**
* @author aihe 2018/9/6
*/
public class Consumer {
private final static String QUEUE_NAME = "hello1";
public static void main(String[] args) throws IOException, TimeoutException {
ConnectionFactory factory = new ConnectionFactory();
factory.setHost("127.0.0.1");
Connection connection = factory.newConnection();
Channel channel = connection.createChannel();
channel.queueDeclare(QUEUE_NAME, false, false, false, null);
System.out.println(" [*] 正在等待消息. 退出按 CTRL+C");
com.rabbitmq.client.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(" 接收消息:'" + message + "'");
}
};
channel.basicConsume(QUEUE_NAME, true, consumer);
}
}
复制代码
- 运行
生产者
消费者
额外
- 我们可以安装rabbitMQ的图形管理界面
// 查看有哪些插件 rabbitmq-plugins list // 启用管理界面 rabbitmq-plugins enable rabbitmq_management 复制代码
- 进入管理界面 进入地址: http://127.0.0.1:15672/ 账号和密码:guest guest
这些参数大部分都是可以从rabbitmqctl命令获得的。
以上所述就是小编给大家介绍的《RabbitMQ快速入门》,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对 码农网 的支持!
本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们。
Compilers
Alfred V. Aho、Monica S. Lam、Ravi Sethi、Jeffrey D. Ullman / Addison Wesley / 2006-9-10 / USD 186.80
This book provides the foundation for understanding the theory and pracitce of compilers. Revised and updated, it reflects the current state of compilation. Every chapter has been completely revised ......一起来看看 《Compilers》 这本书的介绍吧!