rabbitmq应用总结

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

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();
注:[b]尽量避免在多线程中使用一个channel[/b],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
[b]Direct Exchange[/b] – 处理路由键。需要将一个队列绑定到交换机上,要求该消息与一个特定的路由键完全匹配。这是一个完整的匹配。如果一个队列绑定到该交换机上要求路由键 “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);



[b]Fanout Exchange[/b] – 不处理路由键。你只需要简单的将队列绑定到交换机上。一个发送到交换机的消息都会被转发到与该交换机绑定的所有队列上。很像子网广播,每台子网内的主机都获得了一份复制的消息。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);



[b]Topic Exchange[/b] – 将路由键和某模式进行匹配。此时队列需要绑定要一个模式上。符号“#”匹配一个或多个词,符号“*”匹配不多不少一个词。因此“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);
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值