Class CommandableGrpcClient

java.lang.Object
org.pipservices4.grpc.clients.GrpcClient
org.pipservices4.grpc.clients.CommandableGrpcClient
All Implemented Interfaces:
org.pipservices4.components.config.IConfigurable, org.pipservices4.components.refer.IReferenceable, org.pipservices4.components.run.IClosable, org.pipservices4.components.run.IOpenable
Direct Known Subclasses:
TestCommandableGrpcClient

public class CommandableGrpcClient extends GrpcClient
Abstract client that calls commandable GRPC service.

Commandable services are generated automatically for ICommandable objects. Each command is exposed as Invoke method that receives all parameters as args.

### Configuration parameters ###

 - connection(s):
   - discovery_key:         (optional) a key to retrieve the connection from IDiscovery
   - protocol:              connection protocol: http or https
   - host:                  host name or IP address
   - port:                  port number
   - uri:                   resource URI or connection string with all parameters in it
 - options:
   - retries:               number of retries (default: 3)
   - connect_timeout:       connection timeout in milliseconds (default: 10 sec)
   - timeout:               invocation timeout in milliseconds (default: 10 sec)
   

### References ###

- *:logger:\*:\*:1.0 (optional) ILogger components to pass log messages - *:counters:\*:\*:1.0 (optional) ICounters components to pass collected measurements - *:discovery:*:*:1.0 (optional) IDiscovery services to resolve connection ### Example ###

 
 class MyCommandableGrpcClient extends CommandableGrpcClient implements IMyClient {

     public MyCommandableGrpcClient() {
         super("mydata");
     }

     // ...

     @Override
     public MyData getData(IContext context, String id) {
         return this.callCommand(MyData.class,
                 "get_data",
                 context,
                 Map.of("id", id)
         );
     }

     public static void main(String[] args) throws ConfigException {
         var client = new MyCommandableGrpcClient();
         client.configure(ConfigParams.fromTuples(
                 "connection.protocol", "http",
                 "connection.host", "localhost",
                 "connection.port", 8080
         ));

         var result = client.getData("123", "1");
     }
 }
 
  • Field Details

    • _name

      protected String _name
      The service name
  • Constructor Details

    • CommandableGrpcClient

      public CommandableGrpcClient(String name)
      Create new instance of the commandable client
      Parameters:
      name - The service name
  • Method Details

    • callCommand

      protected <T> T callCommand(Class<T> returnType, String name, org.pipservices4.components.context.IContext context, Object params)
      Calls a remote method via GRPC commadable protocol. The call is made via Invoke method and all parameters are sent in args object. The complete route to remote method is defined as serviceName + "." + name.
      Parameters:
      returnType - generic type of the return object
      name - a name of the command to call.
      context - (optional) a context to trace execution through call chain.
      params - command parameters.
      Returns:
      the received result.