[rabbitmq-discuss] Erlang Consumer Example

Francesco Mazzoli francesco at rabbitmq.com
Tue Jul 31 18:24:16 BST 2012


At Mon, 30 Jul 2012 23:12:01 +0200,
Anton Prokofiev wrote:
> 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.

I just tried out a simple consumer, copying and pasting the example:

   -include("amqp_client.hrl").

   loop(Channel) ->
       receive
           %% This is the first message received
           #'basic.consume_ok'{} ->
               loop(Channel);
   
           %% This is received when the subscription is cancelled
           #'basic.cancel_ok'{} ->
               ok;
   
           %% A delivery
           {#'basic.deliver'{delivery_tag = Tag}, Content} ->
               %% Do something with the message payload
               %% (some work here)
               io:format("~p~n", [Content]),
   
               %% Ack the message
               amqp_channel:cast(Channel, #'basic.ack'{delivery_tag = Tag}),
   
               %% Loop
               loop(Channel)
       end.
   
   test() ->
       {ok, Connection} = amqp_connection:start(#amqp_params_network{}),
       {ok, Channel} = amqp_connection:open_channel(Connection),
       Q = <<"q">>,
       %% declare queue
       #'queue.declare_ok'{} = amqp_channel:call(Channel,
                                                 #'queue.declare'{queue = Q})
       %% consume
       Sub = #'basic.consume'{queue = Q},
       #'basic.consume_ok'{consumer_tag = Tag} = amqp_channel:call(Channel, Sub),
       loop(Channel).

After running `test()', I published some messages to the "(AMQP default)"
exchange with the appropriate routing key ("q") through the management
interface, and the consumer correctly received.

> 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]}*),

As explained by Matthias, this will create a channel with a default consumer,
which is probably not what you need - you most likely want the selective
consumer, which is default (in `open_channel/{1,2}').

The edoc is quite unclear on what the default consumer is (in fact, I don't
think there is a way to know that from the edoc, you have to look at the
source).  It also does not include details about the callback functions required
for the `amqp_gen_consumer' behaviour.  I'll file a bug.

> 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.

I can imagine that to be frustrating.  Try to convince the sysadmins to the last
version :).  Also, you should find the appropriate docs locally - I don't know
how you installed rabbit but they are usually packaged with the broker.  But the
trial and error approach is tedious and dangerous.

--
Francesco * Often in error, never in doubt


More information about the rabbitmq-discuss mailing list