补充:做下面的例子前,需要先安装 rabbitmq-server-3.3.0.exe 服务器。资源我已经上传
安装以后启动服务器就可以了
下面文章出自:http://blog.csdn.net/mydreamongo/article/details/8272025
请保护正版:
abbitmq是一个在amqp基础上完整的,可服用的企业消息系统。他遵循mozilla public license 开源协议。
关于amqp可参考
rabbitmq是一个消费的代理;通过生产者客户端生产一个信息,转送给消费者客户端;在这个传输过程中,根据你的需要可以经过路由、缓冲、持久化来得到这个消息。
先通过一个例子开始:通过rabbitmq输出"hello world!"

其中p代表生产者、c表示消费者、中间红色部分代表消息队列
生产者客户端的发送消息程序如下:
java代码
package com.abin.test;
import java.io.ioexception;
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[] args) throws ioexception {
connectionfactory factory = new connectionfactory();
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();
}
}
运行结果如下:
[x] sent ‘hello world!‘
消费者客户端接收消息程序如下:
import com.rabbitmq.client.queueingconsumer;
public class reqv {
public static void main(string[] argv) throws exception {
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(" [x] received ‘" + message + "‘");
}
运行程序得到的结果如下:
[*] waiting for messages. to exit press ctrl+c
[x] received ‘hello world!‘