[rabbitmq-discuss] amqp_connection:open_channel link channel
Tim Watson
tim at rabbitmq.com
Mon Feb 17 13:02:17 GMT 2014
Hi,
On 16 Feb 2014, at 18:08, Matwey V. Kornilov wrote:
> Is it a good practice to link the channel to the process going to consume the messages? I do want to close the channel when my consumer crashed, and let the broker to redeliver unacked message.
>
> {ok, Channel} = amqp_connection:open_channel(Connection),
> link(Channel),
>
> Why there is no open_channel_link counterpart, as start and start_link?
>
The reason is that there is a level of indirection between the channel process(es) and the process that calls open channel, i.e., the channel process is linked to an internal supervisor process, not to the process that calls open_channel. Probably the easiest way to achieve what you want is to open the channel in your process' initialisation phase and close it during the shutdown phase. If your consumer is built on one of the OTP behaviours (such as gen_server) then you can open and close the channel in the respective init/1 and terminate/2 callbacks. HTH!
> This is for case when channel goes down between open_channel and link.
Indeed, that's a different problem to closing the channel if/when your consumer crashes. The easiest way might seem at first, to use a monitor instead of linking, guaranteeing a 'DOWN' message even if the monitor-ee was dead when the monitor was established. Personally however, I'd suggest looking at the amqp_gen_consumer behaviour instead, since that defines a terminate/2 callback which behaves as follows (from the edocs):
%% This callback is invoked by the channel after it has shut down and
%% just before its process exits.
Cheers,
Tim
More information about the rabbitmq-discuss
mailing list