Erlang code for first tutorial

This commit is contained in:
Marek Majkowski 2010-12-08 17:49:47 +00:00
parent 1d82921b17
commit b7d04ac023
3 changed files with 57 additions and 0 deletions

8
erlang/README.md Normal file
View File

@ -0,0 +1,8 @@
# Erlang code for RabbitMQ tutorials #
wget http://www.rabbitmq.com/releases/plugins/v2.2.0/rabbit_common-2.2.0.ez
unzip rabbit_common-2.2.0.ez
wget http://www.rabbitmq.com/releases/plugins/v2.2.0/amqp_client-2.2.0.ez
unzip amqp_client-2.2.0.ez

28
erlang/receive.erl Normal file
View File

@ -0,0 +1,28 @@
#!/usr/bin/env escript
%%! -pz ./amqp_client ./rabbit_common ./amqp_client/ebin ./rabbit_common/ebin
-include_lib("amqp_client/include/amqp_client.hrl").
main(_) ->
{ok, Connection} = amqp_connection:start(network,
#amqp_params{host = "localhost"}),
{ok, Channel} = amqp_connection:open_channel(Connection),
amqp_channel:call(Channel, #'queue.declare'{queue = <<"hello">>}),
io:format(" [*] Waiting for messages. To exit press CTRL+C\n"),
amqp_channel:subscribe(Channel, #'basic.consume'{queue = <<"hello">>,
no_ack = true}, self()),
receive
#'basic.consume_ok'{} -> ok
end,
loop(Channel).
loop(Channel) ->
receive
{#'basic.deliver'{}, #amqp_msg{payload = Body}} ->
io:format(" [x] Received ~p\n", [Body]),
loop(Channel)
end.

21
erlang/send.erl Normal file
View File

@ -0,0 +1,21 @@
#!/usr/bin/env escript
%%! -pz ./amqp_client ./rabbit_common ./amqp_client/ebin ./rabbit_common/ebin
-include_lib("amqp_client/include/amqp_client.hrl").
main(_) ->
{ok, Connection} = amqp_connection:start(network,
#amqp_params{host = "localhost"}),
{ok, Channel} = amqp_connection:open_channel(Connection),
amqp_channel:call(Channel, #'queue.declare'{queue = <<"hello">>}),
amqp_channel:cast(Channel,
#'basic.publish'{
exchange = <<"">>,
routing_key = <<"hello">>},
#amqp_msg{payload = <<"Hello World!">>}),
io:format(" [x] Sent 'Hello World!'\n"),
ok = amqp_channel:close(Channel),
ok = amqp_connection:close(Connection),
ok.