Class JsonRpcClient

java.lang.Object
com.xensource.xenapi.JsonRpcClient

public class JsonRpcClient extends Object
Provides a JSON-RPC v2.0 client for making remote procedure calls to xapi's backend URL.
This class enables the communication to the JSON-RPC backend. The client utilizes the HttpClient class for sending HTTP POST requests with JSON payloads and the ObjectMapper class from the Jackson library for serialization and deserialization of JSON data.
The client can be customised by passing it as a parameter to corresponding constructor, enabling custom handling of requests.

By default, the timeout for requests is set to 600. The default timeout for connecting to the JSON-RPC backend is set to 5 seconds. The maximum number of concurrent connections handled by the underlying PoolingHttpClientConnectionManager is 10.
See Also:
  • CloseableHttpClient is used to make requests and connect to the backend
  • ObjectMapper is used to marshall requests and responses
  • Constructor Summary

    Constructors
    Constructor
    Description
    JsonRpcClient(URL jsonRpcBackendUrl)
    Create a JsonRpcClient with default settings.
    JsonRpcClient(org.apache.hc.client5.http.impl.classic.CloseableHttpClient client, URL jsonRpcBackendUrl)
    Initialize a JsonRpcClient using a custom CloseableHttpClient instance.
  • Method Summary

    Modifier and Type
    Method
    Description
    protected <T> JsonRpcResponse<T>
    sendRequest(String methodCall, Object[] methodParameters, com.fasterxml.jackson.core.type.TypeReference<T> responseTypeReference)
    Send a method call to xapi's backend.
    void
    setConnectionTimeout(int connectionTimeout)
    Set the connection timeout in seconds for this client's PoolingHttpClientConnectionManager.
    void
    setMaxConcurrentConnections(int maxConcurrentConnections)
    Set the maximum number of connections that this client's PoolingHttpClientConnectionManager will keep open.
    void
    setRequestTimeout(int requestTimeout)
    Set the timeout in seconds for every request made by this client.

    Methods inherited from class java.lang.Object

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

    • JsonRpcClient

      public JsonRpcClient(URL jsonRpcBackendUrl)
      Create a JsonRpcClient with default settings.
      Parameters:
      jsonRpcBackendUrl - the URL of the JSON-RPC backend. Usually of the form https://<address>.
      See Also:
    • JsonRpcClient

      public JsonRpcClient(org.apache.hc.client5.http.impl.classic.CloseableHttpClient client, URL jsonRpcBackendUrl)
      Initialize a JsonRpcClient using a custom CloseableHttpClient instance.
      Parameters:
      client - the custom HttpClient to use for all requests
      jsonRpcBackendUrl - the URL of the JSON-RPC backend. Usually of the form https://<address>.
      See Also:
  • Method Details

    • setRequestTimeout

      public void setRequestTimeout(int requestTimeout)
      Set the timeout in seconds for every request made by this client. If not set the value defaults to 600.
      Parameters:
      requestTimeout - the timeout value in seconds
      See Also:
      • RequestConfig.Builder.setConnectionRequestTimeout(long, TimeUnit)
    • setConnectionTimeout

      public void setConnectionTimeout(int connectionTimeout)
      Set the connection timeout in seconds for this client's PoolingHttpClientConnectionManager. If not set the value defaults to 5.
      Parameters:
      connectionTimeout - the client's connection timeout in seconds.
      See Also:
      • ConnectionConfig.Builder.setConnectTimeout(Timeout)
    • setMaxConcurrentConnections

      public void setMaxConcurrentConnections(int maxConcurrentConnections)
      Set the maximum number of connections that this client's PoolingHttpClientConnectionManager will keep open. If not set the value defaults to 10.
      Parameters:
      maxConcurrentConnections - the maximum number of connections managed by the connection manager
      See Also:
      • ConnPoolControl.setMaxTotal(int)
    • sendRequest

      protected <T> JsonRpcResponse<T> sendRequest(String methodCall, Object[] methodParameters, com.fasterxml.jackson.core.type.TypeReference<T> responseTypeReference) throws IOException
      Send a method call to xapi's backend. You need to provide the type of the data returned by a successful response.
      Type Parameters:
      T - The type of the response's payload. For instance, a map of opaque references to VM objects is expected when calling VM.get_all_records
      Parameters:
      methodCall - the JSON-RPC xapi method call. e.g.: session.login_with_password
      methodParameters - the parameters of the method call
      responseTypeReference - the type of the response, wrapped with a TypeReference
      Returns:
      a JsonRpcResponse object. If its error field is empty, the response was successful.
      Throws:
      com.fasterxml.jackson.core.JsonProcessingException - if the request's payload or the response's payload cannot be written or read as valid JSON
      IOException - if an I/O error occurs when sending or receiving