1.基本安装
分为 server + client
server的安装:
1.添加 deb http://www.rabbitmq.com/debian/ testing main 到 /etc/apt/sources.list
2.apt-get update.
3.sudo apt-get install rabbitmq-server
这个步骤会自动启动 rabbitmq-server 服务。
常用命令:
rabbitmqctl -h
rabbitmqctl status
rabbitmqctl stop
rabbitmqctl start_app
客户端安装:
maven:
<dependency>
<groupId>com.rabbitmq</groupId>
<artifactId>amqp-client</artifactId>
<version>2.8.4</version>
</dependency>
或是下载链接:
wget http://www.rabbitmq.com/releases/rabbitmq-java-client/v2.8.4/rabbitmq-java-client-bin-2.8.4.tar.gz
客户端编码---发送者:
package com.jieting.mq.rabbit.send; import java.io.IOException; import com.rabbitmq.client.Channel; import com.rabbitmq.client.Connection; import com.rabbitmq.client.ConnectionFactory; public class MessageSend { private static final String QUENE_NAME = "hello"; public static void main(String[] args) throws IOException { ConnectionFactory connectionFactory = new ConnectionFactory(); connectionFactory.setHost("localhost"); Connection newConnection = connectionFactory.newConnection(); Channel createChannel = newConnection.createChannel(); createChannel.queueDeclare(QUENE_NAME, true, false, false, null); String message = "hello rabbitmq world!"; createChannel.basicPublish("", QUENE_NAME, null, message.getBytes()); System.out.println(" [x] Sent '" + message + "'"); createChannel.close(); newConnection.close(); } }
消费者代码:
package com.jieting.mq.rabbit.receive; import java.io.IOException; import com.rabbitmq.client.Channel; import com.rabbitmq.client.Connection; import com.rabbitmq.client.ConnectionFactory; import com.rabbitmq.client.ConsumerCancelledException; import com.rabbitmq.client.QueueingConsumer; import com.rabbitmq.client.ShutdownSignalException; public class MessageReceive { private static final String QUENE_NAME = "hello"; public static void main(String[] args) throws IOException, ShutdownSignalException, ConsumerCancelledException, InterruptedException { ConnectionFactory connectionFactory = new ConnectionFactory(); Connection newConnection = connectionFactory.newConnection(); Channel createChannel = newConnection.createChannel(); createChannel.queueDeclare(QUENE_NAME, true, false, false, null); System.out.println(" [*] Waiting for messages. To exit press CTRL+C"); QueueingConsumer queueingConsumer = new QueueingConsumer(createChannel); createChannel.basicConsume(QUENE_NAME, true, queueingConsumer); while (true) { QueueingConsumer.Delivery delivery = queueingConsumer.nextDelivery(); String message = new String(delivery.getBody()); System.out.println(" [x] Received '" + message + "'"); } } }
以上资料都可从 一下地址找到:
http://www.rabbitmq.com/java-client.html
http://www.rabbitmq.com/getstarted.html
已有 0 人发表留言,猛击->>这里<<-参与讨论
ITeye推荐
- —软件人才免语言低担保 赴美带薪读研!—