public class ServiceBroker
extends java.lang.Object
ServiceBroker broker = new ServiceBroker("node-1");
broker.createService(new Service("math") {
public Action add = ctx -> {
return ctx.params.get("a").asInteger() + ctx.params.get("b").asInteger();
};
});
broker.start();
broker.call("math.add", "a", 5, "b", 3).then(rsp -> {
broker.getLogger().info("Response: " + rsp.asInteger());
});
This project is based on the idea of Moleculer Microservices Framework for
Node.js (https://moleculer.services). Special thanks to the Moleculer's
project owner (https://github.com/icebob) for the consultations.| Modifier and Type | Field and Description |
|---|---|
protected ServiceBrokerConfig |
config
Configuration settings and internal components (event bus, cacher,
service registry, etc.) of this node / broker.
|
protected ContextFactory |
contextFactory
Context generator / factory.
|
protected Eventbus |
eventbus
Implementation of the event bus of the current node.
|
protected static org.slf4j.Logger |
logger
SLF4J logger of this class.
|
protected java.util.LinkedHashSet<Middleware> |
middlewares
Middlewares which defined and added to the Broker before the boot
process.
|
protected java.lang.String |
nodeID
Unique ID of this node (~= ServiceBroker instance).
|
static java.lang.String |
PROTOCOL_VERSION
Version of the implemented Moleculer Protocol.
|
protected ServiceInvoker |
serviceInvoker
Service invoker.
|
protected ServiceRegistry |
serviceRegistry
Implementation of the service registry of the current node.
|
protected java.util.LinkedHashMap<java.lang.String,Service> |
services
Services which defined and added to the Broker before the boot process.
|
static java.lang.String |
SOFTWARE_VERSION
Version of the Java ServiceBroker API.
|
protected StrategyFactory |
strategyFactory
Default (round-robin) service invocation factory.
|
protected Transporter |
transporter
Implementation of the Transporter.
|
protected UidGenerator |
uidGenerator
UID generator (each
Context
instance has its own identifier). |
| Constructor and Description |
|---|
ServiceBroker()
Creates a new ServiceBroker without
Transporter. |
ServiceBroker(ServiceBrokerConfig config)
Creates a new ServiceBroker by the specified
configuration. |
ServiceBroker(java.lang.String nodeID)
Creates a new ServiceBroker without
Transporter and with the
specified nodeID. |
ServiceBroker(java.lang.String nodeID,
Cacher cacher,
Transporter transporter)
|
| Modifier and Type | Method and Description |
|---|---|
void |
broadcast(java.lang.String name,
java.lang.Object... params)
Emits an event to ALL listeners from ALL (or the specified) event
group(s), who are listening this event.
|
void |
broadcast(java.lang.String name,
io.datatree.Tree payload)
Emits an event to ALL listeners from ALL event groups, who are
listening this event.
|
void |
broadcast(java.lang.String name,
io.datatree.Tree payload,
Groups groups)
Emits an event to ALL listeners from the specified event group(s),
who are listening this event.
|
void |
broadcastLocal(java.lang.String name,
java.lang.Object... params)
Emits a LOCAL event to ALL listeners from ALL (or the
specified) event group(s), who are listening this event.
|
void |
broadcastLocal(java.lang.String name,
io.datatree.Tree payload)
Emits a LOCAL event to ALL listeners from ALL event groups,
who are listening this event.
|
void |
broadcastLocal(java.lang.String name,
io.datatree.Tree payload,
Groups groups)
Emits a LOCAL event to ALL listeners from the specified
event group(s), who are listening this event.
|
static ServiceBrokerBuilder |
builder()
Creates a new
ServiceBrokerBuilder instance. |
io.datatree.Promise |
call(java.lang.String name,
java.lang.Object... params)
Calls an action (local or remote).
|
io.datatree.Promise |
call(java.lang.String name,
io.datatree.Tree params)
Calls an action (local or remote).
|
io.datatree.Promise |
call(java.lang.String name,
io.datatree.Tree params,
CallOptions.Options opts)
Calls an action (local or remote).
|
void |
createService(Service service)
Installs a new service instance and notifies other nodes about the
actions/listeners of the new service.
|
void |
createService(java.lang.String name,
Service service)
Installs a new service with the specified name (eg.
|
void |
emit(java.lang.String name,
java.lang.Object... params)
Emits an event to ONE listener from ALL (or the specified) event
group(s), who are listening this event.
|
void |
emit(java.lang.String name,
io.datatree.Tree payload)
Emits an event to ONE listener from ALL event groups, who are
listening this event.
|
void |
emit(java.lang.String name,
io.datatree.Tree payload,
Groups groups)
Emits an event to ONE listener from the specified event group(s),
who are listening this event.
|
Action |
getAction(java.lang.String actionName)
Returns an action by name.
|
Action |
getAction(java.lang.String actionName,
java.lang.String nodeID)
Returns an action by name and nodeID.
|
ServiceBrokerConfig |
getConfig()
Returns the configuration settings and internal components (event bus,
cacher, service registry, etc.) of this node / broker.
|
Service |
getLocalService(java.lang.String serviceName)
Returns a local service by name (eg.
|
org.slf4j.Logger |
getLogger()
Returns the SLF4J logger of this broker instance.
|
org.slf4j.Logger |
getLogger(java.lang.Class<?> clazz)
Returns a logger named corresponding to the class passed as parameter.
|
org.slf4j.Logger |
getLogger(java.lang.String name)
Return a logger named according to the name parameter.
|
java.lang.String |
getNodeID()
Returns the unique
nodeID of this node (~= ServiceBroker
instance). |
io.datatree.Promise |
ping(long timeoutMillis,
java.lang.String nodeID)
Sends a PING message to the specified node.
|
io.datatree.Promise |
ping(java.lang.String nodeID)
Sends a PING message to the specified node.
|
boolean |
repl()
Starts a local (System in/out) developer console.
|
boolean |
repl(boolean local)
Starts a local (System in/out) or a remote (telnet-based) developer
console.
|
void |
start()
Start broker.
|
protected <TYPE extends MoleculerComponent> |
start(TYPE component)
Starts the specified
MoleculerComponent. |
void |
stop()
Stop broker and all internal components (event bus, context factory,
etc.).
|
protected void |
stop(MoleculerComponent component)
Stops the specified
MoleculerComponent. |
void |
use(java.util.Collection<Middleware> middlewares)
Installs a collection of middlewares.
|
void |
use(Middleware... middlewares)
Installs one or an array of middleware(s).
|
io.datatree.Promise |
waitForServices(long timeoutMillis,
java.util.Collection<java.lang.String> services)
Waits for a collection of services.
|
io.datatree.Promise |
waitForServices(long timeoutMillis,
java.lang.String... services)
Waits for one (or an array of) service(s).
|
io.datatree.Promise |
waitForServices(java.lang.String... services)
Waits for one or more services.
|
public static final java.lang.String SOFTWARE_VERSION
public static final java.lang.String PROTOCOL_VERSION
protected static final org.slf4j.Logger logger
protected final java.lang.String nodeID
protected final ServiceBrokerConfig config
getConfig method to access this object.protected final java.util.LinkedHashMap<java.lang.String,Service> services
protected final java.util.LinkedHashSet<Middleware> middlewares
protected UidGenerator uidGenerator
Context
instance has its own identifier). Use
getConfig().getUidGenerator() to access this instance.IncrementalUidGenerator,
StandardUidGeneratorprotected StrategyFactory strategyFactory
getConfig().getStrategyFactory() to access this instance.protected ContextFactory contextFactory
getConfig().getContextFactory() to access this instance.DefaultContextFactoryprotected ServiceInvoker serviceInvoker
getConfig().getServiceInvoker() to
access this instance.DefaultServiceInvoker,
CircuitBreakerprotected Eventbus eventbus
getConfig().getEventbus() to access this instance.protected ServiceRegistry serviceRegistry
getConfig().getServiceRegistry() to access this instance.DefaultServiceRegistry,
call(String, Object...)protected Transporter transporter
getConfig().getTransporter() to access this instance. Can be
null.public ServiceBroker(ServiceBrokerConfig config)
configuration.config - configuration of the Brokerpublic ServiceBroker()
Transporter. The
nodeID is generated from the host name and process ID.public ServiceBroker(java.lang.String nodeID)
Transporter and with the
specified nodeID.nodeID - the unique nodeID of this nodepublic ServiceBroker(java.lang.String nodeID,
Cacher cacher,
Transporter transporter)
nodeID - the unique nodeID of this nodecacher - Cacher of this broker instancetransporter - Transporter of this broker instancepublic static ServiceBrokerBuilder builder()
ServiceBrokerBuilder instance. Sample of usage:public ServiceBrokerConfig getConfig()
public java.lang.String getNodeID()
nodeID of this node (~= ServiceBroker
instance).nodeIDpublic void start()
throws java.lang.Exception
fatal - error (missing classes or JARs, used port, etc.)java.lang.Exceptionprotected <TYPE extends MoleculerComponent> TYPE start(TYPE component) throws java.lang.Exception
MoleculerComponent.TYPE - component to startjava.lang.Exception - any configuration or I/O exceptionspublic void stop()
protected void stop(MoleculerComponent component)
MoleculerComponent.component - component to stoppublic org.slf4j.Logger getLogger()
public org.slf4j.Logger getLogger(java.lang.Class<?> clazz)
clazz - the returned logger will be named after clazzpublic org.slf4j.Logger getLogger(java.lang.String name)
name - the name of the loggerpublic void createService(Service service)
service - the new service instancepublic void createService(java.lang.String name,
Service service)
name - custom service name (eg. "user", "logger", "configurator",
etc.)service - the new service instancepublic Service getLocalService(java.lang.String serviceName)
serviceName - service name (eg. "user", "logger", "configurator", etc.)NoSuchElementException - if the service name is not validpublic void use(java.util.Collection<Middleware> middlewares)
middlewares - collection of middlewarespublic void use(Middleware... middlewares)
middlewares - array of middlewarespublic Action getAction(java.lang.String actionName)
actionName - name of the action (in "service.action" syntax, eg.
"math.add")public Action getAction(java.lang.String actionName, java.lang.String nodeID)
actionName - name of the action (in "service.action" syntax, eg.
"math.add")nodeID - node identifier where the service is locatedpublic io.datatree.Promise call(java.lang.String name,
java.lang.Object... params)
name - action name (eg. "math.add" in "service.action" syntax)params - list of parameter name-value pairs and an optional CallOptionspublic io.datatree.Promise call(java.lang.String name,
io.datatree.Tree params)
name - action name (eg. "math.add" in "service.action" syntax)params - Tree structure (input parameters of the method call)public io.datatree.Promise call(java.lang.String name,
io.datatree.Tree params,
CallOptions.Options opts)
name - action name (eg. "math.add" in "service.action" syntax)params - Tree structure (input parameters of the method call)opts - calling options (target nodeID, call timeout, number of
retries)public void emit(java.lang.String name,
java.lang.Object... params)
strategy of the broker for event redirection and
node selection. Sample code:name - name of event (eg. "user.deleted")params - list of parameter name-value pairs and an optional
event group containerpublic void emit(java.lang.String name,
io.datatree.Tree payload,
Groups groups)
strategy of the broker for event redirection and node
selection. Sample code:name - name of event (eg. "user.modified")payload - Tree structure (payload of the event)groups - event group containerpublic void emit(java.lang.String name,
io.datatree.Tree payload)
strategy of the broker for event redirection and node selection. Sample
code:name - name of event (eg. "user.created")payload - Tree structure (payload of the event)public void broadcast(java.lang.String name,
java.lang.Object... params)
name - name of event (eg. "user.deleted")params - list of parameter name-value pairs and an optional
event group containerpublic void broadcast(java.lang.String name,
io.datatree.Tree payload,
Groups groups)
name - name of event (eg. "user.modified")payload - Tree structure (payload of the event)groups - event group containerpublic void broadcast(java.lang.String name,
io.datatree.Tree payload)
name - name of event (eg. "user.created")payload - Tree structure (payload of the event)public void broadcastLocal(java.lang.String name,
java.lang.Object... params)
name - name of event (eg. "user.deleted")params - list of parameter name-value pairs and an optional
event group containerpublic void broadcastLocal(java.lang.String name,
io.datatree.Tree payload,
Groups groups)
name - name of event (eg. "user.modified")payload - Tree structure (payload of the event)groups - event group containerpublic void broadcastLocal(java.lang.String name,
io.datatree.Tree payload)
name - name of event (eg. "user.created")payload - Tree structure (payload of the event)public io.datatree.Promise waitForServices(java.lang.String... services)
services - service namespublic io.datatree.Promise waitForServices(long timeoutMillis,
java.lang.String... services)
timeoutMillis - timeout in millisecondsservices - array of service namespublic io.datatree.Promise waitForServices(long timeoutMillis,
java.util.Collection<java.lang.String> services)
timeoutMillis - timeout in millisecondsservices - collection of service namespublic io.datatree.Promise ping(java.lang.String nodeID)
nodeID - node ID of the destination nodepublic io.datatree.Promise ping(long timeoutMillis,
java.lang.String nodeID)
timeoutMillis - ping timeout in millisecondsnodeID - node ID of the destination nodepublic boolean repl()
public boolean repl(boolean local)
local - true = local console, false = telnet-based console