RabbitMQ遵循的是AMQP协议,许可证the Mozilla Public License v1.1和 the GNU General Public License v2.
RabbitMQ由ERLANG实现的,需要ERLANG VM才能运行
首先安装Erlang,下载http://www.erlang.org/download/otp_win32_R16B02.exe,设置环境变量
安装RabbitMQ Server,下载rabbitmq-server-windows-3.2.1.zip直接解压到目录即可,rabbitmq_server-3.2.1\sbin目录是rabbitserver的执行命令目录
rabbitmq-server.bat start broke as an application.
rabbitmq-service.bat manages the service and starts the broker.
rabbitmqctl.bat manages a running broker.
下载安装RabbitMQ Client,下载Client Library,http://www.rabbitmq.com/releases/rabbitmq-java-client/v3.2.1/rabbitmq-java-client-bin-3.2.1.zip,解压到目录。
利用Client Library编写HelloWord示例,Product发送HelloWord到Queue,consume从Queue中收取消息
消息生产者Send
package com.prometon.yang.rabbitmq;
import com.rabbitmq.client.ConnectionFactory;
import com.rabbitmq.client.Connection;
import com.rabbitmq.client.Channel;
public class Send {
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);
String message = "Hello World!";
channel.basicPublish("", QUEUE_NAME, null, message.getBytes());
System.out.println(" [x] Sent '" + message + "'");
channel.close();
connection.close();
}
}
消息消费者Recv
package com.prometon.yang.rabbitmq;
import com.rabbitmq.client.ConnectionFactory;
import com.rabbitmq.client.Connection;
import com.rabbitmq.client.Channel;
import com.rabbitmq.client.QueueingConsumer;
public class Recv {
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 + "'");
}
}
}
执行程序:
[x] Sent 'Hello World!'
[*] Waiting for messages. To exit press CTRL+C
[x] Received 'Hello World!'