Class KinesisTemplate

java.lang.Object
io.awspring.cloud.kinesis.operations.KinesisTemplate
All Implemented Interfaces:
KinesisOperations

public class KinesisTemplate extends Object implements KinesisOperations
Default KinesisOperations implementation, sending records through a KinesisAsyncClient.

Synchronous methods delegate to their asynchronous counterparts and unwrap the CompletionException so callers see the original AWS exception. A batch is split into PutRecords calls of at most 500 records and 5 MiB each, a conservative size below the current 10 MiB PutRecords limit; the chunks are sent sequentially and their results merged in request order.

When an ObservationRegistry is set, every send and batch send is recorded as a KinesisTemplateObservation named spring.aws.kinesis.template. A split batch is still recorded as one observation carrying the total record and failed counts.

Since:
4.3.0
Author:
Matej Nedic
  • Constructor Details

    • KinesisTemplate

      public KinesisTemplate(software.amazon.awssdk.services.kinesis.KinesisAsyncClient kinesisAsyncClient, tools.jackson.databind.json.JsonMapper jsonMapper)
      Creates a template converting payloads with the default converter backed by the given JsonMapper.
      Parameters:
      kinesisAsyncClient - the client to send records with.
      jsonMapper - the mapper used to serialize non-byte[]/String payloads.
    • KinesisTemplate

      public KinesisTemplate(software.amazon.awssdk.services.kinesis.KinesisAsyncClient kinesisAsyncClient, KinesisMessageConverter messageConverter)
      Creates a template converting payloads with the given converter.
      Parameters:
      kinesisAsyncClient - the client to send records with.
      messageConverter - the converter used to serialize payloads.
  • Method Details

    • setObservationRegistry

      public void setObservationRegistry(io.micrometer.observation.ObservationRegistry observationRegistry)
      Set the ObservationRegistry send operations are observed with. Defaults to ObservationRegistry.NOOP.
      Parameters:
      observationRegistry - the registry.
    • setObservationConvention

      public void setObservationConvention(KinesisTemplateObservation.Convention observationConvention)
      Set a custom KinesisTemplateObservation.Convention overriding the default observation name and key values.
      Parameters:
      observationConvention - the convention.
    • setBatchRetryBackOff

      public void setBatchRetryBackOff(@Nullable BackOff batchRetryBackOff)
      Set a BackOff used to resend batch entries that failed with a retryable error code (ProvisionedThroughputExceededException, InternalFailure). The failed entries are resent in a new PutRecords call after each back-off interval, without blocking a thread, until none remain or the back-off stops; entries with other error codes are never retried. The merged BatchSendResult reports each record once: successful if any attempt succeeded, otherwise with its last error. Defaults to null, which disables retrying.

      Retried records can be written after records that followed them in the batch, so per-shard order is not kept for retried records. The back-off must be bounded.

      Parameters:
      batchRetryBackOff - the back-off, or null to disable retrying.
    • send

      public SendResult send(String streamName, Object payload)
      Sends a record with a random partition key, so it is distributed across the stream's shards.

      The partition key is a random UUID.

      Specified by:
      send in interface KinesisOperations
      Parameters:
      streamName - the target stream.
      payload - the payload to serialize and send.
      Returns:
      the sequence number and shard the record was written to.
    • send

      public SendResult send(String streamName, String partitionKey, Object payload)
      Sends a record with the given partition key, which determines the shard and therefore the ordering group.
      Specified by:
      send in interface KinesisOperations
      Parameters:
      streamName - the target stream.
      partitionKey - the partition key.
      payload - the payload to serialize and send.
      Returns:
      the sequence number and shard the record was written to.
    • send

      public SendResult send(SendRequest request)
      Sends the given record, allowing all record attributes to be set.

      Blocks on sendAsync(SendRequest). A failure is rethrown as the original exception, such as the AWS SDK exception, instead of a CompletionException; a checked cause is wrapped in an IllegalStateException.

      Specified by:
      send in interface KinesisOperations
      Parameters:
      request - the record to send.
      Returns:
      the sequence number and shard the record was written to.
    • sendAsync

      public CompletableFuture<SendResult> sendAsync(String streamName, String partitionKey, Object payload)
      Asynchronously sends a record with the given partition key.
      Specified by:
      sendAsync in interface KinesisOperations
      Parameters:
      streamName - the target stream.
      partitionKey - the partition key.
      payload - the payload to serialize and send.
      Returns:
      a future completing with the sequence number and shard the record was written to.
    • sendAsync

      public CompletableFuture<SendResult> sendAsync(SendRequest request)
      Asynchronously sends the given record.

      The record is serialized with the configured KinesisMessageConverter and sent with a single PutRecord call, including the request's explicit hash key and sequence number for ordering. Serialization runs on the calling thread, so a conversion failure is thrown by this method rather than returned as a failed future; errors from Kinesis complete the future exceptionally.

      Specified by:
      sendAsync in interface KinesisOperations
      Parameters:
      request - the record to send.
      Returns:
      a future completing with the sequence number and shard the record was written to.
    • sendBatch

      public BatchSendResult sendBatch(String streamName, List<SendRequest> requests)
      Sends any number of records to the same stream, splitting them into PutRecords calls of at most 500 records and 5 MiB each, a conservative size below the current 10 MiB PutRecords limit, and merging the results in request order. Individual records can fail while a call as a whole succeeds, so the result reports successful and failed records separately.

      Blocks on sendBatchAsync(String, List) and rethrows a failure of a whole PutRecords call as the original exception instead of a CompletionException. Records Kinesis rejects individually do not throw; they are reported in BatchSendResult.failed().

      Specified by:
      sendBatch in interface KinesisOperations
      Parameters:
      streamName - the target stream, which every request must target.
      requests - the records to send.
      Returns:
      the successful and failed records of the batch, in request order.
    • sendBatchAsync

      public CompletableFuture<BatchSendResult> sendBatchAsync(String streamName, List<SendRequest> requests)
      Asynchronously sends any number of records to the same stream, splitting them into PutRecords calls of at most 500 records and 5 MiB each, a conservative size below the current 10 MiB PutRecords limit.

      All requests are serialized on the calling thread before the first call, so a conversion failure is thrown by this method and nothing is sent. The chunks are sent one after another, so records of a shard keep their request order unless a batch retry back-off resends some of them. If a PutRecords call fails as a whole, the future completes exceptionally and records of earlier chunks may already have been written. SendRequest.sequenceNumberForOrdering() is ignored, because PutRecords does not support it.

      Specified by:
      sendBatchAsync in interface KinesisOperations
      Parameters:
      streamName - the target stream, which every request must target.
      requests - the records to send.
      Returns:
      a future completing with the successful and failed records of the batch, in request order.