All Products
Search
Document Center

Tablestore:Initialize TunnelClient

Last Updated:Jul 31, 2026

Tablestore SDK for Java uses a dedicated client to manage tunnels and start data consumption. Initialize the client with an instance name, endpoint, and access credentials.

Prerequisites

Install Tablestore SDK for Java and configure access credentials. The example uses V4 signing and requires version 5.17.5 or later.

Feature description

TunnelClient is the dedicated client for Tunnel Service. Tunnel management operations and data consumption workers use this client to access the service. Reuse a client instance within a process, and call shutdown after all tunnel operations finish.

The following constructor initializes TunnelClient:

public TunnelClient(
    String endpoint,
    CredentialsProvider credsProvider,
    String instanceName,
    ClientConfiguration config,
    ResourceManager resourceManager
)

The following example uses the default client configuration and an exclusive resource manager to initialize TunnelClient. It then sends a read-only listTunnel request for an existing table to validate the endpoint, access credentials, and network connection. Before you run the example, set region, instanceName, and endpoint based on your instance information. Set tableName to the name of an existing data table in the Wide Column model for connectivity verification.

import com.alicloud.openservices.tablestore.TunnelClient;
import com.alicloud.openservices.tablestore.core.auth.CredentialsProvider;
import com.alicloud.openservices.tablestore.core.auth.DefaultCredentialProvider;
import com.alicloud.openservices.tablestore.core.auth.DefaultCredentials;
import com.alicloud.openservices.tablestore.core.auth.V4Credentials;
import com.alicloud.openservices.tablestore.model.tunnel.ListTunnelRequest;
import com.alicloud.openservices.tablestore.model.tunnel.ListTunnelResponse;

public class InitTunnelClient {

    public static void main(String[] args) {
        String accessKeyId = System.getenv("TABLESTORE_ACCESS_KEY_ID");
        String accessKeySecret = System.getenv("TABLESTORE_ACCESS_KEY_SECRET");
        String region = "<region-id>";
        String instanceName = "<instance-name>";
        String endpoint = "<instance-endpoint>";
        String tableName = "<table-name>";

        TunnelClient tunnelClient = null;
        try {
            DefaultCredentials credentials = new DefaultCredentials(accessKeyId, accessKeySecret);
            V4Credentials credentialsV4 = V4Credentials.createByServiceCredentials(credentials, region);
            CredentialsProvider provider = new DefaultCredentialProvider(credentialsV4);

            tunnelClient = new TunnelClient(
                endpoint,
                provider,
                instanceName,
                null,
                null
            );

            ListTunnelResponse response = tunnelClient.listTunnel(new ListTunnelRequest(tableName));
            System.out.println("TunnelClient initialized. Tunnels: " + response.getTunnelInfos().size());
            System.out.println("RequestId: " + response.getRequestId());
        } finally {
            if (tunnelClient != null) {
                tunnelClient.shutdown();
            }
        }
    }
}

Parameters

Client

The TunnelClient constructor contains the following parameters.

Name

Type

Description

endpoint (required)

String

The endpoint of the Tablestore instance. Use a public, VPC, or classic network endpoint that matches the client network.

credsProvider (required)

CredentialsProvider

The credentials provider. The example uses an AccessKey ID, an AccessKey secret, and the instance region ID to create V4 credentials.

instanceName (required)

String

The name of the Tablestore instance.

config (optional)

ClientConfiguration

The client configuration. If you set this parameter to null, the default configuration is used. Specify a custom configuration to adjust connection, timeout, or retry settings.

resourceManager (optional)

ResourceManager

The resource manager for client connections and threads. If you set this parameter to null, the current client creates and releases the resource manager. Specify a shared resource manager when multiple clients need to share underlying resources.

Client configuration

The config parameter is of the ClientConfiguration type. Use the corresponding setXxx methods to configure connection, timeout, and retry settings. If you do not need custom settings, set config to null. The following table describes common settings.

Name

Type

Description

maxConnections (optional)

int

The maximum number of open HTTP connections. Default value: 300.

ioThreadCount (optional)

int

The number of I/O threads for the asynchronous HTTP client. The default value is the number of processors available to the runtime.

connectionTimeoutInMillisecond (optional)

int

The timeout period for establishing a connection. Unit: milliseconds. Default value: 30000.

socketTimeoutInMillisecond (optional)

int

The timeout period for transmitting data over an established connection. Unit: milliseconds. Default value: 30000.

connectionRequestTimeoutInMillisecond (optional)

int

The timeout period for obtaining a connection from the connection pool. Unit: milliseconds. Default value: -1, which specifies that no separate timeout period is configured.

retryThreadCount (optional)

int

The number of threads used for error retries. Default value: 1.

retryStrategy (optional)

RetryStrategy

The request retry strategy. Default value: DefaultRetryStrategy.

Other client settings, such as proxy, compression, DNS cache, request tracing, and SSL session settings, are also configured by using the corresponding methods of ClientConfiguration.

Resource manager

The resourceManager parameter is of the ResourceManager type. For an independently used TunnelClient, set this parameter to null. The client creates a resource manager based on config and releases its resources when you call shutdown.

To customize or share resources, use ResourceManager(ClientConfiguration configuration, ExecutorService callbackExecutor) to create an owning ResourceManager. The constructor contains the following parameters.

Name

Type

Description

configuration (optional)

ClientConfiguration

The client configuration used to create HTTP connections and retry threads. If you set this parameter to null, the default configuration is used.

callbackExecutor (optional)

ExecutorService

The thread pool used to run asynchronous callbacks. If you set this parameter to null, the SDK creates the default thread pool. The thread pool is shut down when the owning ResourceManager is shut down.

Scenario examples

Customize the client configuration

To adjust the default connection, timeout, or retry settings, import ClientConfiguration in the basic example and replace the client initialization code with the following code:

ClientConfiguration config = new ClientConfiguration();
config.setMaxConnections(200);
config.setConnectionTimeoutInMillisecond(10000);
config.setSocketTimeoutInMillisecond(30000);
config.setRetryThreadCount(2);

tunnelClient = new TunnelClient(
    endpoint,
    provider,
    instanceName,
    config,
    null
);

Share client resources

If multiple clients need to share HTTP connections, retry threads, and callback threads, create an owning ResourceManager and pass the shared instance returned by sharedResourceManager() to each client. After you close all clients, close the owning ResourceManager.

Important

Do not pass an owning ResourceManager directly to multiple clients. Otherwise, when any client calls shutdown, the shared resources are released.

The following code reuses the variables and credentials provider from the basic example. Import ClientConfiguration and ResourceManager before you run the code.

ClientConfiguration config = new ClientConfiguration();
ResourceManager owner = new ResourceManager(config);
TunnelClient tunnelClientA = new TunnelClient(
    endpoint, provider, instanceName, config, owner.sharedResourceManager());
TunnelClient tunnelClientB = new TunnelClient(
    endpoint, provider, instanceName, config, owner.sharedResourceManager());

try {
    tunnelClientA.listTunnel(new ListTunnelRequest(tableName));
    tunnelClientB.listTunnel(new ListTunnelRequest(tableName));
} finally {
    tunnelClientA.shutdown();
    tunnelClientB.shutdown();
    owner.shutdown();
}