[rabbitmq-discuss] amqp_rpc_server
Carlo Bertoldi
mcbain at tiscali.it
Thu Jul 18 09:20:31 BST 2013
Ok :)
I've added start_link functions to both client and server, so the
caller has the possibility do decide what he needs, without breaking
the old behaviour.
Here are the patches:
--- amqp_rpc_client_orig.erl 2013-07-18 10:13:40.000000000 +0200
+++ amqp_rpc_client.erl 2013-07-18 10:13:04.000000000 +0200
@@ -1,4 +1,4 @@
- %% The contents of this file are subject to the Mozilla Public License
+%% The contents of this file are subject to the Mozilla Public License
%% Version 1.1 (the "License"); you may not use this file except in
%% compliance with the License. You may obtain a copy of the License at
%% http://www.mozilla.org/MPL/
@@ -25,7 +25,7 @@
-behaviour(gen_server).
--export([start/2, stop/1]).
+-export([start/2, start_link/2, stop/1]).
-export([call/2]).
-export([init/1, terminate/2, code_change/3, handle_call/3,
handle_cast/2, handle_info/2]).
@@ -53,6 +53,18 @@
{ok, Pid} = gen_server:start(?MODULE, [Connection, Queue], []),
Pid.
+%% @spec (Connection, Queue) -> RpcClient
+%% where
+%% Connection = pid()
+%% Queue = binary()
+%% RpcClient = pid()
+%% @doc Starts, and link to, a new RPC client instance that sends requests
+%% to a specified queue. This function returns the pid of the RPC client
+%% process that can be used to invoke RPCs and stop the client.
+start_link(Connection, Queue) ->
+ {ok, Pid} = gen_server:start_link(?MODULE, [Connection, Queue], []),
+ Pid.
+
%% @spec (RpcClient) -> ok
%% where
%% RpcClient = pid()
--- amqp_rpc_server_orig.erl 2013-07-18 10:13:54.000000000 +0200
+++ amqp_rpc_server.erl 2013-07-18 10:12:54.000000000 +0200
@@ -27,7 +27,7 @@
-export([init/1, terminate/2, code_change/3, handle_call/3,
handle_cast/2, handle_info/2]).
--export([start/3]).
+-export([start/3, start_link/3]).
-export([stop/1]).
-record(state, {channel,
@@ -51,6 +51,20 @@
{ok, Pid} = gen_server:start(?MODULE, [Connection, Queue, Fun], []),
Pid.
+%% @spec (Connection, Queue, RpcHandler) -> RpcServer
+%% where
+%% Connection = pid()
+%% Queue = binary()
+%% RpcHandler = function()
+%% RpcServer = pid()
+%% @doc Starts, and links to, a new RPC server instance that receives
+%% requests via a specified queue and dispatches them to a specified
+%% handler function. This function returns the pid of the RPC server that
+%% can be used to stop the server.
+start_link(Connection, Queue, Fun) ->
+ {ok, Pid} = gen_server:start_link(?MODULE, [Connection, Queue, Fun], []),
+ Pid.
+
%% @spec (RpcServer) -> ok
%% where
%% RpcServer = pid()
On Wed, Jul 17, 2013 at 9:47 PM, Matthias Radestock
<matthias at rabbitmq.com> wrote:
> On 17/07/13 13:51, Carlo Bertoldi wrote:
>>
>> I'm looking through the amqp_rpm_server, in particular the start
>> function. I wonder why you used a start instead of a start_link.
>
>
> Laziness. There is nothing stopping you from linking to the returned pid
> yourself, though obviously at the risk of getting the wrong exit code if the
> process dies immediately after starting up successfully.
>
> Matthias.
--
È molto più bello sapere qualcosa di tutto, che sapere tutto di una cosa.
Blaise Pascal
More information about the rabbitmq-discuss
mailing list