|
| 1 | +// Copyright 2026 Google LLC |
| 2 | +// |
| 3 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +// you may not use this file except in compliance with the License. |
| 5 | +// You may obtain a copy of the License at |
| 6 | +// |
| 7 | +// https://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +// |
| 9 | +// Unless required by applicable law or agreed to in writing, software |
| 10 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +// See the License for the specific language governing permissions and |
| 13 | +// limitations under the License. |
| 14 | + |
| 15 | +import 'dart:async'; |
| 16 | +import 'package:google_cloud_pubsub/google_cloud_pubsub.dart'; |
| 17 | + |
| 18 | +Future<void> streamingPullExample(PubSub pubsub) async { |
| 19 | + // #docregion streaming_pull_example |
| 20 | + final subscription = pubsub.subscription('my-subscription'); |
| 21 | + |
| 22 | + // Establish a streaming pull with 3 parallel streams for high throughput |
| 23 | + // and custom retry settings for handling transient connection drops. |
| 24 | + final stream = subscription.streamingPull( |
| 25 | + maxConcurrentStreams: 3, |
| 26 | + streamAckDeadlineSeconds: 30, |
| 27 | + retry: const RetrySettings( |
| 28 | + maxRetries: 10, |
| 29 | + initialDelay: Duration(seconds: 1), |
| 30 | + maxDelay: Duration(seconds: 30), |
| 31 | + ), |
| 32 | + ); |
| 33 | + |
| 34 | + final listener = stream.listen( |
| 35 | + (ReceivedMessage message) { |
| 36 | + print('Received message: ${message.message.data}'); |
| 37 | + |
| 38 | + // Acknowledge the message. This buffers the ACK in the background |
| 39 | + // and batches it with others, sending it over one of the active |
| 40 | + // gRPC streams or falling back to a unary RPC if streams are down. |
| 41 | + subscription.acknowledge(message); |
| 42 | + }, |
| 43 | + onError: (Object error) { |
| 44 | + print('Stream encountered a permanent error: $error'); |
| 45 | + }, |
| 46 | + onDone: () { |
| 47 | + print('Stream closed.'); |
| 48 | + }, |
| 49 | + ); |
| 50 | + |
| 51 | + // Later, to stop receiving messages and clean up resources: |
| 52 | + await listener.cancel(); |
| 53 | + // #enddocregion streaming_pull_example |
| 54 | +} |
| 55 | + |
| 56 | +Future<void> acknowledgeNowExample(PubSub pubsub) async { |
| 57 | + // #docregion acknowledge_now_example |
| 58 | + final subscription = pubsub.subscription('my-subscription'); |
| 59 | + |
| 60 | + // Pull up to 10 messages from the subscription. |
| 61 | + final messages = await subscription.pull(maxMessages: 10); |
| 62 | + |
| 63 | + if (messages.isNotEmpty) { |
| 64 | + try { |
| 65 | + // Acknowledge all pulled messages immediately and wait for the RPC |
| 66 | + // to complete. This bypasses background batching. |
| 67 | + await subscription.acknowledgeNow(messages); |
| 68 | + print('Successfully acknowledged ${messages.length} messages.'); |
| 69 | + } on Exception catch (e) { |
| 70 | + print('Failed to acknowledge messages: $e'); |
| 71 | + } |
| 72 | + } |
| 73 | + // #enddocregion acknowledge_now_example |
| 74 | +} |
0 commit comments