rabbitmq学习1:hello world
程序员文章站
2022-07-13 15:46:40
...
rabbitMQ是一个在AMQP基础上完整的,可服用的企业消息系统。他遵循Mozilla Public License 开源协议。
关于amqp可参考http://www.oschina.net/p/rabbitmq/
rabbitmq是一个消费的代理;通过生产者客户端生产一个信息,转送给消费者客户端;在这个传输过程中,根据你的需要可以经过路由、缓冲、持久化来得到这个消息。
先通过一个例子开始:通过rabbitmq输出"Hello World!"
其中P代表生产者、C表示消费者、中间红色部分代表消息队列
生产者客户端的发送消息程序如下:
- package com.abin.test;
- import java.io.IOException;
- import com.rabbitmq.client.Channel;
- import com.rabbitmq.client.Connection;
- import com.rabbitmq.client.ConnectionFactory;
- public class Send {
- private final static String QUEUE_NAME = "hello";
- public static void main(String[] args) throws IOException {
- ConnectionFactory factory = new ConnectionFactory();
- factory.setHost("localhost");
- Connection connection = factory.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(" [x] Sent '" + message + "'");
- channel.close();
- connection.close();
- }
- }
package com.abin.test; import java.io.IOException; import com.rabbitmq.client.Channel; import com.rabbitmq.client.Connection; import com.rabbitmq.client.ConnectionFactory; public class Send { private final static String QUEUE_NAME = "hello"; public static void main(String[] args) throws IOException { ConnectionFactory factory = new ConnectionFactory(); factory.setHost("localhost"); Connection connection = factory.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(" [x] Sent '" + message + "'"); channel.close(); connection.close(); } }
运行结果如下:
- [x] Sent 'Hello World!'
[x] Sent 'Hello World!'
消费者客户端接收消息程序如下:
- package com.abin.test;
- import com.rabbitmq.client.Channel;
- import com.rabbitmq.client.Connection;
- import com.rabbitmq.client.ConnectionFactory;
- import com.rabbitmq.client.QueueingConsumer;
- public class Reqv {
- 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();
- channel.queueDeclare(QUEUE_NAME, false, false, false, null);
- System.out.println(" [*] Waiting for messages. To exit press CTRL+C");
- QueueingConsumer consumer = new QueueingConsumer(channel);
- channel.basicConsume(QUEUE_NAME, true, consumer);
- while (true) {
- QueueingConsumer.Delivery delivery = consumer.nextDelivery();
- String message = new String(delivery.getBody());
- System.out.println(" [x] Received '" + message + "'");
- }
- }
- }
package com.abin.test; import com.rabbitmq.client.Channel; import com.rabbitmq.client.Connection; import com.rabbitmq.client.ConnectionFactory; import com.rabbitmq.client.QueueingConsumer; public class Reqv { 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(); channel.queueDeclare(QUEUE_NAME, false, false, false, null); System.out.println(" [*] Waiting for messages. To exit press CTRL+C"); QueueingConsumer consumer = new QueueingConsumer(channel); channel.basicConsume(QUEUE_NAME, true, consumer); while (true) { QueueingConsumer.Delivery delivery = consumer.nextDelivery(); String message = new String(delivery.getBody()); System.out.println(" [x] Received '" + message + "'"); } } }
运行程序得到的结果如下:
- [*] Waiting for messages. To exit press CTRL+C
- [x] Received 'Hello World!'
[*] Waiting for messages. To exit press CTRL+C [x] Received 'Hello World!'
如果消费者出现“[x] Received 'Hello World!'”说明已接收到此消息信息。
推荐阅读
-
Go从零学习-01篇-Hello,world
-
WORDPRESS插件开发学习(一)HELLO WORLD,wordpresshello_PHP教程
-
【碎片知识(1)· 计算机视觉基础】利用OpenCV实现图像的“Hello, world!”
-
go基础1:Hello world与变量声明
-
Linux驱动学习1.hello world;
-
Android学习笔记(一)环境安装及第一个hello world
-
Go语言学习笔记(1)——Hello World!
-
Python语言程序设计(第1周)_Hello World的条件输出
-
Android程序员Objective-C 学习——Hello World
-
1. Vue 入门教程之 Hello World