| Author |
Message |
< Open Telecom Platform (OTP) ~ How to properly add rabbitmq consumer worker to supervisor |
| chakhedik |
Posted: Tue Jul 12, 2011 8:33 am |
|
|
|
Joined: 12 Jul 2011
Posts: 2
|
Hi,
I would like to know how to add rabbitmq consumer as worker in supervisor from this sample code https://github.com/rabbitmq/rabbitmq-tutorials/blob/master/erlang/receive.erl.
This is what I tried and basically it works.
Code: -module(rmqworker).
-export([start/1, init/0]).
-include_lib("amqp_client/include/amqp_client.hrl").
start(_) ->
Pid = proc_lib:spawn_link(?MODULE, init, []),
{ok, Pid}.
init() ->
{ok, Connection} = amqp_connection:start(network, #amqp_params{host = "localhost"}),
{ok, Channel} = amqp_connection:open_channel(Connection),
amqp_channel:call(Channel, #'queue.declare'{queue = <<"queue">>, durable = true}),
amqp_channel:subscribe(Channel, #'basic.consume'{queue = <<"queue">>}, self()),
receive
#'basic.consume_ok'{} -> ok
end,
loop(Channel).
loop(Channel) ->
receive
{#'basic.deliver'{delivery_tag = Tag, routing_key = Queue}, #amqp_msg{payload = Body}} ->
io:format("Body : ~p~n", [Body]),
amqp_channel:cast(Channel, #'basic.ack'{delivery_tag = Tag}),
loop(Channel)
end.
Supervisor init():
Code: init([]) ->
Web = web_specs(rmqworker_web, 8001),
RMQWorker = {rmqworker, {rmqworker, start, []}, permanent, 5000, worker, dynamic},
Processes = [Web, RMQWorker],
Strategy = {one_for_one, 10, 10},
{ok, {Strategy, lists:flatten(Processes)}}.
However, if there's unacknowledged message in queue and ask supervisor to terminate and restart this child, it'll get started again but it doesn't process that unacknowledged message. Also, the new message in queue will not be processed too.
If supervisor restart this child after a crash, also it'll get restarted but will not process any active or unacknowledged queue.
Any help would be much appreciated.
Thanks,
Chak |
Last edited by chakhedik on Fri Jul 15, 2011 3:48 am; edited 1 time in total |
|
| Back to top |
|
| chakhedik |
Posted: Thu Jul 14, 2011 6:38 pm |
|
|
|
Joined: 12 Jul 2011
Posts: 2
|
Finally, I found the proper and correct way, here I would like to share the codes.
Code: -module(rmqworker).
-export([start_link/0, init/1]).
-include_lib("amqp_client/include/amqp_client.hrl").
start_link() ->
proc_lib:start_link(?MODULE, init, [self()]).
init(Parent) ->
{ok, Connection} = amqp_connection:start(network, #amqp_params{host = "localhost"}),
{ok, Channel} = amqp_connection:open_channel(Connection),
amqp_channel:call(Channel, #'queue.declare'{queue = <<"queue">>, durable = true}),
amqp_channel:subscribe(Channel, #'basic.consume'{queue = <<"queue">>}, self()),
receive
#'basic.consume_ok'{} -> ok
end,
proc_lib:init_ack(Parent, {ok, Connection}),
loop(Channel).
loop(Channel) ->
receive
{#'basic.deliver'{delivery_tag = Tag, routing_key = Queue}, #amqp_msg{payload = Body}} ->
io:format("Body : ~p~n", [Body]),
amqp_channel:cast(Channel, #'basic.ack'{delivery_tag = Tag}),
loop(Channel)
end. |
|
|
| Back to top |
|
| nike air max |
Posted: Thu Jan 05, 2012 1:51 am |
|
|
|
Guest
|
| uggs canada produced by individuals have darker color sight who would like glow blue furthermore to atmosphere friendly sight. |
|
|
| Back to top |
|
| babyjing |
Posted: Thu Jan 12, 2012 2:41 am |
|
|
|
Guest
|
| Popular nfl jerseys cheap are going to be scarce over the next three months.Then they're going to be more expensive.The most popular teams will experience the worst shortages, and the James Jones Jersey are breaking all sorts of popularity records this year, from on-field performance to stock sales to merchandise sales.To lure the customer towards their service, providers are leaving no stone unturned in order to provide several plans to make Josh Sitton Jersey.Thats why your website's critics are so vital, and ofcourse why companies must be alerted at all times.With the sport's new passing-crazed style of play challenging the traditional notion that Buffalo Sabres jerseys is a fundamental ingredient for postseason success, these Nick Collins Jersey will provide an intriguing look at the way to win in the NFL when the stakes are highest. |
|
|
| Back to top |
|
| fastrack |
Posted: Fri May 11, 2012 11:47 am |
|
|
|
Joined: 11 May 2012
Posts: 1
Location: USA
|
import sys
message = ' '.join(sys.argv[1:]) or "Hello World!"
channel.basic_publish(exchange='',
routing_key='hello',
body=message)
print " [x] Sent %r" % (message,)
Bad Credit Auto Loan
online car loan |
|
|
| Back to top |
|
|
|
All times are GMT
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum You can attach files in this forum You can download files in this forum
|
|
|