DirectClient<T>

Abstract client that calls a controller directly in the same memory space.

Implements: IConfigurable, IReferenceable, IOpenable

Description

The DirectClientclass allows you to create clients that call a controller directly in the same memory space.

Important points

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

Configuration parameters

  • dependencies:
    • controller: override controller descriptor

References

  • *:logger:*:*:1.0 - (optional) ILogger components to pass log messages
  • *:counters:*:*:1.0 - (optional) ICounters components to pass collected measurements
  • *:traces:*:*:1.0 - (optional) ITracer components to record traces
  • *:discovery:*:*:1.0 - (optional) IDiscovery services to resolve a connection

Fields

_service

The controller reference.

protected T _service T

_opened

The open flag.

protected boolean _opened = false

_logger

The logger.

protected CompositeLogger _logger = new CompositeLogger()

_counters

The performance counters

protected CompositeCounters _counters = new CompositeCounters()

_dependencyResolver

The dependency resolver used to get the controller’s reference.

protected DependencyResolver _dependencyResolver = new DependencyResolver()

_tracer

protected CompositeTracer _tracer = new CompositeTracer()

Instance methods

close

Closes a component and frees used resources.

public void close(IContext context)

  • context: IContext - (optional) a context to trace execution through call chain.

configure

Configures component by passing configuration parameters.

public void configure(ConfigParams config) throws ConfigException

  • config: ConfigParams - configuration parameters to be set.

instrument

Adds instrumentation to log calls and measures call time. It returns a Timing object that is used to end the time measurement.

protected InstrumentTiming instrument(IContext context, String name)

  • context: IContext - (optional) a context to trace execution through call chain.
  • name: string - method name.
  • returns: InstrumentTiming - InstrumentTiming object used to end the time measurement.

isOpen

Checks if the component is open.

public boolean isOpen()

  • returns: boolean - True if the component has been opened and False otherwise.

open

Opens the component.

public void open(IContext context) throws ConnectionException

  • context: IContext - (optional) a context to trace execution through call chain.

setReferences

Sets references to dependent components.

public void setReferences(IReferences references) throws ReferenceException

  • references: IReferences - references to locate the component dependencies.

Examples

 {
  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");
  ...
  }