Class ContextSource

java.lang.Object
services.moleculer.context.ContextSource
Direct Known Subclasses:
Context, ServiceBroker

public abstract class ContextSource extends Object
Base superclass of all objects that can send events (eg. ServiceBroker or Context).
  • Field Details

    • eventbus

      protected final Eventbus eventbus
      Eventbus of the ServiceBroker or Context.
    • serviceInvoker

      protected final ServiceInvoker serviceInvoker
      Service Invoker of the ServiceBroker or Context.
    • uidGenerator

      protected final UidGenerator uidGenerator
      UID Generator of the ServiceBroker or Context.
    • nodeID

      protected final String nodeID
      Source nodeID
  • Constructor Details

  • Method Details

    • emit

      public void emit(String name, Object... params)
      Emits an event to ONE listener from ALL (or the specified) event group(s), who are listening this event. The service broker uses the default strategy of 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 optional event group container
    • emit

      public void emit(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. The service broker uses the default strategy of 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 - Tree structure (payload of the event)
      groups - event group container
    • emit

      public void emit(String name, io.datatree.Tree payload)
      Emits an event to ONE listener from ALL event groups, who are listening this event. The service broker uses the default strategy of 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 - Tree structure (payload of the event)
    • emit

      public void emit(String name, PacketStream stream)
      Emits an event to ONE listener from ALL event groups, who are listening this event. The service broker uses the default strategy of the broker for event redirection and node selection.
      Parameters:
      name - name of event (eg. "user.created")
      stream - streamed data
    • emit

      public void emit(String name, PacketStream stream, Groups groups)
      Emits an event to ONE listener from ALL event groups, who are listening this event. The service broker uses the default strategy of the broker for event redirection and node selection.
      Parameters:
      name - name of event (eg. "user.created")
      stream - streamed data
      groups - event group container
    • broadcast

      public void broadcast(String name, Object... params)
      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 optional event group container
    • broadcast

      public void broadcast(String name, io.datatree.Tree payload, Groups groups)
      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 - Tree structure (payload of the event)
      groups - event group container
    • broadcast

      public void broadcast(String name, io.datatree.Tree payload)
      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 - Tree structure (payload of the event)
    • broadcast

      public void broadcast(String name, PacketStream stream)
      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

      public void broadcast(String name, PacketStream stream, Groups groups)
      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
      groups - event group container
    • broadcastLocal

      public void broadcastLocal(String name, Object... params)
      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 optional event group container
    • broadcastLocal

      public void broadcastLocal(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. 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 - Tree structure (payload of the event)
      groups - event group container
    • broadcastLocal

      public void broadcastLocal(String name, io.datatree.Tree payload)
      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 - Tree structure (payload of the event)
    • call

      public io.datatree.Promise call(String name, Object... params)
      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

      public io.datatree.Promise call(String name, io.datatree.Tree params)
      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 - Tree structure (input parameters of the method call)
      Returns:
      response Promise
    • call

      public io.datatree.Promise call(String name, io.datatree.Tree params, CallOptions.Options opts)
      Calls an action (local or remote).
      Parameters:
      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)
      Returns:
      response Promise
    • call

      public io.datatree.Promise call(String name, PacketStream stream)
      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

      public io.datatree.Promise call(String name, PacketStream stream, CallOptions.Options opts)
      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

      public PacketStream 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)