Main Content

Configure the Client-Server Connection

You configure the client-server connection using an object that implements the MWHttpClientConfig interface. This interface defines these properties:

  • Interruptible determines if MATLAB® functions can be interrupted

  • TimeOutMs determines the amount of time, in milliseconds, the client waits for a response before timing out

  • MaxConnections - determines the maximum number of connections the client opens to fulfill multiple requests

  • ResponseSizeLimit - determines the maximum size, in bytes, of the response a client accepts.

The API provides a default implementation, MWHttpClientDefaultConfig, that is automatically used when an HTTP client is instantiated. To modify the configuration, extend MWHttpClientDefaultConfig and pass it to the HTTP client constructor.

Create a Connection with the Default Configuration

When you create a client connection using the default constructor, MWHttpClient(), an instance of MWHttpClientDefaultConfig is automatically used to configure the client-server connection. The default configuration sets these connection properties:

  • Interruptible = false

  • TimeOutMs = 120000

  • MaxConnections = -1, specifying that the client uses as many connections as the system allows

  • ResponseSizeLimit = 64*1024*1024 (64 MB)

Create a Connection with a Custom Configuration

To change one or more connection properties:

  1. Implement a custom connection configuration by extending the MWHttpClientDefaultConfig interface.

  2. Create the client connection using one of the constructors that accepts a configuration object:

    • MWHttpClient(MWHttpClientConfig config)

    • MWHttpClient(MWHttpClientConfig config, MWSSLConfig sslConfig)

This code sample creates a client connection with a timeout value of 1000 ms:

class MyClientConfig extends MWHttpClientDefaultConfig 
{
   public long getTimeOutMs()
   {
     return 1000;
   }
 }
 ...
 MWClient client = new MWHttpClient(new MyClientConfig());
 ... 

For information see Customize Security Configuration.

Implement a Custom Connection Configuration

To implement a custom connection configuration extend the MWHttpClientDefaultConfig interface. The MWHttpClientDefaultConfig interface has one getter method for each configuration property:

  • public int getMaxConnectionsPerAddress() returns the value for the maximum number of connections a client can use to handle simultaneous requests.

  • public long getTimeOutMs() returns the number of milliseconds the client will wait for a response before generating an error.

  • public boolean isInterruptible() returns a boolean specifying if the MATLAB function can be interrupted while waiting for a response.

    Note

    If this method returns false, then getMaxConnectionsPerAddress() must return -1.

  • public int getResponseSizeLimit() returns the maximum number of bytes the client can accept in a response.

You only need to overrides the getters for properties you wish to change. For example, to specify that a client times out after 1 s and can accept 4 MB responses, override getTimeOutMs() and getResponseSizeLimit():

class MyClientConfig extends MWHttpClientDefaultConfig
 {
   public long getTimeOutMs()
   {
     return 60000;
   }
   public int getResponseSizeLimit()
   {
     return 4*1024*1024;
   }
 }