Uh oh!
There was an error while loading. Please reload this page.
This repository was archived by the owner on Mar 9, 2026. It is now read-only.
- Notifications
You must be signed in to change notification settings - Fork 215
samples: sample for receiving messages with exactly-once delivery enabled#588
Merged
Uh oh!
There was an error while loading. Please reload this page.
Merged
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
2ab6383
Add receive_messages_with_exactly_once_subscribe sample with its own …
pradn 40bcfa2
Address Tianzi and Mahesh's comments.
pradn 444bf11
Add code for arg parsing / integrate sample with infra
pradn 4d6911d
Add sample test
pradn c032332
Reformat and remove min lease extension period setting from sample
pradn c1350ec
Address Tianzi's comments.
pradn babe234
Merge branch 'main' into exactly_once_sample
pradn 03c3e4b
Fix import of subscriber exceptions.
pradn File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Uh oh!
There was an error while loading. Please reload this page.
Jump to
Jump to file
Failed to load files.
Loading
Uh oh!
There was an error while loading. Please reload this page.
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -580,6 +580,61 @@ def callback(message: pubsub_v1.subscriber.message.Message) -> None: | ||
| # [END pubsub_subscriber_blocking_shutdown] | ||
| def receive_messages_with_exactly_once_delivery_enabled( | ||
| project_id: str, subscription_id: str, timeout: Optional[float] = None | ||
| ) -> None: | ||
| """Receives messages from a pull subscription with exactly-once delivery enabled.""" | ||
| # [START pubsub_subscriber_exactly_once] | ||
| from concurrent.futures import TimeoutError | ||
| from google.cloud import pubsub_v1 | ||
| from google.cloud.pubsub_v1.subscriber import exceptions as sub_exceptions | ||
| # TODO(developer) | ||
| # project_id = "your-project-id" | ||
| # subscription_id = "your-subscription-id" | ||
| # Number of seconds the subscriber should listen for messages | ||
| # timeout = 5.0 | ||
| subscriber = pubsub_v1.SubscriberClient() | ||
| # The `subscription_path` method creates a fully qualified identifier | ||
| # in the form `projects/{project_id}/subscriptions/{subscription_id}` | ||
| subscription_path = subscriber.subscription_path(project_id, subscription_id) | ||
| def callback(message: pubsub_v1.subscriber.message.Message) -> None: | ||
| print(f"Received {message}.") | ||
| # Use `ack_with_response()` instead of `ack()` to get a future that tracks | ||
| # the result of the acknowledge call. When exactly-once delivery is enabled | ||
| # on the subscription, the message is guaranteed to not be delivered again | ||
| # if the ack future succeeds. | ||
| ack_future = message.ack_with_response() | ||
pradn marked this conversation as resolved.
Uh oh!There was an error while loading. Please reload this page. | ||
| try: | ||
| # Block on result of acknowledge call. | ||
| # When `timeout` is not set, result() will block indefinitely, | ||
| # unless an exception is encountered first. | ||
| ack_future.result(timeout=timeout) | ||
| print(f"Ack for message {message.message_id} successful.") | ||
| except sub_exceptions.AcknowledgeError as e: | ||
| print( | ||
| f"Ack for message {message.message_id} failed with error: {e.error_code}" | ||
| ) | ||
| streaming_pull_future = subscriber.subscribe(subscription_path, callback=callback) | ||
| print(f"Listening for messages on {subscription_path}..\n") | ||
| # Wrap subscriber in a 'with' block to automatically call close() when done. | ||
| with subscriber: | ||
| try: | ||
| # When `timeout` is not set, result() will block indefinitely, | ||
| # unless an exception is encountered first. | ||
| streaming_pull_future.result(timeout=timeout) | ||
| except TimeoutError: | ||
| streaming_pull_future.cancel() # Trigger the shutdown. | ||
| streaming_pull_future.result() # Block until the shutdown is complete. | ||
| # [END pubsub_subscriber_exactly_once] | ||
| def synchronous_pull(project_id: str, subscription_id: str) -> None: | ||
| """Pulling messages synchronously.""" | ||
| # [START pubsub_subscriber_sync_pull] | ||
| @@ -881,6 +936,17 @@ def callback(message: pubsub_v1.subscriber.message.Message) -> None: | ||
| "timeout", default=None, type=float, nargs="?" | ||
| ) | ||
| receive_messages_with_exactly_once_delivery_enabled_parser = subparsers.add_parser( | ||
| "receive-messages-with-exactly-once-delivery-enabled", | ||
| help=receive_messages_with_exactly_once_delivery_enabled.__doc__, | ||
| ) | ||
| receive_messages_with_exactly_once_delivery_enabled_parser.add_argument( | ||
| "subscription_id" | ||
| ) | ||
| receive_messages_with_exactly_once_delivery_enabled_parser.add_argument( | ||
| "timeout", default=None, type=float, nargs="?" | ||
| ) | ||
| synchronous_pull_parser = subparsers.add_parser( | ||
| "receive-synchronously", help=synchronous_pull.__doc__ | ||
| ) | ||
| @@ -967,6 +1033,10 @@ def callback(message: pubsub_v1.subscriber.message.Message) -> None: | ||
| receive_messages_with_blocking_shutdown( | ||
| args.project_id, args.subscription_id, args.timeout | ||
| ) | ||
| elif args.command == "receive-messages-with-exactly-once-delivery-enabled": | ||
| receive_messages_with_exactly_once_delivery_enabled( | ||
| args.project_id, args.subscription_id, args.timeout | ||
| ) | ||
| elif args.command == "receive-synchronously": | ||
| synchronous_pull(args.project_id, args.subscription_id) | ||
| elif args.command == "receive-synchronously-with-lease": | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.