RabbitMQ要实现Hello World,其实也很简单。只需一个服务器来发送消息,另外有个客户端接收消息即可。
整体的设计流程如下:
消息生产者发送Hello到消息队列,消息消费者从队列中接收消息。
下载依赖Jar包
RabbitMQ要用Java实现发送消息,就必须使用Java客户端库。目前RabbizMQ的Java客户端库最新版为为 3.5.5 。可以从Maven仓库下载,也可以直接去官网下载。
<dependency>
<groupId>com.rabbitmq</groupId>
<artifactId>amqp-client</artifactId>
<version>3.5.5</version>
</dependency>
使用Java创建发送者
package com.favccxx.favrabbit;
import java.io.IOException;
import java.util.concurrent.TimeoutException;
import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Connection;
import com.rabbitmq.client.ConnectionFactory;
public class Sender {
private final static String QUEUE_NAME = “hello”;
public static void main(String[] argv) 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 RabbitMQ World!”;
channel.basicPublish(“”, QUEUE_NAME, null, message.getBytes());
System.out.println(” [x] Sent ‘” + message + “‘”);
}
}
RabbitMQ控制台监控消息队列
运行上面的代码,从RabbitMQ控制台就可以看到刚刚发送的消息。
使用Java接收消息
package com.favccxx.favrabbit;
import java.io.IOException;
import java.util.concurrent.TimeoutException;
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 Receiver {
private final static String QUEUE_NAME = “hello”;
public static void main(String[] argv)
throws java.io.IOException, java.lang.InterruptedException, TimeoutException {
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”);
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 + “‘”);
}
};
channel.basicConsume(QUEUE_NAME, true, consumer);
}
}
分别运行消息发送者和消息接收者,从RabbitMQ控制台可以看到实时的状况。
到此为止,RabbitMQ的Hello World工作就结束了,是不是对消息队列有了一些好感了呢?
CentOS 5.6 安装RabbitMQ http://www.linuxidc.com/Linux/2013-02/79508.htm
RabbitMQ客户端C++安装详细记录 http://www.linuxidc.com/Linux/2012-02/53521.htm
用Python尝试RabbitMQ http://www.linuxidc.com/Linux/2011-12/50653.htm
RabbitMQ集群环境生产实例部署 http://www.linuxidc.com/Linux/2012-10/72720.htm
Ubuntu下PHP + RabbitMQ使用 http://www.linuxidc.com/Linux/2010-07/27309.htm
在CentOS上安装RabbitMQ流程 http://www.linuxidc.com/Linux/2011-12/49610.htm
RabbitMQ概念及环境搭建 http://www.linuxidc.com/Linux/2014-12/110449.htm
RabbitMQ入门教程 http://www.linuxidc.com/Linux/2015-02/113983.htm
RabbitMQ 的详细介绍:请点这里
RabbitMQ 的下载地址:请点这里
本文永久更新链接地址:http://www.linuxidc.com/Linux/2015-10/124089.htm