Created
September 29, 2017 18:53
-
-
Save stevenlivz/53f67492c4c0e82bded1dbd459ee8055 to your computer and use it in GitHub Desktop.
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
-module(sub_server). | |
-behaviour(gen_server). | |
-export([start_link/0]). | |
%% gen_server callbacks | |
-export([init/1, | |
handle_call/3, | |
handle_cast/2, | |
handle_info/2, | |
terminate/2, | |
code_change/3]). | |
-export([retrieve/0]). | |
-record(state, {}). | |
start_link() -> | |
erlang:display("sub_server start_link"), | |
gen_server:start_link({local, ?MODULE}, ?MODULE, [], []). | |
init([]) -> | |
erlang:display("sub_server init"), | |
retrieve(), | |
{ok, #state{}}. | |
handle_call(_Request, _From, State) -> | |
{reply, ignored, State}. | |
handle_cast(_Msg, State) -> | |
{noreply, State}. | |
handle_info(_Info, State) -> | |
erlang:display("ready"), | |
{noreply, State}. | |
terminate(_Reason, _State) -> | |
ok. | |
code_change(_OldVsn, State, _Extra) -> | |
{ok, State}. | |
%% Internal functions | |
retrieve() -> | |
io:format("Connecting ...\n"), | |
{ok, Address} = application:get_env(myapp, "Address"), | |
{ok, Hostname} = application:get_env(myapp, "Hostname"), | |
{ok, Port} = application:get_env(myapp, "Port"), | |
{ok, User} = application:get_env(myapp, "User"), | |
{ok, Password} = application:get_env(myapp, "Password"), | |
{ok, Container} = application:get_env(myapp, "Queue"), | |
{ok, Queue} = application:get_env(myapp, "Queue"), | |
OpnConf = #{ | |
address => Address, | |
hostname => Hostname, | |
port => Port, | |
notify => self(), | |
tls_opts => {secure_port, [{versions, ['tlsv1.1']}]}, | |
container_id => Container, | |
sasl => {plain, User, Password} | |
}, | |
{ok, Connection} = amqp10_client:open_connection(OpnConf), | |
receive | |
{amqp10_event, {connection, Connection, opened}} -> | |
io:format("Connected ...\n") | |
after 2000 -> | |
exit(connection_timeout) | |
end, | |
{ok, Session} = amqp10_client:begin_session(Connection), | |
receive | |
{amqp10_event, {session, Session, begun}} -> | |
io:format("Session Id ...\n") | |
after 2000 -> | |
exit(session_timeout) | |
end, | |
erlang:display("Creating receiver ..."), | |
% create a receiver link | |
{ok, Receiver} = amqp10_client:attach_receiver_link(Session, | |
<<"test-receiver">>, | |
Queue), | |
erlang:display("Getting credit ..."), | |
% grant some credit to the remote sender but don't auto-renew it | |
ok = amqp10_client:flow_link_credit(Receiver, 5, 3), | |
erlang:display("Waiting Delivery ..."), | |
% wait for a delivery | |
listener(), | |
% close off | |
erlang:display("Closing Session ..."), | |
ok = amqp10_client:detach_link(Receiver), | |
ok = amqp10_client:end_session(Session), | |
ok = amqp10_client:close_connection(Connection), | |
ok. | |
listener() -> | |
% wait for a delivery | |
receive | |
{amqp10_msg, Receiver, InMsg} -> | |
erlang:display("Got Message ..."), | |
erlang:display(InMsg), | |
ok | |
after infinity -> | |
exit(delivery_timeout) | |
end, | |
erlang:display("Listening ..."), | |
listener(). |
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
%%%------------------------------------------------------------------- | |
%% @doc myapp top level supervisor. | |
%% @end | |
%%%------------------------------------------------------------------- | |
-module(sub_server_sup). | |
-behaviour(supervisor). | |
%% API | |
-export([start_link/0]). | |
%% Supervisor callbacks | |
-export([init/1]). | |
-define(SERVER, ?MODULE). | |
%%==================================================================== | |
%% API functions | |
%%==================================================================== | |
start_link() -> | |
supervisor:start_link({local, ?SERVER}, ?MODULE, []). | |
%%==================================================================== | |
%% Supervisor callbacks | |
%%==================================================================== | |
%% Child :: {Id,StartFunc,Restart,Shutdown,Type,Modules} | |
init([]) -> | |
SupFlags = #{strategy => one_for_one, intensity => 1, period => 5}, | |
ChildSpecs = [#{id => sub_server, | |
start => {sub_server, start_link, []}, | |
restart => permanent, | |
shutdown => brutal_kill, | |
type => worker, | |
modules => [sub_server]}], | |
erlang:display("sub_server_sup"), | |
{ok, {SupFlags, ChildSpecs}}. | |
% {ok, { {one_for_all, 0, 1}, []} }. | |
%%==================================================================== | |
%% Internal functions | |
%%==================================================================== |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment