Class DirectClient<T>

java.lang.Object
org.pipservices4.rpc.clients.DirectClient<T>
All Implemented Interfaces:
org.pipservices4.components.config.IConfigurable, org.pipservices4.components.refer.IReferenceable, org.pipservices4.components.run.IClosable, org.pipservices4.components.run.IOpenable

public abstract class DirectClient<T> extends Object implements org.pipservices4.components.config.IConfigurable, org.pipservices4.components.run.IOpenable, org.pipservices4.components.refer.IReferenceable
Abstract client that calls service directly in the same memory space.

It is used when multiple microservices are deployed in a single container (monolyth) and communication between them can be done by direct calls rather then through the network.

### Configuration parameters ###

  • dependencies:
    • service: override service descriptor

### References ###

  • *:logger:*:*:1.0 (optional) ILogger components to pass log messages
  • *:counters:*:*:1.0 (optional) ICounters components to pass collected measurements
  • *:tracer:*:*:1.0 (optional) ITracer components to record traces
  • *:service:*:*:1.0 service to call business methods

### Example ###

 
 class MyDirectClient extends DirectClient<IMyService> implements IMyClient {

   public MyDirectClient() {
       super();
       this._dependencyResolver.put('service', new Descriptor(
           "mygroup", "service", "*", "*", "*"));
    }
    ...

    public MyData getData(IContext context, String id) {
        Timing timing = this.instrument(context, 'myclient.get_data');
        MyData result = this._service.getData(context, id);
        timing.endTiming();
        return result;
    }
    ...
 }

 MyDirectClient client = new MyDirectClient();
 client.setReferences(References.fromTuples(
     new Descriptor("mygroup","service","default","default","1.0"), service
 ));

 MyData data = client.getData("123", "1");
 ...
 
 
  • Field Summary

    Fields
    Modifier and Type
    Field
    Description
    protected org.pipservices4.observability.count.CompositeCounters
    The performance counters
    protected org.pipservices4.components.refer.DependencyResolver
    The dependency resolver to get service reference.
    protected org.pipservices4.observability.log.CompositeLogger
    The logger.
    protected boolean
    The open flag.
    protected T
    The service reference.
    protected org.pipservices4.observability.trace.CompositeTracer
    The tracer.
  • Constructor Summary

    Constructors
    Constructor
    Description
    Creates a new instance of the client.
  • Method Summary

    Modifier and Type
    Method
    Description
    void
    close(org.pipservices4.components.context.IContext context)
    Closes component and frees used resources.
    void
    configure(org.pipservices4.components.config.ConfigParams config)
    Configures component by passing configuration parameters.
    instrument(org.pipservices4.components.context.IContext context, String name)
    Adds instrumentation to log calls and measure call time.
    boolean
    Checks if the component is opened.
    void
    open(org.pipservices4.components.context.IContext context)
    Opens the component.
    void
    setReferences(org.pipservices4.components.refer.IReferences references)
    Sets references to dependent components.

    Methods inherited from class java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Field Details

    • _service

      protected T _service
      The service reference.
    • _logger

      protected org.pipservices4.observability.log.CompositeLogger _logger
      The logger.
    • _counters

      protected org.pipservices4.observability.count.CompositeCounters _counters
      The performance counters
    • _dependencyResolver

      protected org.pipservices4.components.refer.DependencyResolver _dependencyResolver
      The dependency resolver to get service reference.
    • _opened

      protected boolean _opened
      The open flag.
    • _tracer

      protected org.pipservices4.observability.trace.CompositeTracer _tracer
      The tracer.
  • Constructor Details

    • DirectClient

      public DirectClient()
      Creates a new instance of the client.
  • Method Details

    • configure

      public void configure(org.pipservices4.components.config.ConfigParams config) throws org.pipservices4.commons.errors.ConfigException
      Configures component by passing configuration parameters.
      Specified by:
      configure in interface org.pipservices4.components.config.IConfigurable
      Parameters:
      config - configuration parameters to be set.
      Throws:
      org.pipservices4.commons.errors.ConfigException - when configuration is wrong.
    • setReferences

      public void setReferences(org.pipservices4.components.refer.IReferences references) throws org.pipservices4.components.refer.ReferenceException
      Sets references to dependent components.
      Specified by:
      setReferences in interface org.pipservices4.components.refer.IReferenceable
      Parameters:
      references - references to locate the component dependencies.
      Throws:
      org.pipservices4.components.refer.ReferenceException - when no found references.
    • instrument

      protected InstrumentTiming instrument(org.pipservices4.components.context.IContext context, String name)
      Adds instrumentation to log calls and measure call time. It returns a Timing object that is used to end the time measurement.
      Parameters:
      context - (optional) a context to trace execution through call chain.
      name - a method name.
      Returns:
      Timing object to end the time measurement.
    • isOpen

      public boolean isOpen()
      Checks if the component is opened.
      Specified by:
      isOpen in interface org.pipservices4.components.run.IOpenable
      Returns:
      true if the component has been opened and false otherwise.
    • open

      public void open(org.pipservices4.components.context.IContext context) throws org.pipservices4.commons.errors.ConnectionException
      Opens the component.
      Specified by:
      open in interface org.pipservices4.components.run.IOpenable
      Parameters:
      context - (optional) a context to trace execution through call chain.
      Throws:
      org.pipservices4.commons.errors.ConnectionException - when service reference is missing.
    • close

      public void close(org.pipservices4.components.context.IContext context)
      Closes component and frees used resources.
      Specified by:
      close in interface org.pipservices4.components.run.IClosable
      Parameters:
      context - (optional) a context to trace execution through call chain.