Class AbstractSubscription<T extends AbstractSubscription<T>>
- java.lang.Object
-
- com.salesforce.multicloudj.pubsub.driver.AbstractSubscription<T>
-
- All Implemented Interfaces:
AutoCloseable
- Direct Known Subclasses:
GcpSubscription
public abstract class AbstractSubscription<T extends AbstractSubscription<T>> extends Object implements AutoCloseable
Abstract base class for subscription implementations. Implementations should handle proper resource cleanup in the close method, including flushing pending acknowledgments, closing connections, and stopping background threads.
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description static classAbstractSubscription.Builder<T extends AbstractSubscription<T>>
-
Field Summary
Fields Modifier and Type Field Description protected CredentialsOverridercredentialsOverriderprotected URIendpointprotected AtomicBooleanisShutdownFlag indicating whether the subscription has been shut down.protected AtomicReference<Throwable>permanentErrorprotected StringproviderIdprotected URIproxyEndpointprotected longreceiveTimeoutSecondsprotected Stringregionprotected StringsubscriptionNameprotected AtomicReference<Throwable>unreportedAckErr
-
Constructor Summary
Constructors Modifier Constructor Description protectedAbstractSubscription(AbstractSubscription.Builder<T> builder)protectedAbstractSubscription(String providerId, String subscriptionName, String region, CredentialsOverrider credentialsOverrider)
-
Method Summary
All Methods Instance Methods Abstract Methods Concrete Methods Modifier and Type Method Description abstract booleancanNack()voidclose()Implements AutoCloseable interface for try-with-resources support.protected abstract Batcher.OptionscreateAckBatcherOptions()protected Batcher.OptionscreateReceiveBatcherOptions()Creates batcher options for this subscription.protected abstract List<Message>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 Map<String,String>getAttributes()abstract Class<? extends SubstrateSdkException>getException(Throwable t)StringgetProviderId()abstract booleanisRetryable(Throwable error)Messagereceive()Receives and returns the next message from the Subscription's queue.voidsendAck(AckID ackID)Sends acknowledgment for a single message.CompletableFuture<Void>sendAcks(List<AckID> ackIDs)Sends acknowledgments for multiple messages.voidsendNack(AckID ackID)Sends negative acknowledgment for a single message.CompletableFuture<Void>sendNacks(List<AckID> ackIDs)Sends negative acknowledgment for multiple messages.protected voidvalidateAckIDType(AckID ackID)
-
-
-
Field Detail
-
providerId
protected final String providerId
-
subscriptionName
protected final String subscriptionName
-
region
protected final String region
-
endpoint
protected final URI endpoint
-
proxyEndpoint
protected final URI proxyEndpoint
-
receiveTimeoutSeconds
protected final long receiveTimeoutSeconds
-
credentialsOverrider
protected final CredentialsOverrider credentialsOverrider
-
isShutdown
protected final AtomicBoolean isShutdown
Flag indicating whether the subscription has been shut down.
-
permanentError
protected final AtomicReference<Throwable> permanentError
-
unreportedAckErr
protected final AtomicReference<Throwable> unreportedAckErr
-
-
Constructor Detail
-
AbstractSubscription
protected AbstractSubscription(String providerId, String subscriptionName, String region, CredentialsOverrider credentialsOverrider)
-
AbstractSubscription
protected AbstractSubscription(AbstractSubscription.Builder<T> builder)
-
-
Method Detail
-
getProviderId
public String getProviderId()
-
receive
public Message 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 threadsWhen 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
protected abstract List<Message> doReceiveBatch(int batchSize)
Provider specific implementation must fetch at most batchSize messages from the remote service.
-
createReceiveBatcherOptions
protected Batcher.Options 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
public void sendAck(AckID ackID)
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
public CompletableFuture<Void> sendAcks(List<AckID> ackIDs)
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
public void sendNack(AckID ackID)
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
public CompletableFuture<Void> sendNacks(List<AckID> ackIDs)
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
public abstract boolean isRetryable(Throwable error)
-
validateAckIDType
protected void validateAckIDType(AckID ackID)
-
createAckBatcherOptions
protected abstract Batcher.Options createAckBatcherOptions()
-
close
public void close() throws ExceptionImplements 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
public abstract Class<? extends SubstrateSdkException> getException(Throwable t)
-
-