rabbitmq应用总结
http://melin.iteye.com/blog/691265
推荐http://hi.baidu.com/vegeta_ma/item/9929cbe23b20f0246cabb807
1,Connection
连接,与rabbitmqserver建立的一个连接,由ConnectionFactory创建,虽然创建时指定了多个serveraddress,但每个connection只与一个物理的server进行连接,此连接是基于Socket进行连接的,这个可以相似的理解为像一个DBConnection。
ConnectionParametersparams=newConnectionParameters();
params.setUsername(userName);
params.setPassword(password);
params.setVirtualHost(virtualHost);
params.setRequestedHeartbeat(0);
ConnectionFactoryfactory=newConnectionFactory(params);
Connectionconn=factory.newConnection(hostName,
AMQP.PROTOCOL.PORT);
2,Channel
建立在connection基础上的一个通道,相对于connection来说,它是轻量级的。可以这样理解,它就像是hibernate里面的session一样,相对于DBConnection来说,session就是一个轻量级的东西。
Channelchannel=conn.createChannel();
注:尽量避免在多线程中使用一个channel,Channeljavadoc有如下说明:
WhileaChannelcanbeusedbymultiplethreads,it'simportanttoensure
thatonlyonethreadexecutesacommandatonce.Concurrentexecutionof
commandswilllikelycauseanUnexpectedFrameErrortobethrown.
另官方JavaClientAPIGuide里面也同样提到
Channelthread-safety
Ingeneral,Channelinstancesshouldnotbeusedbymorethanonethreadsimultaneously:applicationcodeshouldmaintainaclearnotionofthreadownershipforChannelinstances.IfmorethanonethreadneedstoaccessaparticularChannelinstance,theapplicationshouldenforcemutualexclusionitself,forexamplebysynchronisingontheChannel.
SymptomsofincorrectserialisationofChanneloperationsinclude,butarenotlimitedto,de>IllegalStateExceptionde>swiththemessage"cannotexecutemorethanonesynchronousAMQPcommandatatime",andde>UnexpectedFrameErrorde>s.
3,Exchange,Queue,RoutingKey
DirectExchange–处理路由键。需要将一个队列绑定到交换机上,要求该消息与一个特定的路由键完全匹配。这是一个完整的匹配。如果一个队列绑定到该交换机上要求路由键“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);
FanoutExchange–不处理路由键。你只需要简单的将队列绑定到交换机上。一个发送到交换机的消息都会被转发到与该交换机绑定的所有队列上。很像子网广播,每台子网内的主机都获得了一份复制的消息。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);
TopicExchange–将路由键和某模式进行匹配。此时队列需要绑定要一个模式上。符号“#”匹配一个或多个词,符号“*”匹配不多不少一个词。因此“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);