版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/qq_20120669/article/details/72497533
RabbitMQ 的helloword 的demo。
加入到项目的pom.xml文件中。
<!-- https://mvnrepository.com/artifact/com.rabbitmq/amqp-client -->
<dependency>
<groupId>com.rabbitmq</groupId>
<artifactId>amqp-client</artifactId>
<version>4.1.0</version>
</dependency>
配置发送端:
package com.lk.spider.service.practice_java.rabbitmqtest;
import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Connection;
import com.rabbitmq.client.ConnectionFactory;
import java.io.IOException;
import java.util.concurrent.TimeoutException;
/**
* Created by LK on 2017/5/18.
*/
public class Send {
private final static String QUEUE_NAME = "hello";
public static void main(String[] args) throws IOException, TimeoutException {
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.lk.spider.service.practice_java.rabbitmqtest;
import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Connection;
import com.rabbitmq.client.ConnectionFactory;
import com.rabbitmq.client.QueueingConsumer;
/**
* Created by LK on 2017/5/18.
*/
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 + "'");
}
}
}
执行结果:
下面是原理图以及解释:
这个图就能说明全部了。
Publisher 是生产者,是Message的生产者,publisher这个Clients产生了一些Message
Consumer是消费者,Publisher产生的Message,最终到达Consumer这个Clients进行消费。
Exchange:指定消息按什么规则,路由到哪个Queue,Message消息先要到达Exchange,在Server中承担着
装载Message,是Message的容器,等待被消费出去。
Routing Key:在Exchange和Queue之间 的规则,Exchange通过定义好的Routing key将Message送到对应的Queue中去,是Exchange和Queue之间的桥梁。
Broker:就是一个rabbitmq server。
VirualHost: 虚拟主机,一个Broker里面可以有多个virtualHost,它的作用是用作不同用户的权限分离。
Connection:
publisher/Consumer和Broker之间的TCP连接。
Channel:在Connection里面建立的,由于老建立Connection开销过大。
具体的步骤:
1、获取Conection
ConnectionFactory factory = new ConnectionFactory();
factory.setHost("localhost");
Connection connection = factory.newConnection();
2、获取Channel
Channel channel = connection.createChannel();
3、定义Exchange 和Queue
channel.queueDeclare(QUEUE_NAME, false, false, false, null);
4、使用一个RoutingKey将queue 绑定到一个Exchange上。
5、通过制定一个Exchange和一个RoutingKey来将消息发送到对应的queue上。
channel.basicPublish("", QUEUE_NAME, null, message.getBytes());
6、消费者
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 + "'");
}
消费者,只关心Queue就行。