Created
August 14, 2014 01:33
-
-
Save bjhaid/d12d5dcde91a3f845593 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
-module(spike). | |
-include("./deps/amqp_client/include/amqp_client.hrl"). | |
-export([start/1]). | |
start(Queue) -> | |
amqp_lifecycle(Queue). | |
amqp_lifecycle(Queue) -> | |
{ok, Connection} = amqp_connection:start(#amqp_params_network{}), | |
Channel = amqp_connection:open_channel(Connection), | |
log(queue,Queue), | |
log(setup_consumer,"start"), | |
setup_consumer(Channel, Queue), | |
log(setup_consumer,"finished"). | |
setup_consumer(Channel, Queue) -> | |
log(setup_consumer,"basic.consume"), | |
BasicConsume = #'basic.consume'{queue = Queue, | |
no_ack = true}, | |
amqp_channel:call(Channel, BasicConsume), | |
log(setup_consumer,"basic.consume_ok start receive"), | |
receive | |
#'basic.consume_ok'{consumer_tag = ConsumerTag} -> ok | |
end, | |
log(setup_consumer,"basic.consume_ok finished"), | |
log(read_messages,"start"), | |
Msg = read_messages(0), | |
io:format("Msg: ~p~n", [Msg]), | |
log(read_messages,"finish"), | |
log(basic_cancel,"start"), | |
BasicCancel = #'basic.cancel'{consumer_tag = ConsumerTag}, | |
#'basic.cancel_ok'{consumer_tag = ConsumerTag} = amqp_channel:call(Channel,BasicCancel). | |
read_messages(Timeouts) -> | |
receive | |
{#'basic.deliver'{consumer_tag=_ConsumerTag, delivery_tag=_DeliveryTag, redelivered=_Redelivered, exchange=_Exchange, routing_key=RoutingKey}, Content} -> | |
log(read_messages,"basic.deliver"), | |
io:format("RoutingKey received: ~p~n", [RoutingKey]), | |
#amqp_msg{payload = Payload} = Content, | |
io:format("Payload received: ~p~n", [Payload]), | |
read_messages(0); | |
Any -> | |
io:format("received unexpected Any: ~p~n", [Any]), | |
read_messages(0) | |
after 1000 -> | |
case Timeouts of | |
0 -> | |
Timeouts2 = Timeouts + 1, | |
read_messages(Timeouts2); | |
5 -> | |
io:format("~n"), | |
io:format("Message timeout exceeded ~n"); | |
_ -> | |
Timeouts2 = Timeouts + 1, | |
io:format("."), | |
read_messages(Timeouts2) | |
end | |
end. | |
log(Key,Value) -> | |
io:format("~p: ~p~n",[Key,Value]). |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment