Class ContextSource
java.lang.Object
services.moleculer.context.ContextSource
- Direct Known Subclasses:
Context, ServiceBroker
Base superclass of all objects that can send events (eg. ServiceBroker or
Context).
-
Field Summary
FieldsModifier and TypeFieldDescriptionprotected final EventbusEventbus of the ServiceBroker or Context.protected final StringSource nodeIDprotected final ServiceInvokerService Invoker of the ServiceBroker or Context.protected final UidGeneratorUID Generator of the ServiceBroker or Context. -
Constructor Summary
ConstructorsModifierConstructorDescriptionprotectedContextSource(ServiceInvoker serviceInvoker, Eventbus eventbus, UidGenerator uidGenerator, String nodeID) -
Method Summary
Modifier and TypeMethodDescriptionvoidSends an event to ALL listeners from ALL event groups, who are listening this event.voidSends an event to ALL listeners from the specified event group(s), who are listening this event.protected voidbroadcast(String name, io.datatree.Tree payload, Groups groups, PacketStream stream, CallOptions.Options opts, boolean local) voidSends an event to ALL listeners from ALL (or the specified) event group(s), who are listening this event.voidbroadcast(String name, PacketStream stream) Sends an event to ALL listeners from ALL event groups, who are listening this event.voidbroadcast(String name, PacketStream stream, Groups groups) Sends an event to ALL listeners from ALL event groups, who are listening this event.voidbroadcastLocal(String name, io.datatree.Tree payload) Emits a LOCAL event to ALL listeners from ALL event groups, who are listening this event.voidbroadcastLocal(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.voidbroadcastLocal(String name, Object... params) Emits a LOCAL event to ALL listeners from ALL (or the specified) event group(s), who are listening this event.io.datatree.PromiseCalls an action (local or remote).io.datatree.Promisecall(String name, io.datatree.Tree params, CallOptions.Options opts) Calls an action (local or remote).protected io.datatree.Promisecall(String name, io.datatree.Tree params, CallOptions.Options opts, PacketStream stream) io.datatree.PromiseCalls an action (local or remote).io.datatree.Promisecall(String name, PacketStream stream) Calls an action (local or remote).io.datatree.Promisecall(String name, PacketStream stream, CallOptions.Options opts) Calls an action (local or remote).Creates a stream what is suitable for transferring large files (or other "unlimited" media content) between Moleculer Nodes.voidEmits an event to ONE listener from ALL event groups, who are listening this event.voidEmits an event to ONE listener from the specified event group(s), who are listening this event.protected voidemit(String name, io.datatree.Tree payload, Groups groups, PacketStream stream, CallOptions.Options opts) voidEmits an event to ONE listener from ALL (or the specified) event group(s), who are listening this event.voidemit(String name, PacketStream stream) Emits an event to ONE listener from ALL event groups, who are listening this event.voidemit(String name, PacketStream stream, Groups groups) Emits an event to ONE listener from ALL event groups, who are listening this event.
-
Field Details
-
eventbus
Eventbus of the ServiceBroker or Context. -
serviceInvoker
Service Invoker of the ServiceBroker or Context. -
uidGenerator
UID Generator of the ServiceBroker or Context. -
nodeID
Source nodeID
-
-
Constructor Details
-
ContextSource
protected ContextSource(ServiceInvoker serviceInvoker, Eventbus eventbus, UidGenerator uidGenerator, String nodeID)
-
-
Method Details
-
emit
Emits an event to ONE listener from ALL (or the specified) event group(s), who are listening this event. The service broker uses the defaultstrategyof the broker for event redirection and node selection. Sample code:
ctx.emit("user.deleted", "a", 1, "b", 2);
...or send event to (one or more) listener group(s):
ctx.emit("user.deleted", "a", 1, "b", 2, Groups.of("logger"));- Parameters:
name- name of event (eg. "user.deleted")params- list of parameter name-value pairs and an optionalevent groupcontainer
-
emit
Emits an event to ONE listener from the specified event group(s), who are listening this event. The service broker uses the defaultstrategyof the broker for event redirection and node selection. Sample code:
Tree params = new Tree();
params.put("a", true);
params.putList("b").add(1).add(2).add(3);
ctx.emit("user.created", params, Groups.of("group1", "group2"));- Parameters:
name- name of event (eg. "user.modified")payload-Treestructure (payload of the event)groups-event groupcontainer
-
emit
Emits an event to ONE listener from ALL event groups, who are listening this event. The service broker uses the defaultstrategyof the broker for event redirection and node selection. Sample code:
Tree params = new Tree();
params.put("a", true);
params.putList("b").add(1).add(2).add(3);
ctx.emit("user.modified", params);- Parameters:
name- name of event (eg. "user.created")payload-Treestructure (payload of the event)
-
emit
Emits an event to ONE listener from ALL event groups, who are listening this event. The service broker uses the defaultstrategyof the broker for event redirection and node selection.- Parameters:
name- name of event (eg. "user.created")stream- streamed data
-
emit
Emits an event to ONE listener from ALL event groups, who are listening this event. The service broker uses the defaultstrategyof the broker for event redirection and node selection.- Parameters:
name- name of event (eg. "user.created")stream- streamed datagroups-event groupcontainer
-
broadcast
Sends an event to ALL listeners from ALL (or the specified) event group(s), who are listening this event. Sample code:
ctx.broadcast("user.deleted", "a", 1, "b", 2);
...or send event to (one or more) listener group(s):
ctx.broadcast("user.deleted", "a", 1, "b", 2, Groups.of("logger"));- Parameters:
name- name of event (eg. "user.deleted")params- list of parameter name-value pairs and an optionalevent groupcontainer
-
broadcast
Sends an event to ALL listeners from the specified event group(s), who are listening this event. Sample code:
Tree params = new Tree();
params.put("a", true);
params.putList("b").add(1).add(2).add(3);
ctx.broadcast("user.created", params, Groups.of("group1", "group2"));- Parameters:
name- name of event (eg. "user.modified")payload-Treestructure (payload of the event)groups-event groupcontainer
-
broadcast
Sends an event to ALL listeners from ALL event groups, who are listening this event. Sample code:
Tree params = new Tree();
params.put("a", true);
params.putList("b").add(1).add(2).add(3);
ctx.broadcast("user.modified", params);- Parameters:
name- name of event (eg. "user.created")payload-Treestructure (payload of the event)
-
broadcast
Sends an event to ALL listeners from ALL event groups, who are listening this event.- Parameters:
name- name of event (eg. "user.created")stream- streamed data
-
broadcast
Sends an event to ALL listeners from ALL event groups, who are listening this event.- Parameters:
name- name of event (eg. "user.created")stream- streamed datagroups-event groupcontainer
-
broadcastLocal
Emits a LOCAL event to ALL listeners from ALL (or the specified) event group(s), who are listening this event. Sample code:
ctx.broadcastLocal("user.deleted", "a", 1, "b", 2);
...or send event to (one or more) local listener group(s):
ctx.broadcastLocal("user.deleted", "a", 1, "b", 2, Groups.of("logger"));- Parameters:
name- name of event (eg. "user.deleted")params- list of parameter name-value pairs and an optionalevent groupcontainer
-
broadcastLocal
Emits a LOCAL event to ALL listeners from the specified event group(s), who are listening this event. Sample code:
Tree params = new Tree();
params.put("a", true);
params.putList("b").add(1).add(2).add(3);
ctx.broadcastLocal("user.created", params, Groups.of("group1", "group2"));- Parameters:
name- name of event (eg. "user.modified")payload-Treestructure (payload of the event)groups-event groupcontainer
-
broadcastLocal
Emits a LOCAL event to ALL listeners from ALL event groups, who are listening this event. Sample code:
Tree params = new Tree();
params.put("a", true);
params.putList("b").add(1).add(2).add(3);
ctx.broadcastLocal("user.modified", params);- Parameters:
name- name of event (eg. "user.created")payload-Treestructure (payload of the event)
-
call
Calls an action (local or remote). Sample code:
broker.call("service.action").then(ctx -> {
// Nested call:
return ctx.call("math.add", "a", 1, "b", 2);
});
...or with CallOptions:
return ctx.call("math.add", "a", 1, "b", 2, CallOptions.nodeID("node2"));- Parameters:
name- action name (eg. "math.add" in "service.action" syntax)params- list of parameter name-value pairs and an optional CallOptions- Returns:
- response Promise
-
call
Calls an action (local or remote). Sample code:
broker.call("service.action").then(ctx -> {
// Nested call:
Tree params = new Tree();
params.put("a", true);
params.putList("b").add(1).add(2).add(3);
rerturn ctx.call("math.add", params);
});- Parameters:
name- action name (eg. "math.add" in "service.action" syntax)params-Treestructure (input parameters of the method call)- Returns:
- response Promise
-
call
Calls an action (local or remote).- Parameters:
name- action name (eg. "math.add" in "service.action" syntax)params-Treestructure (input parameters of the method call)opts- calling options (target nodeID, call timeout, number of retries)- Returns:
- response Promise
-
call
Calls an action (local or remote).- Parameters:
name- action name (eg. "math.add" in "service.action" syntax)stream- streamed data (optional)- Returns:
- response Promise
-
call
Calls an action (local or remote).- Parameters:
name- action name (eg. "math.add" in "service.action" syntax)stream- streamed data (optional)opts- calling options (target nodeID, call timeout, number of retries)- Returns:
- response Promise
-
createStream
Creates a stream what is suitable for transferring large files (or other "unlimited" media content) between Moleculer Nodes. Sample:
public Action send = ctx -> { PacketStream reqStream = ctx.createStream(); ctx.call("service.action", reqStream).then(rsp -> { // Receive bytes into file PacketStream rspStream = (PacketStream) rsp.asObject(); rspStream.transferTo(new File("out")); } // Send bytes from file reqStream.transferFrom(new File("in")); }- Returns:
- new stream
-
emit
protected void emit(String name, io.datatree.Tree payload, Groups groups, PacketStream stream, CallOptions.Options opts) -
broadcast
protected void broadcast(String name, io.datatree.Tree payload, Groups groups, PacketStream stream, CallOptions.Options opts, boolean local) -
call
protected io.datatree.Promise call(String name, io.datatree.Tree params, CallOptions.Options opts, PacketStream stream)
-