[erlang-questions] Message Loop in Gen_Server

JD Bothma <>
Wed Apr 17 21:13:29 CEST 2013


Cool.

Like a few replies have said now, handle_info in the gen_server callback
module is where you can handle those.

If all you need is to handle messages from rabbitmq, you could get away
with a plain process, perhaps being supervised.

In the long run it's worth the added benefits of proc_lib as Garret
suggested, which you get for free in gen_server, but gen_server adds the
call and cast options. If you don't need gen_server call and cast, it's
worth looking into "OTP-compliant special processes"
http://www.erlang.org/doc/design_principles/spec_proc.html - often you
don't need a gen_server, or forging a gen_server into what you want isn't
the cleanest solution.

The easiest way to get started with something good enough, though, is
gen_server :)


On 17 April 2013 19:57, Lee Sylvester <> wrote:

> It's checking for messages from RabbitMQ.
>
> Cheers,
> Lee
>
>
> On 17 Apr 2013, at 18:47, JD Bothma <> wrote:
>
> Can I ask what your never-exiting loop is doing?
>
> what is the purpose of the gen_server if the process will forever loop in
> loop and not deal with gen_server messages?
>
>
> On 17 April 2013 19:33, Lee Sylvester <> wrote:
>
>> Hey guys,
>>
>> So, I've hit a "best practice" conundrum in OTP; I have a server
>> utilising gen_server for a RabbitMQ consumer.  In the init of that
>> gen_server, I'm setting up a RabbitMQ connection, but I also need to start
>> a loop.  My guess was that I shouldn't call this before init exits, as I
>> was passing the Connection and Channel objects to state for handling
>> elsewhere.  If I handle the loop in init, surely it will never return?
>>
>> To simplify what I'm saying (as I'm confusing myself here), here's my
>> code:
>>
>> init([]) ->
>>     {ok, Connection} = amqp_connection:start(#amqp_params_network{
>> host="localhost" }),
>>     {ok, Channel} = amqp_connection:open_channel(Connection),
>>     amqp_channel:call(Channel, #'exchange.declare'{exchange =
>> <<"user_msgs">>,
>>                                                    type = <<"direct">>}),
>>     #'queue.declare_ok'{queue = Queue} =
>>         amqp_channel:call(Channel, #'queue.declare'{exclusive = true}),
>>     State = {Channel, Connection},
>>     amqp_channel:call(Channel, #'queue.bind'{exchange = <<"user_msgs">>,
>>                                               routing_key =
>> term_to_binary(node(self())),
>>                                               queue = Queue}),
>>     amqp_channel:subscribe(Channel, #'basic.consume'{queue = Queue,
>>         no_ack = true}, self()),
>>     receive
>>         #'basic.consume_ok'{} -> ok
>>     end,
>>     loop(Channel),
>>     {ok, State}.
>>
>> Now, if I don't put the loop in my init, then how can I be sure that the
>> loop is called every time the gen_server restarts?  Can someone please
>> suggest the "right" way to call the loop in my gen_server?
>>
>> Thanks loads,
>> Lee
>> _______________________________________________
>> erlang-questions mailing list
>> 
>> http://erlang.org/mailman/listinfo/erlang-questions
>>
>
>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://erlang.org/pipermail/erlang-questions/attachments/20130417/3e248fd0/attachment.html>


More information about the erlang-questions mailing list