public class PubSubSubscriberTemplate extends Object implements PubSubSubscriberOperations, org.springframework.beans.factory.DisposableBean
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.
Constructor and Description |
---|
PubSubSubscriberTemplate(SubscriberFactory subscriberFactory)
Default
PubSubSubscriberTemplate constructor. |
Modifier and Type | Method and Description |
---|---|
org.springframework.util.concurrent.ListenableFuture<Void> |
ack(Collection<? extends AcknowledgeablePubsubMessage> acknowledgeablePubsubMessages)
Acknowledge messages in per-subscription batches.
|
void |
destroy()
Destroys the default executor, regardless of whether it was used.
|
PubSubMessageConverter |
getMessageConverter()
Get the converter used to convert a message payload to the desired type.
|
SubscriberFactory |
getSubscriberFactory() |
org.springframework.util.concurrent.ListenableFuture<Void> |
modifyAckDeadline(Collection<? extends AcknowledgeablePubsubMessage> acknowledgeablePubsubMessages,
int ackDeadlineSeconds)
Modify multiple messages' ack deadline in per-subscription batches.
|
org.springframework.util.concurrent.ListenableFuture<Void> |
nack(Collection<? extends AcknowledgeablePubsubMessage> acknowledgeablePubsubMessages)
Nack messages in per-subscription batches.
|
List<AcknowledgeablePubsubMessage> |
pull(String subscription,
Integer maxMessages,
Boolean returnImmediately)
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.
|
org.springframework.util.concurrent.ListenableFuture<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.
|
<T> List<ConvertedAcknowledgeablePubsubMessage<T>> |
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.
|
<T> org.springframework.util.concurrent.ListenableFuture<List<ConvertedAcknowledgeablePubsubMessage<T>>> |
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.
|
org.springframework.util.concurrent.ListenableFuture<List<AcknowledgeablePubsubMessage>> |
pullAsync(String subscription,
Integer maxMessages,
Boolean returnImmediately)
Asynchronously pull a number of messages from a Google Cloud Pub/Sub subscription.
|
com.google.pubsub.v1.PubsubMessage |
pullNext(String subscription)
Pull and auto-acknowledge a message from a Google Cloud Pub/Sub subscription.
|
org.springframework.util.concurrent.ListenableFuture<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 the
Executor to control per-subscription batch
parallelization in acknowledgement and deadline operations. |
void |
setAsyncPullExecutor(Executor asyncPullExecutor)
Set a custom
Executor 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.
|
Subscriber |
subscribe(String subscription,
Consumer<BasicAcknowledgeablePubsubMessage> messageConsumer)
Add a callback method to an existing subscription.
|
<T> 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.
|
public PubSubSubscriberTemplate(SubscriberFactory subscriberFactory)
PubSubSubscriberTemplate
constructor.subscriberFactory
- the Subscriber
factory
to subscribe to subscriptions or pull messages.public PubSubMessageConverter getMessageConverter()
public void setMessageConverter(PubSubMessageConverter pubSubMessageConverter)
pubSubMessageConverter
- the converter to setpublic void setAckExecutor(Executor ackExecutor)
Executor
to control per-subscription batch
parallelization in acknowledgement and deadline operations.ackExecutor
- the executor to setpublic void setAsyncPullExecutor(Executor asyncPullExecutor)
Executor
to control the threads that process
the responses of the asynchronous pull callback operations.asyncPullExecutor
- the executor to setpublic Subscriber subscribe(String subscription, Consumer<BasicAcknowledgeablePubsubMessage> messageConsumer)
PubSubSubscriberOperations
The created Subscriber
is returned so it can be stopped.
subscribe
in interface PubSubSubscriberOperations
subscription
- canonical subscription name, e.g., "subscriptionName", or the fully-qualified
subscription name in the projects/<project_name>/subscriptions/<subscription_name>
formatmessageConsumer
- the callback method triggered when new messages arrivepublic <T> Subscriber subscribeAndConvert(String subscription, Consumer<ConvertedBasicAcknowledgeablePubsubMessage<T>> messageConsumer, Class<T> payloadType)
PubSubSubscriberOperations
The created Subscriber
is returned so it can be stopped.
subscribeAndConvert
in interface PubSubSubscriberOperations
T
- the type of the payloadsubscription
- canonical subscription name, e.g., "subscriptionName", or the fully-qualified
subscription name in the projects/<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 convertedpublic List<AcknowledgeablePubsubMessage> pull(String subscription, Integer maxMessages, Boolean returnImmediately)
PubSubSubscriberOperations
pull
in interface PubSubSubscriberOperations
subscription
- canonical subscription name, e.g., "subscriptionName", or the fully-qualified
subscription name in the projects/<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 satisfy maxMessages
.
Setting this parameter to true
is not recommended as it may result in long delays in message delivery.public org.springframework.util.concurrent.ListenableFuture<List<AcknowledgeablePubsubMessage>> pullAsync(String subscription, Integer maxMessages, Boolean returnImmediately)
PubSubSubscriberOperations
pullAsync
in interface PubSubSubscriberOperations
subscription
- canonical subscription name, e.g., "subscriptionName", or the fully-qualified
subscription name in the projects/<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 satisfy maxMessages
.
Setting this parameter to true
is not recommended as it may result in long delays in message delivery.public <T> List<ConvertedAcknowledgeablePubsubMessage<T>> pullAndConvert(String subscription, Integer maxMessages, Boolean returnImmediately, Class<T> payloadType)
PubSubSubscriberOperations
pullAndConvert
in interface PubSubSubscriberOperations
T
- the type of the payloadsubscription
- canonical subscription name, e.g., "subscriptionName", or the fully-qualified
subscription name in the projects/<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 satisfy maxMessages
.
Setting this parameter to true
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 convertedpublic <T> org.springframework.util.concurrent.ListenableFuture<List<ConvertedAcknowledgeablePubsubMessage<T>>> pullAndConvertAsync(String subscription, Integer maxMessages, Boolean returnImmediately, Class<T> payloadType)
PubSubSubscriberOperations
pullAndConvertAsync
in interface PubSubSubscriberOperations
T
- the type of the payloadsubscription
- canonical subscription name, e.g., "subscriptionName", or the fully-qualified
subscription name in the projects/<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 satisfy maxMessages
.
Setting this parameter to true
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 convertedpublic List<com.google.pubsub.v1.PubsubMessage> pullAndAck(String subscription, Integer maxMessages, Boolean returnImmediately)
PubSubSubscriberOperations
pullAndAck
in interface PubSubSubscriberOperations
subscription
- canonical subscription name, e.g., "subscriptionName", or the fully-qualified
subscription name in the projects/<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 satisfy maxMessages
.
Setting this parameter to true
is not recommended as it may result in long delays in message delivery.public org.springframework.util.concurrent.ListenableFuture<List<com.google.pubsub.v1.PubsubMessage>> pullAndAckAsync(String subscription, Integer maxMessages, Boolean returnImmediately)
PubSubSubscriberOperations
pullAndAckAsync
in interface PubSubSubscriberOperations
subscription
- canonical subscription name, e.g., "subscriptionName", or the fully-qualified
subscription name in the projects/<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 satisfy maxMessages
.
Setting this parameter to true
is not recommended as it may result in long delays in message delivery.public com.google.pubsub.v1.PubsubMessage pullNext(String subscription)
PubSubSubscriberOperations
pullNext
in interface PubSubSubscriberOperations
subscription
- canonical subscription name, e.g., "subscriptionName", or the fully-qualified
subscription name in the projects/<project_name>/subscriptions/<subscription_name>
formatnull
if none exists in the subscriptionpublic org.springframework.util.concurrent.ListenableFuture<com.google.pubsub.v1.PubsubMessage> pullNextAsync(String subscription)
PubSubSubscriberOperations
pullNextAsync
in interface PubSubSubscriberOperations
subscription
- canonical subscription name, e.g., "subscriptionName", or the fully-qualified
subscription name in the projects/<project_name>/subscriptions/<subscription_name>
formatnull
if none exists in the subscriptionpublic SubscriberFactory getSubscriberFactory()
public org.springframework.util.concurrent.ListenableFuture<Void> ack(Collection<? extends AcknowledgeablePubsubMessage> acknowledgeablePubsubMessages)
ack
in interface PubSubSubscriberOperations
acknowledgeablePubsubMessages
- messages, potentially from different subscriptions.ListenableFuture
indicating overall success or failure.public org.springframework.util.concurrent.ListenableFuture<Void> nack(Collection<? extends AcknowledgeablePubsubMessage> acknowledgeablePubsubMessages)
nack
in interface PubSubSubscriberOperations
acknowledgeablePubsubMessages
- messages, potentially from different subscriptions.ListenableFuture
indicating overall success or failure.public org.springframework.util.concurrent.ListenableFuture<Void> modifyAckDeadline(Collection<? extends AcknowledgeablePubsubMessage> acknowledgeablePubsubMessages, int ackDeadlineSeconds)
modifyAckDeadline
in interface PubSubSubscriberOperations
acknowledgeablePubsubMessages
- messages, potentially from different subscriptions.ackDeadlineSeconds
- the new ack deadline in seconds. A deadline of 0 effectively nacks the messages.ListenableFuture
indicating overall success or failure.public void destroy()
destroy
in interface org.springframework.beans.factory.DisposableBean
Copyright © 2021. All rights reserved.