RabbitMQ学习一. 在windows下安装配置
import java.util.concurrent.TimeoutException;
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 java.io.IOException, TimeoutException {
/* 使用工厂类建立Connection和Channel,并且设置参数 */
ConnectionFactory factory = new ConnectionFactory();
factory.setHost("localhost");// MQ的IP
factory.setPort(5672);// MQ端口
factory.setUsername("guest");// MQ用户名
factory.setPassword("guest");// MQ密码
Connection connection = factory.newConnection();
Channel channel = connection.createChannel();
/* 创建消息队列,并且发送消息 */
channel.queueDeclare(QUEUE_NAME, false, false, false, null);
String message = "Hello World!"+System.currentTimeMillis();
channel.basicPublish("", QUEUE_NAME, null, message.getBytes());
System.out.println(" [x] Sent '" + message + "'");
/* 关闭连接 */
channel.close();
connection.close();
}
}
1.下载并安装erlang,http://www.erlang.org/download.html,最新版是R15B01(5.9.1)。由于我机器是64位的Win7,所以找到otp_win64_R15B01.exe下载并安装。
2.配置环境变量
增加一个系统环境变量ERLANG_HOME配置为C:\Program Files\erl5.9.1
3.下载RabbitMQ,最新版是2.8.1,http://www.rabbitmq.com/releases/rabbitmq-server/v2.8.1/rabbitmq-server-windows-2.8.1.zip。
4.解压RabbitMQ的zip包,运行sbin/rabbitmq-server.bat,启动RabbitMQ服务器。
5.测试安装是否成功
在 dos命令下进入rabbitmq的sbin目录
,输入 rabbitmq-server start 启动mq服务
Rabbit还自带监控功能.
cmd进到sbin目录,键入rabbitmq-plugins enable rabbitmq_management启用监控管理,然后重启Rabbitmq服务器。打开网址http://localhost:55672,用户名和密码都是guest。
cmd进到sbin目录,键入rabbitmq-plugins enable rabbitmq_management启用监控管理,然后重启Rabbitmq服务器。打开网址http://localhost:55672,用户名和密码都是guest。
node.js 下的rabbitmq api地址:https://github.com/postwait/node-amqp
rabbitmq 安装配置和管理 http://blog.csdn.net/zhangxinrun/article/details/8178262
测试类:
发生消息到MQ
import java.util.concurrent.TimeoutException;
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 java.io.IOException, TimeoutException {
/* 使用工厂类建立Connection和Channel,并且设置参数 */
ConnectionFactory factory = new ConnectionFactory();
factory.setHost("localhost");// MQ的IP
factory.setPort(5672);// MQ端口
factory.setUsername("guest");// MQ用户名
factory.setPassword("guest");// MQ密码
Connection connection = factory.newConnection();
Channel channel = connection.createChannel();
/* 创建消息队列,并且发送消息 */
channel.queueDeclare(QUEUE_NAME, false, false, false, null);
String message = "Hello World!"+System.currentTimeMillis();
channel.basicPublish("", QUEUE_NAME, null, message.getBytes());
System.out.println(" [x] Sent '" + message + "'");
/* 关闭连接 */
channel.close();
connection.close();
}
}
接受mq消息
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(" [x1] Received '" + message + "'");
}
}
}
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(" [x1] Received '" + message + "'");
}
}
}