[rabbitmq-discuss] Message receiving

Matthew Sackman matthew at rabbitmq.com
Thu Jun 30 13:05:53 BST 2011


Hi,

On Wed, Jun 29, 2011 at 05:05:12PM +0000, Alexander Kuleshov wrote:
> I try to receive message from rabbitmq server. At the start i connect to server:
[snip]
> I send message in {ok, Message} tuple from another code:
> 
> Chanel ! {ok, Message}.
> 
> Chanel i get from (  ok = gen_fsm:sync_send_all_state_event({global,
> "game1"}, {set_channel, self()}), ) in main function. Then
> amqp_channel:cast send message Message, but the answer does not come.
> First lopp/1 clause not execute.
> 
> How can i send message from another module and receive answer in
> loop/1 correctly?

I assume you're doing an io:format or some dbg:trace to determine that
you never hit the first branch of loop/1? It certainly should work, and
indeed does work for me.

I've removed the gen_fsm stuff, and just spawn it from the shell:

main() ->
    {ok, Connection} = amqp_connection:start(#amqp_params_network{}),
    {ok, Channel} = amqp_connection:open_channel(Connection),
    amqp_channel:call(Channel, #'queue.declare'{queue = <<"test">>}),
    amqp_channel:subscribe(Channel, #'basic.consume'{queue = <<"test">>}, self()),

    receive
        #'basic.consume_ok'{} ->
            ok
    end,
    loop(Channel).

loop(Channel) ->
    receive
        {#'basic.deliver'{delivery_tag = Tag}, #amqp_msg{payload = Body}} ->
            amqp_channel:cast(Channel, #'basic.ack'{delivery_tag = Tag}),
            loop(Channel);
        {ok, Message} ->
            amqp_channel:cast(Channel,
                              #'basic.publish'{routing_key = <<"test">>},
                              #amqp_msg{payload = Message}),
            loop(Channel)
    end.

(amqp_client at hazel)1> Pid = spawn(test, www_main, []).
<0.40.0>

  Then turn on tracing to amqp_channel:cast:

(amqp_client at hazel)2> dbg:tracer(), dbg:p(all,c), dbg:tp(amqp_channel, cast, []).
{ok,[{matched,amqp_client at hazel,2}]}

  Now send a message to Pid:

(amqp_client at hazel)7> Pid ! {ok, <<"blah2">>}.                                   
{ok,<<"blah2">>}

(<0.40.0>) call amqp_channel:cast(<0.56.0>,{'basic.publish',0,<<>>,<<"test">>,false,false},{amqp_msg,{'P_basic',undefined,undefined,undefined,undefined,undefined,
                     undefined,undefined,undefined,undefined,undefined,
                     undefined,undefined,undefined,undefined},
          <<"blah2">>})
(<0.40.0>) call amqp_channel:cast(<0.56.0>,{'basic.ack',3,false})

So we can see that the publish was indeed sent, and then it was received
and an ack was sent too. Sure enough, rabbitmqctl list_queues shows the
test queue is empty.

In your case, is the test queue not empty?

Matthew


More information about the rabbitmq-discuss mailing list