[rabbitmq-discuss] Calls to management functions

Ben Hood 0x6e6562 at gmail.com
Mon Jul 9 15:47:51 BST 2007


> There two ways to do this in general:
>
> 1) make the particular gen_server implementation higher-order. In the
> above case we would pass handle_management_message etc functions as
> arguments on initialisation.

I looked into this by trying to parameterize the server with a
callback module. However, I was starting the server in a supervision
tree and the child specification calls apply/3 on the server under the
hood which doesn't allow to parameterize the server. I couldn't see
how to do by passing a function to the init() method, because I would
need to register this in the handle_call() callback. I suppose you
could save it to a database in the init method and then query it in
the handle_call() method, but this seems a bit complicated to me.

>
> 2) create a custom behaviour

Looked into that as well. However, extending the gen_server behaviour
only adds extra callbacks that you would have implement in each
implementation in addition to the standard callback, hence not saving
you any code.

What I ended up doing (I don't know if this is the most elegant
solution, but it seemed to be pragmatic) was to adapt the
rabbit_management module to implement my
own gen_server module. The server is started by an OTP supervisor
(I've created an OTP app using rabbit as dependency) and the module
just contains the bare bones gen_server behaviour callbacks as well as
the business logic. I refactored the exchange initialization, message
parsing, hessian decoding, function invocation, reply encoding and
sending to an external rpc utility module for more generic reuse (for
example if I want to have other servers that implement different
business logic).




More information about the rabbitmq-discuss mailing list