rabbitmq.Our Extensions.Consumer Cancel: Consumer Cancel Notification/“取消消费”通知

取消机制,有两个方面:
一个是作为消费者,主动脱离不再接收消息
二是,服务节点不提供消息供消费,消费需要被通知进而取消必要等待

主动脱离不再接收消息:
需要主动向服务node发出basic.cancel, 服务器回复basic.cancel.ok,则完成
服务节点不提供消息供消费:
一是,由于默认并不支持这点,客户端需要主动向服务节点声明这个能力。
二是,实现默认的回调函数,这会在

Note:
当消费者已经从服务器获得取消消费等待的消息时,其还可以执行主动发出basic.cancel,服务节点也会正常回复
basic.cancel.ok。反之,服务器已收到客户端的basic.cancel,则不会再触发回调客户端的取消通知

>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>

原文:

When a channel is consuming from a queue, there are variousreasons which could cause the consumption to stop. One ofthese is obviously if the client issues abasic.cancel on the same channel, which willcause the consumer to be cancelled and the server replieswith a basic.cancel-ok. Other events, such asthe queue being deleted, or in a clustered scenario, thenode on which the queue is located failing, will cause theconsumption to be cancelled, but the client channel will notbe informed, which is frequently unhelpful.

To solve this, we have introduced an extension in which thebroker will send to the client a basic.cancelin the case of such unexpected consumer cancellations. Thisis not sent in the case of the broker receiving abasic.cancel from the client. AMQP 0-9-1clients don't by default expect to receivebasic.cancel methods from the brokerasynchronously, and so in order to enable this behaviour,the client must present a capabilities table inits client-properties in which there is a keyconsumer_cancel_notify and a boolean valuetrue. See thecapabilities section for furtherdetails on this.

Our supported clients present this capability by default tothe broker and thus will be sent the asynchronousbasic.cancel method by the broker, which theypresent to the consumer callback. For example, in our Javaclient, the Consumer interface has ahandleCancel callback, which can be overriddenby sub-classing the DefaultConsumer class:

channel.queueDeclare(queue, false, true, false, null);
Consumer consumer = new DefaultConsumer(channel) {
    @Override
    public void handleCancel(String consumerTag) throws IOException {
        // consumer has been cancelled unexpectedly
    }
};
channel.basicConsume(queue, consumer);
It is not an error for the client to issue a basic.cancel for a consumer which has beenunexpectedly cancelled (e.g. due to queue deletion). Bydefinition, there is a race possible between a clientissuing a basic.cancel, and the broker sendingout the asynchronous notification. In such cases, the brokerdoes not error when it receives the basic.cancel and replies with a basic.cancel-ok as normal.

Consumer Cancellation and HA failover

Clients supporting consumer cancel notification will always beinformed when a queue is deleted or becomesunavailable. Consumers may request that they should be cancelledwhen a mirrored queue fails over (see the page on mirrored queues for why and how this can bedone).

Client and Server Capabilities

The AMQP 0-8 specification defined acapabilities field as part of theconnection.open method. This field wasdeprecated in the AMQP 0-9-1 specification and is notinspected by the RabbitMQ broker. As specified in AMQP 0-8,it also suffered from being a shortstr: astring of no more than 256 characters.

There is good reason for both the client and the serverbeing able to present extensions and capabilities that theysupport, thus we have introduced an alternative form ofcapabilities. In the server-properties field ofconnection.start, and in theclient-properties field ofconnection.start-ok, the field value (apeer-properties table) can optionally contain akey named capabilities for which the value isanother table, in which the keys name the capabilitiessupported. The values for these capability keys aretypically booleans, indicating whether or not the capabilityis supported, but may vary based on the nature of thecapability.

For example, the server-properties presented bythe RabbitMQ broker to a client may look like:

{ "product"      = (longstr) "RabbitMQ",
  "platform"     = (longstr) "Erlang/OTP",
  "information"  = (longstr) "Licensed under the MPL.  See http://www.rabbitmq.com/",
  "copyright"    = (longstr) "Copyright (C) 2007-2016 Pivotal Software, Inc.",
  "capabilities" = (table)   { "exchange_exchange_bindings" = (bool) true,
                               "consumer_cancel_notify"     = (bool) true,
                               "basic.nack"                 = (bool) true,
                               "publisher_confirms"         = (bool) true },
  "version"      = (longstr) "2.4.0" }

Note that it is optional for clients to present thiscapabilities table as part of theclient-properties table: failure to presentsuch a table does not preclude the client from being able touse extensions such as exchange toexchange bindings. However, in some cases such asconsumer cancellation notification, the client must presentthe associated capability otherwise the broker will have noway of knowing that the client is capable of receiving theadditional notifications.


原文链接:www.rabbitmq.com/consumer-cancel.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值