[rabbitmq-discuss] Erlang Consumer Example

Anton Prokofiev a.d.prokofiev at gmail.com
Mon Jul 30 22:12:01 BST 2012


Hello, Matthias!



On 07/30/2012 11:52 AM, Matthias Radestock wrote:
> Anton,
>
> On 29/07/12 21:59, anton prokofiev wrote:
>> On Sunday, 29 July 2012 21:21:06 UTC+2, anton prokofiev wrote:
>>     I tried to create  a consumer in Erlang for RabbitMQ and get into
>>     big troubles.
>>     Looks like examples that are available in RabbitMQ website did 
>> not work.
>
> *Which* example didn't work?
It seams to me that  this one did not work:

Sub = #'basic.consume'{queue = Q},
       #'basic.consume_ok'{consumer_tag = Tag} = amqp_channel:call(Channel, Sub) %% the caller is the subscriber


At least I do not managed to force it to work.
In another one,

Sub = #'basic.consume'{queue = Q},
       #'basic.consume_ok'{consumer_tag = Tag} = amqp_channel:subscribe(Channel, Sub, Consumer)
     

It was not clear to me what should be used as a consumer, as final 
example contains only case when we use *#'basic.get'* command.

If I understood it correctly, I should pass a PID of the consumer 
process, when I  open channel:
amqp_connection:open_channel(Connection,none,*{amqp_direct_consumer,[Pid]}*), 


Unfortunately I can not found this in the documentation, or may be it 
was not clearly enough pointed out. :(

I should say that I  have not too much experience with erlang, and just 
started to work with rabbitMQ.
Another problem is that RabbitMQ broker I work with is out of my control 
and documentation that I have really misleading.
As a result I have to use try-and-error approach to develop my application.

>> Looks like I have found a correct solution:
>>
>> 1. I have implemented simple gen_server, that just print all messages to
>> the stdout.
>>
>> 2. And  to like this:
>>    {ok,Connection} = amqp_connection:start(ConnectionParams), % open
>> connection with predefined params.
>>    {ok,Pid} =  test_srv:start_link(),    % Start our own consumer.
>> {ok,Channel}  =
>> amqp_connection:open_channel(Connection,none,*{amqp_direct_consumer,[Pid]}*), 
>>
>
> The above creates a channel with a default consumer. More typically 
> you'd associate consumers with particular subscriptions, as shown in 
> http://www.rabbitmq.com/erlang-client-user-guide.html#subscribing.
>
>> Would it be better to create my own implementation of consume behavior,
>> or handle them thought the message_info as it is now?
>> Any recommendations?
>
> Implementing your own instance of the amqp_gen_consumer behaviour is 
> probably the more "correct" approach, but it's a bit more work and if 
> the behaviour of the amqp_direct_consumer fits in well with the rest 
> of your system design then just use that.
>
> Regards,
>
> Matthias.

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.rabbitmq.com/pipermail/rabbitmq-discuss/attachments/20120730/6b58d7d5/attachment.htm>


More information about the rabbitmq-discuss mailing list