Class PubSubTemplate
java.lang.Object
com.google.cloud.spring.pubsub.core.PubSubTemplate
- All Implemented Interfaces:
PubSubPublisherOperations,PubSubOperations,PubSubSubscriberOperations
Default implementation of
PubSubOperations.
The main Google Cloud Pub/Sub integration component for publishing to topics and consuming messages from subscriptions asynchronously or by pulling.
-
Constructor Summary
ConstructorsConstructorDescriptionPubSubTemplate(PubSubPublisherTemplate pubSubPublisherTemplate, PubSubSubscriberTemplate pubSubSubscriberTemplate) DefaultPubSubTemplateconstructor that uses aPubSubPublisherTemplateand aPubSubSubscriberTemplate.PubSubTemplate(PublisherFactory publisherFactory, SubscriberFactory subscriberFactory) DefaultPubSubTemplateconstructor. -
Method Summary
Modifier and TypeMethodDescriptionack(Collection<? extends AcknowledgeablePubsubMessage> acknowledgeablePubsubMessages) Acknowledge a batch of messages.modifyAckDeadline(Collection<? extends AcknowledgeablePubsubMessage> acknowledgeablePubsubMessages, int ackDeadlineSeconds) Modify the ack deadline of a batch of messages.nack(Collection<? extends AcknowledgeablePubsubMessage> acknowledgeablePubsubMessages) Negatively acknowledge a batch of messages.Send a message to Pub/Sub.<T> CompletableFuture<String>Send a message to Pub/Sub.<T> CompletableFuture<String>Uses the configured message converter to first convert the payload and headers to aPubsubMessageand then publish it.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.PubsubMessagePull 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.voidsetMessageConverter(PubSubMessageConverter messageConverter) com.google.cloud.pubsub.v1.Subscribersubscribe(String subscription, Consumer<BasicAcknowledgeablePubsubMessage> messageConsumer) Add a callback method to an existing subscription.<T> com.google.cloud.pubsub.v1.SubscribersubscribeAndConvert(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
-
PubSubTemplate
DefaultPubSubTemplateconstructor.- Parameters:
publisherFactory- thePublisherfactory to publish to topics.subscriberFactory- theSubscriberfactory to subscribe to subscriptions.
-
PubSubTemplate
public PubSubTemplate(PubSubPublisherTemplate pubSubPublisherTemplate, PubSubSubscriberTemplate pubSubSubscriberTemplate) DefaultPubSubTemplateconstructor that uses aPubSubPublisherTemplateand aPubSubSubscriberTemplate.- Parameters:
pubSubPublisherTemplate- thePubSubPublisherTemplateto publish to topics.pubSubSubscriberTemplate- thePubSubSubscriberTemplateto subscribe to subscriptions or pull messages- Since:
- 1.1
-
-
Method Details
-
getPubSubPublisherTemplate
-
getPubSubSubscriberTemplate
-
getMessageConverter
-
setMessageConverter
-
publish
Uses the configured message converter to first convert the payload and headers to aPubsubMessageand then publish it.- Specified by:
publishin interfacePubSubPublisherOperations- Type Parameters:
T- the type of the payload to publish- Parameters:
topic- short topic name, e.g., "topicName", or the fully-qualified topic name in theprojects/[project_name]/topics/[topic_name]formatpayload- an object that will be serialized and sentheaders- the headers to publish- Returns:
- the listenable future of the call
-
publish
Description copied from interface:PubSubPublisherOperationsSend a message to Pub/Sub.- Specified by:
publishin interfacePubSubPublisherOperations- Type Parameters:
T- the type of the payload to publish- Parameters:
topic- short topic name, e.g., "topicName", or the fully-qualified topic name in theprojects/[project_name]/topics/[topic_name]formatpayload- an object that will be serialized and sent- Returns:
- the listenable future of the call
-
publish
public CompletableFuture<String> publish(String topic, com.google.pubsub.v1.PubsubMessage pubsubMessage) Description copied from interface:PubSubPublisherOperationsSend a message to Pub/Sub.- Specified by:
publishin interfacePubSubPublisherOperations- Parameters:
topic- short topic name, e.g., "topicName", or the fully-qualified topic name in theprojects/[project_name]/topics/[topic_name]formatpubsubMessage- a Google Cloud Pub/Sub API message- Returns:
- the listenable future of the call
-
subscribe
public com.google.cloud.pubsub.v1.Subscriber subscribe(String subscription, Consumer<BasicAcknowledgeablePubsubMessage> messageConsumer) Description copied from interface:PubSubSubscriberOperationsAdd a callback method to an existing subscription.The created
Subscriberis returned so it can be stopped.- Specified by:
subscribein 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:PubSubSubscriberOperationsAdd a callback method to an existing subscription that receives Pub/Sub messages converted to the requested payload type.The created
Subscriberis returned so it can be stopped.- Specified by:
subscribeAndConvertin 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:PubSubSubscriberOperationsPull a number of messages from a Google Cloud Pub/Sub subscription.- Specified by:
pullin 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 totrueis 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:PubSubSubscriberOperationsAsynchronously pull a number of messages from a Google Cloud Pub/Sub subscription.- Specified by:
pullAsyncin 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 totrueis 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:PubSubSubscriberOperationsPull a number of messages from a Google Cloud Pub/Sub subscription and convert them to Spring messages with the desired payload type.- Specified by:
pullAndConvertin 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 totrueis 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:PubSubSubscriberOperationsAsynchronously 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:
pullAndConvertAsyncin 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 totrueis 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:PubSubSubscriberOperationsPull and auto-acknowledge a number of messages from a Google Cloud Pub/Sub subscription.- Specified by:
pullAndAckin 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 totrueis 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:PubSubSubscriberOperationsAsynchronously pull and auto-acknowledge a number of messages from a Google Cloud Pub/Sub subscription.- Specified by:
pullAndAckAsyncin 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 totrueis 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:PubSubSubscriberOperationsPull and auto-acknowledge a message from a Google Cloud Pub/Sub subscription.- Specified by:
pullNextin 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
nullif none exists in the subscription
-
pullNextAsync
Description copied from interface:PubSubSubscriberOperationsAsynchronously pull and auto-acknowledge a message from a Google Cloud Pub/Sub subscription.- Specified by:
pullNextAsyncin 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
nullif none exists in the subscription
-
ack
public CompletableFuture<Void> ack(Collection<? extends AcknowledgeablePubsubMessage> acknowledgeablePubsubMessages) Description copied from interface:PubSubSubscriberOperationsAcknowledge a batch of messages. The messages must have the same project id.- Specified by:
ackin interfacePubSubSubscriberOperations- Parameters:
acknowledgeablePubsubMessages- messages to be acknowledged- Returns:
CompletableFuture<Void>the CompletableFuture for the asynchronous execution
-
nack
public CompletableFuture<Void> nack(Collection<? extends AcknowledgeablePubsubMessage> acknowledgeablePubsubMessages) Description copied from interface:PubSubSubscriberOperationsNegatively acknowledge a batch of messages. The messages must have the same project id.- Specified by:
nackin interfacePubSubSubscriberOperations- Parameters:
acknowledgeablePubsubMessages- messages to be negatively acknowledged- Returns:
CompletableFuture<Void>the CompletableFuture for the asynchronous execution
-
modifyAckDeadline
public CompletableFuture<Void> modifyAckDeadline(Collection<? extends AcknowledgeablePubsubMessage> acknowledgeablePubsubMessages, int ackDeadlineSeconds) Description copied from interface:PubSubSubscriberOperationsModify the ack deadline of a batch of messages. The messages must have the same project id.- Specified by:
modifyAckDeadlinein interfacePubSubSubscriberOperations- Parameters:
acknowledgeablePubsubMessages- messages to be modifiedackDeadlineSeconds- the new ack deadline in seconds. A deadline of 0 effectively nacks the messages.- Returns:
CompletableFuture<Void>the CompletableFuture for the asynchronous execution
-
getPublisherFactory
-
getSubscriberFactory
-