本地部署RabbitMQ
一.下载RabbitMQ管理工具
1.windows下载地址:
http://www.rabbitmq.com/install-windows.html
2.Linux,BSD, UNIX下载地址:
①:Debian and Ubuntu
http://www.rabbitmq.com/install-debian.html#downloads
②:RHEL, CentOS, Fedora, openSUSE
http://www.rabbitmq.com/install-rpm.html
③:Generic UNIX Build
http://www.rabbitmq.com/install-generic-unix.html
1、家庭用途可选择版本有:Linux Mint、Ubuntu、OpenSUSE、Fedora、PC-BSD
2、商业用途可选择版本有:Debian、RHEL、CentOS
4、理想用途可选择版本有:FreeBSD、OpenBSD、Solaris、OpenSolaris
二.下载Erlang(安装RabbitMQ需要安装Erlang)
1.安装Erlang时需要注意与RabbitMQ的版本是否对应。
http://www.rabbitmq.com/which-erlang.html
2.下载地址
http://www.erlang.org/downloads
3.安装Erlang及环境配置
①:安装完成之后需要配置Erlang环境变量
②:测试环境变量是否配置成功
三、安装RabbitMQ并启动
1.启动RabbitMQ
cmd到RabbitMQ的安装目录下的sbin目录执行命令:
rabbitmq-plugins.bat enable rabbitmq_management
2.打开RabbitMQ管理地址
四.测试RabbitMQ
1.代码依赖jar包
①:amqp-client
②:rabbitmq-client
http://maven.outofmemory.cn/com.rabbitmq/
本地测试不需要设置IP、端口号、账号、密码,如果修改过以上内容则需要重新指定
2.生产者代码
import com.rabbitmq.client.ConnectionFactory;
import com.rabbitmq.client.Connection;
import com.rabbitmq.client.Channel;
public class testRabbitMQProducer {
private final static String QUEUE_NAME = "RabbitMQ_Test"; // 消息队列名
public static void main(String[] argv) throws Exception {
// 创建连接连接到RabbitMQ
ConnectionFactory factory = new ConnectionFactory();
// 设置ip
//factory.setHost("localhost");
//设置端口
//factory.setPort(5672);
//设置用户名
//factory.setUsername("guest");
//设置密码
//factory.setPassword("guest");
//设置url(包括ip、端口、用户名、密码)
//factory.setUri("amqp://guest:guest@localhost:5672");
// 创建一个连接
Connection connection = factory.newConnection();
// 创建一个频道
Channel channel = connection.createChannel();
// 指定一个队列
channel.queueDeclare(QUEUE_NAME, false, false, false, null);
String message = "hello";
// 往队列中发出一条消息
channel.basicPublish("", QUEUE_NAME, null, message.getBytes());
System.out.println(" [x] Sent '" + message + "'");
// 关闭频道和连接
channel.close();
connection.close();
}
}
3.消费者代码
import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Connection;
import com.rabbitmq.client.ConnectionFactory;
import com.rabbitmq.client.QueueingConsumer;
//消费者
public class testRabbitMQConsumer {
private final static String QUEUE_NAME = "RabbitMQ_Test"; // 消息队列名
public static void main(String[] argv) throws Exception {
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(" [*] Waiting for messages. To exit press CTRL+C");
// 创建队列消费者
QueueingConsumer consumer = new QueueingConsumer(channel);
// 指定消费队列
channel.basicConsume(QUEUE_NAME, true, consumer);
while (true) { // 消费者程序运行开着 如果生产者新增了数据会自动获取
Thread.sleep(500);
// nextDelivery是一个阻塞方法(内部实现其实是阻塞队列的take方法)
QueueingConsumer.Delivery delivery = consumer.nextDelivery();
String message = new String(delivery.getBody());
System.out.println("'[x] Received '" + message);
}
}
}