Class AbstractSubscription<T extends AbstractSubscription<T>>
- All Implemented Interfaces:
Provider,AutoCloseable
Implementations should handle proper resource cleanup in the close method, including flushing pending acknowledgments, closing connections, and stopping background threads.
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionstatic classAbstractSubscription.Builder<T extends AbstractSubscription<T>> -
Field Summary
FieldsModifier and TypeFieldDescriptionprotected final CredentialsOverriderprotected final URIprotected final AtomicBooleanFlag indicating whether the subscription has been shut down.protected final AtomicReference<Throwable>protected final Stringprotected final URIprotected final Stringprotected final Stringprotected final AtomicReference<Throwable> -
Constructor Summary
ConstructorsModifierConstructorDescriptionprotectedprotectedAbstractSubscription(String providerId, String subscriptionName, String region, CredentialsOverrider credentialsOverrider) -
Method Summary
Modifier and TypeMethodDescriptionabstract booleancanNack()voidclose()Implements AutoCloseable interface for try-with-resources support.protected abstract Batcher.Optionsprotected Batcher.OptionsCreates batcher options for this subscription.doReceiveBatch(int batchSize) Provider specific implementation must fetch at most batchSize messages from the remote service.protected abstract voiddoSendAcks(List<AckID> ackIDs) protected abstract voiddoSendNacks(List<AckID> ackIDs) abstract GetAttributeResultabstract Class<? extends SubstrateSdkException>Maps a given Throwable from the provider implementation to a specific SubstrateSdkException.Retrieves the unique identifier for this provider.abstract booleanisRetryable(Throwable error) receive()Receives and returns the next message from the Subscription's queue.voidSends acknowledgment for a single message.Sends acknowledgments for multiple messages.voidSends negative acknowledgment for a single message.Sends negative acknowledgment for multiple messages.
-
Field Details
-
providerId
-
subscriptionName
-
region
-
endpoint
-
proxyEndpoint
-
credentialsOverrider
-
isShutdown
Flag indicating whether the subscription has been shut down. -
permanentError
-
unreportedAckErr
-
-
Constructor Details
-
AbstractSubscription
protected AbstractSubscription(String providerId, String subscriptionName, String region, CredentialsOverrider credentialsOverrider) -
AbstractSubscription
-
-
Method Details
-
getProviderId
Description copied from interface:ProviderRetrieves the unique identifier for this provider.- Specified by:
getProviderIdin interfaceProvider- Returns:
- A String representing the provider's ID such as aws.
-
receive
Receives and returns the next message from the Subscription's queue.This method provides a simple, blocking interface for consuming messages from a pub/sub subscription. It automatically handles: - Background prefetching to maintain a healthy message queue - Flow control to prevent CPU spinning when no messages are available - Error handling and retry logic for transient failures - Thread-safe concurrent access from multiple threads
When messages are available, this method returns immediately. When the queue is empty, it will wait for new messages to arrive from the cloud provider, with automatic prefetching happening in the background to keep the queue populated.
This method can be called concurrently from multiple threads safely.
- Returns:
- the next available message from the subscription
- Throws:
SubstrateSdkException- if the subscription has been shut down, is in a permanent error state, or if interrupted while waiting for messages
-
doReceiveBatch
Provider specific implementation must fetch at most batchSize messages from the remote service. -
createReceiveBatcherOptions
Creates batcher options for this subscription. Provides defaults that cloud providers can override to provide provider-specific batching configuration that aligns with their service limits and performance characteristics. -
sendAck
Sends acknowledgment for a single message.This method enqueues the acknowledgment for batch processing and returns immediately. The actual acknowledgment is sent asynchronously in batches for efficiency.
- Parameters:
ackID- the acknowledgment identifier
-
sendAcks
Sends acknowledgments for multiple messages.The returned Future completes immediately upon enqueueing the acknowledgments for batch processing, without waiting for the underlying RPC to complete.
- Parameters:
ackIDs- the list of acknowledgment identifiers- Returns:
- a CompletableFuture that completes when acknowledgments are enqueued
-
sendNack
Sends negative acknowledgment for a single message.- Parameters:
ackID- the acknowledgment ID to negatively acknowledge- Throws:
InvalidArgumentException- if ackID is nullSubstrateSdkException- if the subscription is in an error state or has been shut down
-
sendNacks
Sends negative acknowledgment for multiple messages.- Parameters:
ackIDs- the list of acknowledgment IDs to negatively acknowledge- Returns:
- a CompletableFuture that completes when the nack is queued
- Throws:
InvalidArgumentException- if ackIDs is null or contains null elementsSubstrateSdkException- if the subscription is in an error state or has been shut down
-
canNack
public abstract boolean canNack() -
isRetryable
-
getAttributes
-
doSendAcks
-
doSendNacks
-
createAckBatcherOptions
-
close
Implements AutoCloseable interface for try-with-resources support.Shuts down the subscription and releases all resources.
This method should: - Flush any pending acknowledgments or nacks - Close network connections - Release any other provider-specific resources
After calling this method, the subscription should not accept new operations. It is safe to call this method multiple times.
- Specified by:
closein interfaceAutoCloseable- Throws:
Exception
-
getException
Description copied from interface:ProviderMaps a given Throwable from the provider implementation to a specific SubstrateSdkException. This is used for exception handling abstraction.- Specified by:
getExceptionin interfaceProvider- Parameters:
t- The Throwable to be mapped.- Returns:
- The Class of the corresponding SubstrateSdkException.
-