<html><head><meta http-equiv="Content-Type" content="text/html charset=us-ascii"><meta http-equiv="Content-Type" content="text/html charset=iso-8859-1"></head><body style="word-wrap: break-word; -webkit-nbsp-mode: space; -webkit-line-break: after-white-space; ">It's checking for messages from RabbitMQ.<div><br></div><div>Cheers,</div><div>Lee</div><div><br></div><div><br></div><div><div><div>On 17 Apr 2013, at 18:47, JD Bothma <<a href="mailto:jbothma@gmail.com">jbothma@gmail.com</a>> wrote:</div><br class="Apple-interchange-newline"><blockquote type="cite"><div dir="ltr">Can I ask what your never-exiting loop is doing?<div><br></div><div style="">what is the purpose of the gen_server if the process will forever loop in loop and not deal with gen_server messages?</div></div><div class="gmail_extra">
<br><br><div class="gmail_quote">On 17 April 2013 19:33, Lee Sylvester <span dir="ltr"><<a href="mailto:lee.sylvester@gmail.com" target="_blank">lee.sylvester@gmail.com</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">
Hey guys,<br>
<br>
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?<br>
<br>
To simplify what I'm saying (as I'm confusing myself here), here's my code:<br>
<br>
init([]) -><br>
{ok, Connection} = amqp_connection:start(#amqp_params_network{ host="localhost" }),<br>
{ok, Channel} = amqp_connection:open_channel(Connection),<br>
amqp_channel:call(Channel, #'exchange.declare'{exchange = <<"user_msgs">>,<br>
type = <<"direct">>}),<br>
#'queue.declare_ok'{queue = Queue} =<br>
amqp_channel:call(Channel, #'queue.declare'{exclusive = true}),<br>
State = {Channel, Connection},<br>
amqp_channel:call(Channel, #'queue.bind'{exchange = <<"user_msgs">>,<br>
routing_key = term_to_binary(node(self())),<br>
queue = Queue}),<br>
amqp_channel:subscribe(Channel, #'basic.consume'{queue = Queue,<br>
no_ack = true}, self()),<br>
receive<br>
#'basic.consume_ok'{} -> ok<br>
end,<br>
loop(Channel),<br>
{ok, State}.<br>
<br>
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?<br>
<br>
Thanks loads,<br>
Lee<br>
_______________________________________________<br>
erlang-questions mailing list<br>
<a href="mailto:erlang-questions@erlang.org">erlang-questions@erlang.org</a><br>
<a href="http://erlang.org/mailman/listinfo/erlang-questions" target="_blank">http://erlang.org/mailman/listinfo/erlang-questions</a><br>
</blockquote></div><br></div>
</blockquote></div><br></div></body></html>