Class PubSubSubscriberTemplate
java.lang.Object
com.google.cloud.spring.pubsub.core.subscriber.PubSubSubscriberTemplate
- All Implemented Interfaces:
PubSubSubscriberOperations
,org.springframework.beans.factory.DisposableBean
public class PubSubSubscriberTemplate
extends Object
implements PubSubSubscriberOperations, org.springframework.beans.factory.DisposableBean
Default implementation of
PubSubSubscriberOperations
.
The main Google Cloud Pub/Sub integration component for consuming messages from subscriptions asynchronously or by pulling.
A custom Executor
can be injected to control per-subscription batch parallelization in
acknowledgement and deadline operations. By default, this is a single thread executor, created
per instance of the PubSubSubscriberTemplate
.
A custom Executor
can be injected to control the threads that process the responses of
the asynchronous pull callback operations. By default, this is executed on the same thread that
executes the callback.
- Since:
- 1.1
-
Constructor Summary
ConstructorDescriptionPubSubSubscriberTemplate
(SubscriberFactory subscriberFactory) DefaultPubSubSubscriberTemplate
constructor. -
Method Summary
Modifier and TypeMethodDescriptionack
(Collection<? extends AcknowledgeablePubsubMessage> acknowledgeablePubsubMessages) Acknowledge messages in per-subscription batches.void
destroy()
Destroys the default executor, regardless of whether it was used.Get the converter used to convert a message payload to the desired type.modifyAckDeadline
(Collection<? extends AcknowledgeablePubsubMessage> acknowledgeablePubsubMessages, int ackDeadlineSeconds) Modify multiple messages' ack deadline in per-subscription batches.nack
(Collection<? extends AcknowledgeablePubsubMessage> acknowledgeablePubsubMessages) Nack messages in per-subscription batches.Pull a number of messages from a Google Cloud Pub/Sub subscription.List<com.google.pubsub.v1.PubsubMessage>
pullAndAck
(String subscription, Integer maxMessages, Boolean returnImmediately) Pull and auto-acknowledge a number of messages from a Google Cloud Pub/Sub subscription.CompletableFuture<List<com.google.pubsub.v1.PubsubMessage>>
pullAndAckAsync
(String subscription, Integer maxMessages, Boolean returnImmediately) Asynchronously pull and auto-acknowledge a number of messages from a Google Cloud Pub/Sub subscription.pullAndConvert
(String subscription, Integer maxMessages, Boolean returnImmediately, Class<T> payloadType) Pull a number of messages from a Google Cloud Pub/Sub subscription and convert them to Spring messages with the desired payload type.pullAndConvertAsync
(String subscription, Integer maxMessages, Boolean returnImmediately, Class<T> payloadType) Asynchronously pull a number of messages from a Google Cloud Pub/Sub subscription and convert them to Spring messages with the desired payload type.Asynchronously pull a number of messages from a Google Cloud Pub/Sub subscription.com.google.pubsub.v1.PubsubMessage
Pull and auto-acknowledge a message from a Google Cloud Pub/Sub subscription.CompletableFuture<com.google.pubsub.v1.PubsubMessage>
pullNextAsync
(String subscription) Asynchronously pull and auto-acknowledge a message from a Google Cloud Pub/Sub subscription.void
setAckExecutor
(Executor ackExecutor) Sets theExecutor
to control per-subscription batch parallelization in acknowledgement and deadline operations.void
setAsyncPullExecutor
(Executor asyncPullExecutor) Set a customExecutor
to control the threads that process the responses of the asynchronous pull callback operations.void
setMessageConverter
(PubSubMessageConverter pubSubMessageConverter) Set the converter used to convert a message payload to the desired type.com.google.cloud.pubsub.v1.Subscriber
subscribe
(String subscription, Consumer<BasicAcknowledgeablePubsubMessage> messageConsumer) Add a callback method to an existing subscription.<T> com.google.cloud.pubsub.v1.Subscriber
subscribeAndConvert
(String subscription, Consumer<ConvertedBasicAcknowledgeablePubsubMessage<T>> messageConsumer, Class<T> payloadType) Add a callback method to an existing subscription that receives Pub/Sub messages converted to the requested payload type.
-
Constructor Details
-
PubSubSubscriberTemplate
DefaultPubSubSubscriberTemplate
constructor.- Parameters:
subscriberFactory
- theSubscriber
factory to subscribe to subscriptions or pull messages.
-
-
Method Details
-
getMessageConverter
Get the converter used to convert a message payload to the desired type.- Returns:
- the currently used converter
-
setMessageConverter
Set the converter used to convert a message payload to the desired type.- Parameters:
pubSubMessageConverter
- the converter to set
-
setAckExecutor
Sets theExecutor
to control per-subscription batch parallelization in acknowledgement and deadline operations.- Parameters:
ackExecutor
- the executor to set
-
setAsyncPullExecutor
Set a customExecutor
to control the threads that process the responses of the asynchronous pull callback operations.- Parameters:
asyncPullExecutor
- the executor to set
-
subscribe
public com.google.cloud.pubsub.v1.Subscriber subscribe(String subscription, Consumer<BasicAcknowledgeablePubsubMessage> messageConsumer) Description copied from interface:PubSubSubscriberOperations
Add a callback method to an existing subscription.The created
Subscriber
is returned so it can be stopped.- Specified by:
subscribe
in interfacePubSubSubscriberOperations
- Parameters:
subscription
- short subscription name, e.g., "subscriptionName", or the fully-qualified subscription name in theprojects/[project_name]/subscriptions/[subscription_name]
formatmessageConsumer
- the callback method triggered when new messages arrive- Returns:
- subscriber listening to new messages
-
subscribeAndConvert
public <T> com.google.cloud.pubsub.v1.Subscriber subscribeAndConvert(String subscription, Consumer<ConvertedBasicAcknowledgeablePubsubMessage<T>> messageConsumer, Class<T> payloadType) Description copied from interface:PubSubSubscriberOperations
Add a callback method to an existing subscription that receives Pub/Sub messages converted to the requested payload type.The created
Subscriber
is returned so it can be stopped.- Specified by:
subscribeAndConvert
in interfacePubSubSubscriberOperations
- Type Parameters:
T
- the type of the payload- Parameters:
subscription
- short subscription name, e.g., "subscriptionName", or the fully-qualified subscription name in theprojects/[project_name]/subscriptions/[subscription_name]
formatmessageConsumer
- the callback method triggered when new messages arrivepayloadType
- the type to which the payload of the Pub/Sub message should be converted- Returns:
- subscriber listening to new messages
-
pull
public List<AcknowledgeablePubsubMessage> pull(String subscription, Integer maxMessages, Boolean returnImmediately) Description copied from interface:PubSubSubscriberOperations
Pull a number of messages from a Google Cloud Pub/Sub subscription.- Specified by:
pull
in interfacePubSubSubscriberOperations
- Parameters:
subscription
- short subscription name, e.g., "subscriptionName", or the fully-qualified subscription name in theprojects/[project_name]/subscriptions/[subscription_name]
formatmaxMessages
- the maximum number of pulled messages. If this value is null then up to Integer.MAX_VALUE messages will be requested.returnImmediately
- returns immediately even if subscription doesn't contain enough messages to satisfymaxMessages
. Setting this parameter totrue
is not recommended as it may result in long delays in message delivery.- Returns:
- the list of received acknowledgeable messages
-
pullAsync
public CompletableFuture<List<AcknowledgeablePubsubMessage>> pullAsync(String subscription, Integer maxMessages, Boolean returnImmediately) Description copied from interface:PubSubSubscriberOperations
Asynchronously pull a number of messages from a Google Cloud Pub/Sub subscription.- Specified by:
pullAsync
in interfacePubSubSubscriberOperations
- Parameters:
subscription
- short subscription name, e.g., "subscriptionName", or the fully-qualified subscription name in theprojects/[project_name]/subscriptions/[subscription_name]
formatmaxMessages
- the maximum number of pulled messages. If this value is null then up to Integer.MAX_VALUE messages will be requested.returnImmediately
- returns immediately even if subscription doesn't contain enough messages to satisfymaxMessages
. Setting this parameter totrue
is not recommended as it may result in long delays in message delivery.- Returns:
- the CompletableFuture for the asynchronous execution, returning the list of received acknowledgeable messages
-
pullAndConvert
public <T> List<ConvertedAcknowledgeablePubsubMessage<T>> pullAndConvert(String subscription, Integer maxMessages, Boolean returnImmediately, Class<T> payloadType) Description copied from interface:PubSubSubscriberOperations
Pull a number of messages from a Google Cloud Pub/Sub subscription and convert them to Spring messages with the desired payload type.- Specified by:
pullAndConvert
in interfacePubSubSubscriberOperations
- Type Parameters:
T
- the type of the payload- Parameters:
subscription
- short subscription name, e.g., "subscriptionName", or the fully-qualified subscription name in theprojects/[project_name]/subscriptions/[subscription_name]
formatmaxMessages
- the maximum number of pulled messages. If this value is null then up to Integer.MAX_VALUE messages will be requested.returnImmediately
- returns immediately even if subscription doesn't contain enough messages to satisfymaxMessages
. Setting this parameter totrue
is not recommended as it may result in long delays in message delivery.payloadType
- the type to which the payload of the Pub/Sub messages should be converted- Returns:
- the list of received acknowledgeable messages
-
pullAndConvertAsync
public <T> CompletableFuture<List<ConvertedAcknowledgeablePubsubMessage<T>>> pullAndConvertAsync(String subscription, Integer maxMessages, Boolean returnImmediately, Class<T> payloadType) Description copied from interface:PubSubSubscriberOperations
Asynchronously pull a number of messages from a Google Cloud Pub/Sub subscription and convert them to Spring messages with the desired payload type.- Specified by:
pullAndConvertAsync
in interfacePubSubSubscriberOperations
- Type Parameters:
T
- the type of the payload- Parameters:
subscription
- short subscription name, e.g., "subscriptionName", or the fully-qualified subscription name in theprojects/[project_name]/subscriptions/[subscription_name]
formatmaxMessages
- the maximum number of pulled messages. If this value is null then up to Integer.MAX_VALUE messages will be requested.returnImmediately
- returns immediately even if subscription doesn't contain enough messages to satisfymaxMessages
. Setting this parameter totrue
is not recommended as it may result in long delays in message delivery.payloadType
- the type to which the payload of the Pub/Sub messages should be converted- Returns:
- the CompletableFuture for the asynchronous execution, returning the list of received acknowledgeable messages
-
pullAndAck
public List<com.google.pubsub.v1.PubsubMessage> pullAndAck(String subscription, Integer maxMessages, Boolean returnImmediately) Description copied from interface:PubSubSubscriberOperations
Pull and auto-acknowledge a number of messages from a Google Cloud Pub/Sub subscription.- Specified by:
pullAndAck
in interfacePubSubSubscriberOperations
- Parameters:
subscription
- short subscription name, e.g., "subscriptionName", or the fully-qualified subscription name in theprojects/[project_name]/subscriptions/[subscription_name]
formatmaxMessages
- the maximum number of pulled messages. If this value is null then up to Integer.MAX_VALUE messages will be requested.returnImmediately
- returns immediately even if subscription doesn't contain enough messages to satisfymaxMessages
. Setting this parameter totrue
is not recommended as it may result in long delays in message delivery.- Returns:
- the list of received messages
-
pullAndAckAsync
public CompletableFuture<List<com.google.pubsub.v1.PubsubMessage>> pullAndAckAsync(String subscription, Integer maxMessages, Boolean returnImmediately) Description copied from interface:PubSubSubscriberOperations
Asynchronously pull and auto-acknowledge a number of messages from a Google Cloud Pub/Sub subscription.- Specified by:
pullAndAckAsync
in interfacePubSubSubscriberOperations
- Parameters:
subscription
- short subscription name, e.g., "subscriptionName", or the fully-qualified subscription name in theprojects/[project_name]/subscriptions/[subscription_name]
formatmaxMessages
- the maximum number of pulled messages. If this value is null then up to Integer.MAX_VALUE messages will be requested.returnImmediately
- returns immediately even if subscription doesn't contain enough messages to satisfymaxMessages
. Setting this parameter totrue
is not recommended as it may result in long delays in message delivery.- Returns:
- the CompletableFuture for the asynchronous execution, returning the list of received acknowledgeable messages
-
pullNext
Description copied from interface:PubSubSubscriberOperations
Pull and auto-acknowledge a message from a Google Cloud Pub/Sub subscription.- Specified by:
pullNext
in interfacePubSubSubscriberOperations
- Parameters:
subscription
- short subscription name, e.g., "subscriptionName", or the fully-qualified subscription name in theprojects/[project_name]/subscriptions/[subscription_name]
format- Returns:
- a received message, or
null
if none exists in the subscription
-
pullNextAsync
Description copied from interface:PubSubSubscriberOperations
Asynchronously pull and auto-acknowledge a message from a Google Cloud Pub/Sub subscription.- Specified by:
pullNextAsync
in interfacePubSubSubscriberOperations
- Parameters:
subscription
- short subscription name, e.g., "subscriptionName", or the fully-qualified subscription name in theprojects/[project_name]/subscriptions/[subscription_name]
format- Returns:
- the CompletableFuture for the asynchronous execution, returning a received message, or
null
if none exists in the subscription
-
getSubscriberFactory
-
ack
public CompletableFuture<Void> ack(Collection<? extends AcknowledgeablePubsubMessage> acknowledgeablePubsubMessages) Acknowledge messages in per-subscription batches. If any batch fails, the returned Future is marked as failed. If multiple batches fail, the returned Future will contain whichever exception was detected first.- Specified by:
ack
in interfacePubSubSubscriberOperations
- Parameters:
acknowledgeablePubsubMessages
- messages, potentially from different subscriptions.- Returns:
CompletableFuture
indicating overall success or failure.
-
nack
public CompletableFuture<Void> nack(Collection<? extends AcknowledgeablePubsubMessage> acknowledgeablePubsubMessages) Nack messages in per-subscription batches. If any batch fails, the returned Future is marked as failed. If multiple batches fail, the returned Future will contain whichever exception was detected first.- Specified by:
nack
in interfacePubSubSubscriberOperations
- Parameters:
acknowledgeablePubsubMessages
- messages, potentially from different subscriptions.- Returns:
CompletableFuture
indicating overall success or failure.
-
modifyAckDeadline
public CompletableFuture<Void> modifyAckDeadline(Collection<? extends AcknowledgeablePubsubMessage> acknowledgeablePubsubMessages, int ackDeadlineSeconds) Modify multiple messages' ack deadline in per-subscription batches. If any batch fails, the returned Future is marked as failed. If multiple batches fail, the returned Future will contain whichever exception was detected first.- Specified by:
modifyAckDeadline
in interfacePubSubSubscriberOperations
- Parameters:
acknowledgeablePubsubMessages
- messages, potentially from different subscriptions.ackDeadlineSeconds
- the new ack deadline in seconds. A deadline of 0 effectively nacks the messages.- Returns:
CompletableFuture
indicating overall success or failure.
-
destroy
public void destroy()Destroys the default executor, regardless of whether it was used.- Specified by:
destroy
in interfaceorg.springframework.beans.factory.DisposableBean
-