Tablestore SDK for Java can continuously consume data from a tunnel, process each batch with a callback, and configure heartbeats, checkpoints, thread pools, and consumption concurrency.
Usage notes
-
The retention period of incremental logs is the same as the Stream log expiration period of the table and can be up to seven days. For a BaseAndStream tunnel, if full data consumption does not finish within this period, the
OTSTunnelExpirederror is returned when incremental data consumption begins. The tunnel cannot continue to consume incremental data. -
If incremental consumption falls behind the retention period, the tunnel may resume from the latest available data. As a result, some data may not be consumed.
-
An expired tunnel may be disabled. If the tunnel remains disabled for more than 30 days, it is deleted and cannot be restored.
Prerequisites
Install the Tablestore SDK for Java and initialize TunnelClient.
Feature description
TunnelWorker connects to a tunnel by tunnel ID, uses heartbeats to obtain the channels assigned to the current client, continuously pulls data, and passes each batch of records to IChannelProcessor. If multiple TunnelWorker instances consume the same tunnel, the server distributes channels among the clients.
To consume data from a tunnel:
-
Implement
IChannelProcessor. Use theprocessmethod to process each batch and theshutdownmethod to release resources used by the callback. -
Create a
TunnelWorkerConfigobject to configure the callback and consumption behavior. -
Create a
TunnelWorkerwith the tunnel ID,TunnelClient, andTunnelWorkerConfig. -
Call
connectAndWorkingto start consumption.void process(ProcessRecordsInput input); void shutdown();
The following example prints each record pulled from the tunnel and then starts consumption.
private static class SimpleProcessor implements IChannelProcessor {
@Override
public void process(ProcessRecordsInput input) {
for (StreamRecord record : input.getRecords()) {
System.out.println(record);
}
}
@Override
public void shutdown() {
// Release resources used by the callback.
}
}
String tunnelId = "example_tunnel_id";
TunnelWorkerConfig config =
new TunnelWorkerConfig(new SimpleProcessor());
TunnelWorker worker =
new TunnelWorker(tunnelId, tunnelClient, config);
worker.connectAndWorking();
connectAndWorking returns after it starts background consumption tasks. Keep the application process running. To stop consumption, call worker.shutdown(), config.shutdown(), and tunnelClient.shutdown() in that order. worker.shutdown() closes tunnel connections and invokes the callback's shutdown method. config.shutdown() shuts down the read, processing, and helper thread pools. TunnelWorker registers a JVM shutdown hook that attempts to stop the worker, but the application must still explicitly release these resources.
Parameters
Worker
The TunnelWorker constructor contains the following parameters.
|
Name |
Type |
Description |
|
tunnelId (required) |
String |
The tunnel ID. Obtain it by creating, listing, or querying tunnels. |
|
client (required) |
TunnelClientInterface |
The initialized |
|
workerConfig (required) |
TunnelWorkerConfig |
The callback and consumption behavior configuration. |
Consumption configuration
workerConfig is of the TunnelWorkerConfig type and contains the following parameters.
|
Name |
Type |
Description |
|
channelProcessor (required) |
IChannelProcessor |
The data processing callback. This parameter is required when you use the three-parameter |
|
heartbeatTimeoutInSec (optional) |
long |
The heartbeat timeout in seconds. The default value is |
|
heartbeatIntervalInSec (optional) |
long |
The heartbeat interval in seconds. The default value is |
|
checkpointIntervalInMillis (optional) |
long |
The interval at which consumption checkpoints are recorded on the server, in milliseconds. The default value is |
|
clientTag (optional) |
String |
A custom client tag that is used to generate a client ID and distinguish |
|
readRecordsExecutor (optional) |
ThreadPoolExecutor |
The thread pool that pulls data. The default pool has |
|
processRecordsExecutor (optional) |
ThreadPoolExecutor |
The thread pool that processes data. Its default configuration is the same as |
|
maxChannelParallel (optional) |
int |
The maximum number of channels from which data is concurrently pulled and processed. Use this parameter to limit memory usage. The default value is |
|
channelHelperExecutor (optional) |
ThreadPoolExecutor |
The helper thread pool that initializes channels, schedules pipelines, and handles runtime errors. If this parameter is not set, a cached thread pool is used. |
|
maxRetryIntervalInMillis (optional) |
int |
The maximum base interval for exponential backoff during incremental data pulls, in milliseconds. The default value is |
|
readMaxTimesPerRound (optional) |
int |
The maximum number of |
|
readMaxBytesPerRound (optional) |
int |
The maximum amount of data pulled in one pipeline round, in bytes. The default value is |
|
enableClosingChannelDetect (optional) |
boolean |
Specifies whether to detect channels in the |
If you start multiple TunnelWorker instances on the same machine, you can reuse one TunnelWorkerConfig object to share the read and processing thread pools. After all workers stop, call config.shutdown() only once.
Callback data
The process method receives a ProcessRecordsInput object that contains the following fields.
|
Field |
Type |
Description |
|
records |
|
The records pulled in the current batch. Call |
|
nextToken |
String |
The token for the next batch. Call |
|
traceId |
String |
The trace ID of the current pull request. Call |
|
channelId |
String |
The ID of the channel to which the current batch belongs. Call |
Scenario examples
Tune consumption parameters
If consumption throughput or memory usage does not meet your requirements, adjust the heartbeat and checkpoint intervals, channel concurrency, the number and size of pulls per round, and the backoff interval for incremental pulls.
TunnelWorkerConfig config =
new TunnelWorkerConfig(new SimpleProcessor());
config.setHeartbeatIntervalInSec(10);
config.setHeartbeatTimeoutInSec(60);
config.setCheckpointIntervalInMillis(10_000);
config.setMaxChannelParallel(16);
config.setReadMaxTimesPerRound(4);
config.setReadMaxBytesPerRound(8 * 1024 * 1024);
config.setMaxRetryIntervalInMillis(3_000);