Beekeeper::Client - Make RPC calls through message bus
Version 0.05
my $client = Beekeeper::Client->instance; $client->send_notification( method => "my.service.foo", params => { foo => $foo }, ); my $resp = $client->call_remote( method => "my.service.bar", params => { %args }, ); die uneless $resp->success; print $resp->result; my $req = $client->call_remote_async( method => "my.service.baz", params => { %args }, on_success => sub { my $resp = shift; print resp->result; }, on_error => sub { my $error = shift; die error->message; }, ); $client->wait_async_calls;
This module connects to the message broker and makes RPC calls through message bus.
There are four different methods to do so:
┌───────────────────┬──────────────┬────────┬────────┬────────┐ │ method │ sent to │ queued │ result │ blocks │ ├───────────────────┼──────────────┼────────┼────────┼────────┤ │ call_remote │ 1 worker │ yes │ yes │ yes │ │ call_remote_async │ 1 worker │ yes │ yes │ no │ │ fire_remote │ 1 worker │ yes │ no │ no │ │ send_notification │ many workers │ no │ no │ no │ └───────────────────┴──────────────┴────────┴────────┴────────┘
All methods in this module are exported by default to Beekeeper::Worker.
Beekeeper::Worker
Connects to the message broker and returns a singleton instance.
Unless explicit connection parameters to the broker are provided tries to connect using the configuration from config file bus.config.json.
bus.config.json
Broadcast a notification to the message bus.
All clients and workers listening for method will receive it. If no one is listening the notification is lost.
method
A string with the name of the notification being sent with format "{service_class}.{method}".
"{service_class}.{method}"
An arbitrary value or data structure sent with the notification. It could be undefined, but it should not contain blessed references that cannot be serialized as JSON.
A string with the name of the remote bus when sending notifications to another logical bus. Notifications to another bus need a router shoveling them.
Make this client start accepting specified notifications from message bus.
$method is a string with the format "{service_class}.{method}". A default or fallback handler can be specified using a wildcard as "{service_class}.*".
$method
$callback is a coderef that will be called when a notification is received. When executed, the callback will receive a parameter $params which contains the notification value or data structure sent.
$callback
$params
Note that callbacks will not be executed timely if AnyEvent loop is not running.
Make this client stop accepting specified notifications from message bus.
$method must be one of the strings used previously in accept_notifications.
accept_notifications
Makes a synchronous RPC call to a service worker through the message bus.
It will wait (in the event loop) until a response is received, wich will be either an Beekeeper::JSONRPC::Response object or a Beekeeper::JSONRPC::Error.
Beekeeper::JSONRPC::Response
Beekeeper::JSONRPC::Error
On error it will die unless raise_error option is set to false.
raise_error
This method accepts the following parameters:
A string with the name of the method to be invoked with format "{service_class}.{method}".
An arbitrary value or data structure to be passed as parameters to the defined method. It could be undefined, but it should not contain blessed references that cannot be serialized as JSON.
A string with the name of the remote bus when calling methods of workers connected to another logical bus. Requests to another bus need a router shoveling them.
Time in seconds before cancelling the request and returning an error response. If the request takes too long but otherwise was executed successfully the response will eventually arrive but it will be ignored.
If set to true (the default) dies with the received error message when a call returns an error response. If set to false returns a Beekeeper::JSONRPC::Error instead.
Makes an asynchronous RPC call to a service worker through the message bus.
It returns immediately a Beekeeper::JSONRPC::Request object which, once completed, will have a defined response.
Beekeeper::JSONRPC::Request
response
This method accepts parameters method, params, address and timeout the same as call_remote. Additionally two callbacks can be specified:
params
address
timeout
call_remote
Callback which will be executed after receiving a successful response with a Beekeeper::JSONRPC::Response object as parameter. Must be a coderef.
Callback which will be executed after receiving an error response with a Beekeeper::JSONRPC::Error object as parameter. Must be a coderef.
Fire and forget an asynchronous RPC call to a service worker through the message bus.
It returns undef immediately, there is no way to know if the call was executed successfully or not.
This method accepts parameters method, params and address the same as call_remote.
Wait (running the event loop) until all calls made by call_remote_async are completed either by success, error or timeout.
call_remote_async
Add an arbitrary authentication data blob to subsequent calls or notifications sent.
This data persists for client lifetime in standalone clients. Within worker context it persists until the end of current request only, and will be piggybacked on calls made to another workers within the scope of current request.
The meaning of this data is application specific, this framework doesn't give any special one to it.
Gets the current authentication data blob.
Beekeeper::MQTT, Beekeeper::Worker.
José Micó, jose.mico@gmail.com
jose.mico@gmail.com
Copyright 2015-2021 José Micó.
This is free software; you can redistribute it and/or modify it under the same terms as the Perl 5 programming language itself.
This software is distributed in the hope that it will be useful, but it is provided “as is” and without any express or implied warranties. For details, see the full text of the license in the file LICENSE.
To install Beekeeper, copy and paste the appropriate command in to your terminal.
cpanm
cpanm Beekeeper
CPAN shell
perl -MCPAN -e shell install Beekeeper
For more information on module installation, please visit the detailed CPAN module installation guide.