[erlang-questions] Erlang - supervised gen_server to drain off a queue

Daniel Luna daniel@REDACTED
Wed Jan 2 21:13:30 CET 2013


The task you are describing is very much a gen_server type of thing.

Just subscribe to the rabbit queue and you are good to go.

Copy/pasting from our code, we do something similar to the following

    #'basic.qos_ok'{} =
        amqp_channel:call(Channel, #'basic.qos'{prefetch_count = Prefetch}),
    #'basic.consume_ok'{consumer_tag = Tag} =
        amqp_channel:subscribe(Channel,
                               #'basic.consume'{queue = Q},
                               self()),
    receive
        #'basic.consume_ok'{consumer_tag = Tag} -> ok
    end,

When you want to unsubscribe you can either just kill the reader or
unsubscribe using the consumer_tag.

Every message can then be handled in handle_info receiving
{#'basic.deliver'{delivery_tag = Tag}, #amqp_msg{payload =
PayloadBin}}.

After you've done the logging you ack the message:
amqp_channel:cast(Channel, #'basic.ack'{delivery_tag = Tag}).

That should be enough to get you going.

/Daniel


On 29 December 2012 02:35, hyperboreean <hyperboreean@REDACTED> wrote:
> Hi guys,
>
> I'm just trying to bring Erlang into my company - it's a hard road, but
> we might eventually use it for at least some of our projects. I want to
> get started by changing a small piece of code that reads an AMQP queue
> and appends the contents of each message to a file. To be more specific,
> there are multiple producers for that queue (which produce error
> messages, if any) and there's this consumer which gets the error
> messages from the queue and updates a file with them. Now, this piece of
> software has always been problematic for us, for one reason or another -
> it's not reliable and when it fails it might bring the AMQP broker to
> its knees because of the number of messages that are not consumed.
>
> I'd like to change this code with some Erlang code - maybe with a
> supervision tree so that if/when the server fails, it gets automatically
> restarted. I was thinking of trying a gen_server, but then I figured out
> that there's no client of the server, the code just has to constantly
> pull data out of that queue.
>
> Any good hints on how to get started on this ? What's the OTP way of
> doing this kind of things?
>
> Thank you!
>
> _______________________________________________
> erlang-questions mailing list
> erlang-questions@REDACTED
> http://erlang.org/mailman/listinfo/erlang-questions



More information about the erlang-questions mailing list