rabbitMq使用(mac平台)
1.下载
wget http://www.rabbitmq.com/releases/rabbitmq-server/v3.5.3/rabbitmq-server-mac-standalone-3.5.3.tar.gz
2.启动
adeMacBook-Pro:rabbitmq_server-3.5.3 apple$ sbin/rabbitmq-server RabbitMQ 3.5.3. Copyright (C) 2007-2014 GoPivotal, Inc. ## ## Licensed under the MPL. See http://www.rabbitmq.com/ ## ## ########## Logs: sbin/../var/log/rabbitmq/rabbit@adeMacBook-Pro.log ###### ## sbin/../var/log/rabbitmq/rabbit@adeMacBook-Pro-sasl.log ########## Starting broker... completed with 0 plugins.
3.状态查询
adeMacBook-Pro:rabbitmq_server-3.5.3 apple$ sbin/rabbitmqctl status Status of node ‘rabbit@adeMacBook-Pro‘ ... [{pid,79602}, {running_applications,[{rabbit,"RabbitMQ","3.5.3"}, {mnesia,"MNESIA CXC 138 12","4.12.5"}, {os_mon,"CPO CXC 138 46","2.3.1"}, {xmerl,"XML parser","1.3.7"}, {sasl,"SASL CXC 138 11","2.4.1"}, {stdlib,"ERTS CXC 138 10","2.4"}, {kernel,"ERTS CXC 138 10","3.2"}]}, {os,{unix,darwin}},
相关页面:
http://www.rabbitmq.com/install-standalone-mac.html
到官网下载java客户端:
并且将rabbitmq-client.jar加入到classpath下面:
Send:
package rabbitmq_demo; import java.util.concurrent.TimeoutException; import com.rabbitmq.client.Channel; import com.rabbitmq.client.Connection; import com.rabbitmq.client.ConnectionFactory; public class Send { //队列名称 private final static String QUEUE_NAME = "hello"; public static void main(String[] argv) throws java.io.IOException, TimeoutException { /** * 创建连接连接到MabbitMQ */ ConnectionFactory factory = new ConnectionFactory(); //设置MabbitMQ所在主机ip或者主机名 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 rabbitmq_demo; import java.util.concurrent.TimeoutException; import com.rabbitmq.client.Channel; import com.rabbitmq.client.Connection; import com.rabbitmq.client.ConnectionFactory; import com.rabbitmq.client.QueueingConsumer; public class Recv { //队列名称 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"); //创建队列消费者 QueueingConsumer consumer = new QueueingConsumer(channel); //指定消费队列 channel.basicConsume(QUEUE_NAME, true, consumer); while (true) { //nextDelivery是一个阻塞方法(内部实现其实是阻塞队列的take方法) QueueingConsumer.Delivery delivery = consumer.nextDelivery(); String message = new String(delivery.getBody()); System.out.println(" [x] Received ‘" + message + "‘"); } } }
运行效果:
github地址:https://github.com/super-d2/rabbitmq_demo
Linux下面使用方法:http://www.cnblogs.com/astroboyx/archive/2012/04/09/2739902.html
郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。