rabbitmq应用总结

http://melin.iteye.com/blog/691265
推荐 http://hi.baidu.com/vegeta_ma/item/9929cbe23b20f0246cabb807

1,Connection
连接,与rabbitmq server建立的一个连接,由ConnectionFactory创建,虽然创建时指定了多个server address,但每个connection只与一个物理的server进行连接,此连接是基于Socket进行连接的,这个可以相似的理解为像一个DB Connection。
ConnectionParametersparams = new ConnectionParameters();
params.setUsername(userName);
params.setPassword(password);
params.setVirtualHost(virtualHost);
params.setRequestedHeartbeat(0);
ConnectionFactoryfactory = new ConnectionFactory(params);
Connection conn = factory.newConnection(hostName,
AMQP.PROTOCOL.PORT);
2,Channel
建立在connection基础上的一个通道,相对于connection来说,它是轻量级的。可以这样理解,它就像是hibernate里面的session一样,相对于DB Connection来说,session就是一个轻量级的东西。
Channel channel =conn.createChannel();
注: 尽量避免在多线程中使用一个channel,Channeljavadoc有如下说明:
While aChannel can be used by multiple threads, it's important to ensure
that onlyone thread executes a command at once. Concurrent execution of
commandswill likely cause an UnexpectedFrameError to be thrown.
另官方Java Client API Guide里面也同样提到
     Channel thread-safety
In general, Channel instances should not be used by more than one thread simultaneously: applicationcode should maintain a clear notion of thread ownership for Channel instances.If more than one thread needs to access a particular Channel instance, the application should enforcemutual exclusion itself, for example by synchronising on the Channel.
Symptoms of incorrect serialisation of Channel operations include, but are not limited to,de>IllegalStateExceptionde>s with the message "cannot execute more than one synchronous AMQP command at a time", and de>UnexpectedFrameErrorde>s.

3,Exchange,Queue,RoutingKey
Direct Exchange – 处理路由键。需要将一个队列绑定到交换机上,要求该消息与一个特定的路由键完全匹配。这是一个完整的匹配。如果一个队列绑定到该交换机上要求路由键 “dog”,则只有被标记为“dog”的消息才被转发,不会转发dog.puppy,也不会转发dog.guard,只会转发dog。
Channel channel = connection.createChannel();
channel.exchangeDeclare("exchangeName", "direct"); //direct fanout topic
channel.queueDeclare("queueName");
//channel.queueDeclare(queueName, true, false, false, null);
channel.queueBind("queueName", "exchangeName", "routingKey");

byte[] messageBodyBytes = "hello world".getBytes();
//需要绑定路由键
channel.basicPublish("exchangeName", "routingKey", MessageProperties.PERSISTENT_TEXT_PLAIN, messageBodyBytes);



Fanout Exchange – 不处理路由键。你只需要简单的将队列绑定到交换机上。一个发送到交换机的消息都会被转发到与该交换机绑定的所有队列上。很像子网广播,每台子网内的主机都获得了一份复制的消息。Fanout交换机转发消息是最快的。

Channel channel = connection.createChannel();
channel.exchangeDeclare("exchangeName", "fanout"); //direct fanout topic
channel.queueDeclare("queueName");
//channel.queueDeclare(queueName, true, false, false, null);
channel.queueBind("queueName", "exchangeName", "routingKey");

channel.queueDeclare("queueName1");
channel.queueBind("queueName1", "exchangeName", "routingKey1");

byte[] messageBodyBytes = "hello world".getBytes();
//路由键需要设置为空
channel.basicPublish("exchangeName", "", MessageProperties.PERSISTENT_TEXT_PLAIN, messageBodyBytes);



Topic Exchange – 将路由键和某模式进行匹配。此时队列需要绑定要一个模式上。符号“#”匹配一个或多个词,符号“*”匹配不多不少一个词。因此“audit.#”能够匹配到“audit.irs.corporate”,但是“audit.*” 只会匹配到“audit.irs”。我在RedHat的朋友做了一张不错的图,来表明topic交换机是如何工作的:

Channel channel = connection.createChannel();
channel.exchangeDeclare("exchangeName", "topic"); //direct fanout topic
channel.queueDeclare("queueName");
//channel.queueDeclare(queueName, true, false, false, null);
channel.queueBind("queueName", "exchangeName", "routingKey.*");

byte[] messageBodyBytes = "hello world".getBytes();
channel.basicPublish("exchangeName", "routingKey.one", MessageProperties.PERSISTENT_TEXT_PLAIN, messageBodyBytes);

你可能感兴趣的:(rabbitmq)