Skip to content

Commit cb0f9a5

Browse files
committed
Merge remote-tracking branch 'remotes/janimo/erlang'
2 parents 05a8c3d + ba47e9a commit cb0f9a5

File tree

5 files changed

+138
-0
lines changed

5 files changed

+138
-0
lines changed

erlang/README.md

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,3 +42,13 @@ You need Erlang Client binaries:
4242

4343
./receive_logs.erl
4444
./emit_log.erl "info: This is the log message"
45+
46+
[Tutorial four: Routing](http://www.rabbitmq.com/tutorial-four-python.html):
47+
48+
./receive_logs_direct.erl info
49+
./emit_log_direct.py info Hello
50+
51+
[Tutorial five: Topics](http://www.rabbitmq.com/tutorial-five-python.html):
52+
53+
./receive_logs_topic.erl *.rabbit
54+
./emit_log_topic.erl red.rabbit Hello

erlang/emit_log_direct.erl

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
#!/usr/bin/env escript
2+
%%! -pz ./amqp_client ./rabbit_common ./amqp_client/ebin ./rabbit_common/ebin
3+
4+
-include_lib("amqp_client/include/amqp_client.hrl").
5+
6+
main(Argv) ->
7+
{ok, Connection} =
8+
amqp_connection:start(#amqp_params_network{host = "localhost"}),
9+
{ok, Channel} = amqp_connection:open_channel(Connection),
10+
11+
amqp_channel:call(Channel, #'exchange.declare'{exchange = <<"direct_logs">>,
12+
type = <<"direct">>}),
13+
14+
{Severity, Message} = case Argv of
15+
[] -> {<<"info">>, <<"Hello World!">>};
16+
[S] -> {list_to_binary(S), <<"Hello World!">>};
17+
[S | Msg] -> {list_to_binary(S), list_to_binary(string:join(Msg, " "))}
18+
end,
19+
amqp_channel:cast(Channel,
20+
#'basic.publish'{
21+
exchange = <<"direct_logs">>,
22+
routing_key = Severity},
23+
#amqp_msg{payload = Message}),
24+
io:format(" [x] Sent ~p:~p~n", [Severity, Message]),
25+
ok = amqp_channel:close(Channel),
26+
ok = amqp_connection:close(Connection),
27+
ok.

erlang/emit_log_topic.erl

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
#!/usr/bin/env escript
2+
%%! -pz ./amqp_client ./rabbit_common ./amqp_client/ebin ./rabbit_common/ebin
3+
4+
-include_lib("amqp_client/include/amqp_client.hrl").
5+
6+
main(Argv) ->
7+
{ok, Connection} =
8+
amqp_connection:start(#amqp_params_network{host = "localhost"}),
9+
{ok, Channel} = amqp_connection:open_channel(Connection),
10+
11+
amqp_channel:call(Channel, #'exchange.declare'{exchange = <<"topic_logs">>,
12+
type = <<"topic">>}),
13+
14+
{RoutingKey, Message} = case Argv of
15+
[] -> {<<"anonymous.info">>, <<"Hello World!">>};
16+
[R] -> {list_to_binary(R), <<"Hello World!">>};
17+
[R | Msg] -> {list_to_binary(R), list_to_binary(string:join(Msg, " "))}
18+
end,
19+
amqp_channel:cast(Channel,
20+
#'basic.publish'{
21+
exchange = <<"topic_logs">>,
22+
routing_key = RoutingKey},
23+
#amqp_msg{payload = Message}),
24+
io:format(" [x] Sent ~p:~p~n", [RoutingKey, Message]),
25+
ok = amqp_channel:close(Channel),
26+
ok = amqp_connection:close(Connection),
27+
ok.

erlang/receive_logs_direct.erl

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
#!/usr/bin/env escript
2+
%%! -pz ./amqp_client ./rabbit_common ./amqp_client/ebin ./rabbit_common/ebin
3+
4+
-include_lib("amqp_client/include/amqp_client.hrl").
5+
6+
main(Argv) ->
7+
{ok, Connection} =
8+
amqp_connection:start(#amqp_params_network{host = "localhost"}),
9+
{ok, Channel} = amqp_connection:open_channel(Connection),
10+
11+
amqp_channel:call(Channel, #'exchange.declare'{exchange = <<"direct_logs">>,
12+
type = <<"direct">>}),
13+
14+
#'queue.declare_ok'{queue = Queue} =
15+
amqp_channel:call(Channel, #'queue.declare'{exclusive = true}),
16+
17+
lists:foreach(fun(S) ->
18+
amqp_channel:call(Channel, #'queue.bind'{exchange = <<"direct_logs">>,
19+
routing_key = list_to_binary(S),
20+
queue = Queue})
21+
end, Argv),
22+
23+
io:format(" [*] Waiting for logs. To exit press CTRL+C~n"),
24+
25+
amqp_channel:subscribe(Channel, #'basic.consume'{queue = Queue,
26+
no_ack = true}, self()),
27+
receive
28+
#'basic.consume_ok'{} -> ok
29+
end,
30+
loop(Channel).
31+
32+
loop(Channel) ->
33+
receive
34+
{#'basic.deliver'{routing_key=RoutingKey}, #amqp_msg{payload = Body}} ->
35+
io:format(" [x] ~p:~p~n", [RoutingKey, Body]),
36+
loop(Channel)
37+
end.

erlang/receive_logs_topic.erl

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
#!/usr/bin/env escript
2+
%%! -pz ./amqp_client ./rabbit_common ./amqp_client/ebin ./rabbit_common/ebin
3+
4+
-include_lib("amqp_client/include/amqp_client.hrl").
5+
6+
main(Argv) ->
7+
{ok, Connection} =
8+
amqp_connection:start(#amqp_params_network{host = "localhost"}),
9+
{ok, Channel} = amqp_connection:open_channel(Connection),
10+
11+
amqp_channel:call(Channel, #'exchange.declare'{exchange = <<"topic_logs">>,
12+
type = <<"topic">>}),
13+
14+
#'queue.declare_ok'{queue = Queue} =
15+
amqp_channel:call(Channel, #'queue.declare'{exclusive = true}),
16+
17+
lists:foreach(fun(R) ->
18+
amqp_channel:call(Channel, #'queue.bind'{exchange = <<"topic_logs">>,
19+
routing_key = list_to_binary(R),
20+
queue = Queue})
21+
end, Argv),
22+
23+
io:format(" [*] Waiting for logs. To exit press CTRL+C~n"),
24+
25+
amqp_channel:subscribe(Channel, #'basic.consume'{queue = Queue,
26+
no_ack = true}, self()),
27+
receive
28+
#'basic.consume_ok'{} -> ok
29+
end,
30+
loop(Channel).
31+
32+
loop(Channel) ->
33+
receive
34+
{#'basic.deliver'{routing_key=RoutingKey}, #amqp_msg{payload = Body}} ->
35+
io:format(" [x] ~p:~p~n", [RoutingKey, Body]),
36+
loop(Channel)
37+
end.

0 commit comments

Comments
 (0)