RabbitMQ安装和测试小记
2013-08-05 11:18
274 查看
操作系统CentOS x86_64 5.4
安装Erlang
可以到www.erlang.org去下载最新的源码,编译安装。
不过RabbitMQ的官方文档建议使用yum安装。
EPEL是啥?
参见http://fedoraproject.org/wiki/EPEL/FAQ#howtouse
安装步骤:
wget http://download.fedora.redhat.com/pub/epel/5/x86_64/epel-release-5-3.noarch.rpm
rpm -Uvh epel-release-5-3.noarch.rpm
yum install erlang
安装RabbitMQ
到 http://www.rabbitmq.com/server.html 下载
rabbitmq-server-1.7.2-1.x86_64.rpm
然后
rpm -ivh rabbitmq-server-1.7.2-1.x86_64.rpm
运行RabbitMQ
/etc/init.d/rabbitmq-server start
注意
RabbitMQ使用了5672端口,修改iptables把5672端口打开。
测试
在官网下载java的客户端rabbitmq-java-client-bin-1.7.2.tar.gz
消息发布
[java]
view plaincopy
import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Connection;
import com.rabbitmq.client.ConnectionFactory;
import com.rabbitmq.client.ConnectionParameters;
import com.rabbitmq.client.MessageProperties;
public class PublishTest {
public static void main(String[] args) {
try {
ConnectionParameters params = new ConnectionParameters();
params.setUsername("guest");
params.setPassword("guest");
params.setVirtualHost("/");
params.setRequestedHeartbeat(0);
ConnectionFactory factory = new ConnectionFactory(params);
Connection conn = factory.newConnection("192.168.56.110", 5672);
Channel channel = conn.createChannel();
channel.exchangeDeclare("exchange1", "direct", true);
channel.queueDeclare("queue1", true);
channel.queueBind("queue1", "exchange1", "key1");
byte[] messageBodyBytes = "Hello, world!".getBytes();
channel.basicPublish("exchange1", "key1", MessageProperties.PERSISTENT_TEXT_PLAIN, messageBodyBytes);
channel.close();
conn.close();
System.out.println("Over.");
} catch (Exception e) {
e.printStackTrace();
}
}
}
消息接收
[java]
view plaincopy
import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Connection;
import com.rabbitmq.client.ConnectionFactory;
import com.rabbitmq.client.ConnectionParameters;
import com.rabbitmq.client.GetResponse;
public class ConsumerTest {
public static void main(String[] args) {
try {
ConnectionParameters params = new ConnectionParameters();
params.setUsername("guest");
params.setPassword("guest");
params.setVirtualHost("/");
params.setRequestedHeartbeat(0);
ConnectionFactory factory = new ConnectionFactory(params);
Connection conn = factory.newConnection("192.168.56.110", 5672);
Channel channel = conn.createChannel();
boolean noAck = false;
GetResponse response = channel.basicGet("queue1", noAck);
if (response == null) {
System.out.println("No message!");
} else {
byte[] body = response.getBody();
long deliveryTag = response.getEnvelope().getDeliveryTag();
String str = new String(body);
System.out.println(str);
channel.basicAck(deliveryTag, false); // acknowledge receipt of the message
}
channel.close();
conn.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}
<
a9b7
script>
$(".MathJax").remove();
安装Erlang
可以到www.erlang.org去下载最新的源码,编译安装。
不过RabbitMQ的官方文档建议使用yum安装。
EPEL是啥?
参见http://fedoraproject.org/wiki/EPEL/FAQ#howtouse
安装步骤:
wget http://download.fedora.redhat.com/pub/epel/5/x86_64/epel-release-5-3.noarch.rpm
rpm -Uvh epel-release-5-3.noarch.rpm
yum install erlang
安装RabbitMQ
到 http://www.rabbitmq.com/server.html 下载
rabbitmq-server-1.7.2-1.x86_64.rpm
然后
rpm -ivh rabbitmq-server-1.7.2-1.x86_64.rpm
运行RabbitMQ
/etc/init.d/rabbitmq-server start
注意
RabbitMQ使用了5672端口,修改iptables把5672端口打开。
测试
在官网下载java的客户端rabbitmq-java-client-bin-1.7.2.tar.gz
消息发布
[java]
view plaincopy
import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Connection;
import com.rabbitmq.client.ConnectionFactory;
import com.rabbitmq.client.ConnectionParameters;
import com.rabbitmq.client.MessageProperties;
public class PublishTest {
public static void main(String[] args) {
try {
ConnectionParameters params = new ConnectionParameters();
params.setUsername("guest");
params.setPassword("guest");
params.setVirtualHost("/");
params.setRequestedHeartbeat(0);
ConnectionFactory factory = new ConnectionFactory(params);
Connection conn = factory.newConnection("192.168.56.110", 5672);
Channel channel = conn.createChannel();
channel.exchangeDeclare("exchange1", "direct", true);
channel.queueDeclare("queue1", true);
channel.queueBind("queue1", "exchange1", "key1");
byte[] messageBodyBytes = "Hello, world!".getBytes();
channel.basicPublish("exchange1", "key1", MessageProperties.PERSISTENT_TEXT_PLAIN, messageBodyBytes);
channel.close();
conn.close();
System.out.println("Over.");
} catch (Exception e) {
e.printStackTrace();
}
}
}
消息接收
[java]
view plaincopy
import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Connection;
import com.rabbitmq.client.ConnectionFactory;
import com.rabbitmq.client.ConnectionParameters;
import com.rabbitmq.client.GetResponse;
public class ConsumerTest {
public static void main(String[] args) {
try {
ConnectionParameters params = new ConnectionParameters();
params.setUsername("guest");
params.setPassword("guest");
params.setVirtualHost("/");
params.setRequestedHeartbeat(0);
ConnectionFactory factory = new ConnectionFactory(params);
Connection conn = factory.newConnection("192.168.56.110", 5672);
Channel channel = conn.createChannel();
boolean noAck = false;
GetResponse response = channel.basicGet("queue1", noAck);
if (response == null) {
System.out.println("No message!");
} else {
byte[] body = response.getBody();
long deliveryTag = response.getEnvelope().getDeliveryTag();
String str = new String(body);
System.out.println(str);
channel.basicAck(deliveryTag, false); // acknowledge receipt of the message
}
channel.close();
conn.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}
<
a9b7
script>
$(".MathJax").remove();
相关文章推荐
- [AMQP] RabbitMQ 安装与测试
- RabbitMQ入门安装及测试
- ubuntu12.04 安装 rabbitmq与测试
- RabbitMQ入门安装及测试
- RabbitMQ安装测试学习过程简单的记录
- 需要安装 MongoDB,RabbitMQ,或者 MySQL?使用 Docker 来简化开发和测试
- 需要安装 MongoDB,RabbitMQ,或者 MySQL?使用 Docker 来简化开发和测试
- RabbitMQ安装和简单测试示例(windows版本)
- RabbitMQ安装和测试小记
- RabbitMQ安装与测试
- RabbitMQ 消息队列(centos安装与php下代码测试)
- RabbitMQ安装与测试
- [AMQP] RabbitMQ 安装与测试
- 关于安装及测试rabbitmq-c出现的问题
- VMware中使用CentOS安装并测试Hadoop
- 概括描述Ubuntu PHP安装测试(转)
- Dataguard实战(7):RHEL4.5 安装 Oracle10G Dataguard 多个备库 - 物理备库数据同步测试
- RabbitMQ-二、安装部署
- maven入门浅析(一)-----maven安装、配置、创建项目骨架、编译、测试、打包、运行
- ubuntu12.04下nfs安装、配置、测试