Last active
August 29, 2015 14:07
-
-
Save habibutsu/18fc2138e22f2df7de0f to your computer and use it in GitHub Desktop.
Examples for https://github.com/jbrisbin/amqp_client
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
% Compile: | |
% erlc +debug_info amqp_consumer.erl; | |
% Run: | |
% erl -noshell -s amqp_consumer consume -s init stop | |
-module(amqp_consumer). | |
-export([consume/0]). | |
-include_lib("amqp_client/include/amqp_client.hrl"). | |
consume_loop(Channel) -> | |
receive {#'basic.deliver'{delivery_tag = DeliveryTag}, #amqp_msg{payload = Payload}} -> | |
io:format("[~p] Payload: ~p", [self(), Payload]), | |
amqp_channel:call( | |
Channel, #'basic.ack'{delivery_tag = DeliveryTag}), | |
io:format(" ack~n"), | |
consume_loop(Channel) | |
end. | |
consume() -> | |
io:format("Connect ... "), | |
{ok, Connection} = amqp_connection:start( | |
#amqp_params_network{ | |
host="127.0.0.1", | |
port=5672, | |
username = <<"username">>, | |
password = <<"password">>, | |
virtual_host = <<"/vhost">>}), | |
io:format("Ok~n"), | |
{ok, Channel} = amqp_connection:open_channel(Connection), | |
Exchange = <<"test_exchange">>, | |
Queue = <<"test_queue">>, | |
QueueArgs = [{<<"x-message-ttl">>, signedint, 300000}], | |
RoutingKey = <<"test_route">>, | |
io:format("Declare queue ..."), | |
#'queue.declare_ok'{} = amqp_channel:call( | |
Channel, #'queue.declare'{queue=Queue, durable=true, arguments=QueueArgs}), | |
io:format("Ok~n"), | |
io:format("Binding queue ..."), | |
#'queue.bind_ok'{}=amqp_channel:call( | |
Channel, #'queue.bind'{queue=Queue, exchange=Exchange, routing_key=RoutingKey}), | |
io:format("Ok~n"), | |
#'basic.qos_ok'{} = amqp_channel:call( | |
Channel, #'basic.qos'{prefetch_count=10}), | |
io:format("Start consuming ..."), | |
#'basic.consume_ok'{consumer_tag = Tag} = amqp_channel:call( | |
Channel, #'basic.consume'{queue=Queue}), | |
io:format("Ok~n"), | |
receive #'basic.consume_ok'{consumer_tag = Tag} -> ok end, | |
consume_loop(Channel), | |
#'basic.cancel_ok'{} = amqp_channel:call( | |
Channel, #'basic.cancel'{consumer_tag = Tag}), | |
ok. |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment