Google Cloud Pub/Sub C++ Client 2.13.0
A C++ Client Library for Google Cloud Pub/Sub
Loading...
Searching...
No Matches
subscriber.h
1// Copyright 2020 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#ifndef GOOGLE_CLOUD_CPP_GOOGLE_CLOUD_PUBSUB_SUBSCRIBER_H
16#define GOOGLE_CLOUD_CPP_GOOGLE_CLOUD_PUBSUB_SUBSCRIBER_H
17
18#include "google/cloud/pubsub/ack_handler.h"
19#include "google/cloud/pubsub/exactly_once_ack_handler.h"
20#include "google/cloud/pubsub/message.h"
21#include "google/cloud/pubsub/subscriber_connection.h"
22#include "google/cloud/pubsub/subscription.h"
23#include "google/cloud/pubsub/version.h"
24#include "google/cloud/status.h"
25#include <functional>
26
27namespace google {
28namespace cloud {
29namespace pubsub {
30GOOGLE_CLOUD_CPP_INLINE_NAMESPACE_BEGIN
31/**
32 * Receive messages from the Cloud Pub/Sub service.
33 *
34 * This class is used to receive message from a given subscription, with a fixed
35 * configuration such as credentials, and background threads. Applications that
36 * receive messages from multiple subscriptions need to create separate
37 * instances of this class. Applications wanting to receive events with
38 * configuration parameters also need to create separate instances.
39 *
40 * @see https://cloud.google.com/pubsub for an overview of the Cloud Pub/Sub
41 * service.
42 *
43 * @par Example: subscriber quickstart
44 * @snippet samples.cc subscribe
45 *
46 * @par Performance
47 * `Subscriber` objects are relatively cheap to create, copy, and move. However,
48 * each `Subscriber` object must be created with a
49 * `std::shared_ptr<SubscriberConnection>`, which itself is relatively expensive
50 * to create. Therefore, connection instances should be shared when possible.
51 * See the `MakeSubscriberConnection()` function and the `SubscriberConnection`
52 * interface for more details.
53 *
54 * @par Thread Safety
55 * Instances of this class created via copy-construction or copy-assignment
56 * share the underlying pool of connections. Access to these copies via multiple
57 * threads is guaranteed to work. Two threads operating on the same instance of
58 * this class is not guaranteed to work.
59 *
60 * @par Background Threads
61 * This class uses the background threads configured via the `Options` from
62 * `GrpcOptionList`. Applications can create their own pool of background
63 * threads by (a) creating their own #google::cloud::CompletionQueue, (b)
64 * passing this completion queue as a `GrpcCompletionQueueOption`, and (c)
65 * attaching any number of threads to the completion queue.
66 *
67 * @par Example: using a custom thread pool
68 * @snippet samples.cc custom-thread-pool-subscriber
69 *
70 * @par Asynchronous Functions
71 * Some of the member functions in this class return a `future<T>` (or
72 * `future<StatusOr<T>>`) object. Readers are probably familiar with
73 * [`std::future<T>`][std-future-link]. Our version adds a `.then()` function to
74 * attach a callback to the future, which is invoked when the future is
75 * satisfied. This function returns a `future<U>` where `U` is the return value
76 * of the attached function. More details in the #google::cloud::future
77 * documentation.
78 *
79 * @par Error Handling
80 * This class uses `StatusOr<T>` to report errors. When an operation fails to
81 * perform its work the returned `StatusOr<T>` contains the error details. If
82 * the `ok()` member function in the `StatusOr<T>` returns `true` then it
83 * contains the expected result. Please consult the #google::cloud::StatusOr
84 * documentation for more details.
85 *
86 * @par Changing Retry Parameters Example
87 * @snippet samples.cc subscriber-retry-settings
88 *
89 * [std-future-link]: https://en.cppreference.com/w/cpp/thread/future
90 */
91class Subscriber {
92 public:
93 explicit Subscriber(std::shared_ptr<SubscriberConnection> connection,
94 Options opts = {});
95
96 /**
97 * Creates a new session to receive messages from @p subscription.
98 *
99 * @par Idempotency
100 * @parblock
101 * This is an idempotent operation; it only reads messages from the service.
102 * Will make multiple attempts to start a connection to the service, subject
103 * to the retry policies configured in the `SubscriberConnection`. Once a
104 * successful connection is established the library will try to resume the
105 * connection even if the connection fails with a permanent error. Resuming
106 * the connection is subject to the retry policies as described earlier.
107 *
108 * Note that calling `AckHandler::ack()` and/or `AckHandler::nack()` is
109 * handled differently with respect to retrying. Check the documentation of
110 * these functions for details.
111 * @endparblock
112 *
113 * @par Example
114 * @snippet samples.cc subscribe
115 *
116 * @param cb the callable invoked when messages are received.
117 * @param opts any option overrides to use in this call. These options take
118 * precedence over the options passed in the constructor, and over any
119 * options provided in the `PublisherConnection` initialization.
120 * @return a future that is satisfied when the session will no longer receive
121 * messages. For example, because there was an unrecoverable error trying
122 * to receive data. Calling `.cancel()` in this object will (eventually)
123 * terminate the session and satisfy the future.
124 */
125 future<Status> Subscribe(ApplicationCallback cb, Options opts = {});
126
127 /**
128 * Creates a new session to receive messages from @p subscription using
129 * exactly-once delivery.
130 *
131 * @par Idempotency
132 * @parblock
133 * This is an idempotent operation; it only reads messages from the service.
134 * Will make multiple attempts to start a connection to the service, subject
135 * to the retry policies configured in the `SubscriberConnection`. Once a
136 * successful connection is established the library will try to resume the
137 * connection even if the connection fails with a permanent error. Resuming
138 * the connection is subject to the retry policies as described earlier.
139 *
140 * Note that calling `ExactlyOnceAckHandler::ack()` and/or
141 * `ExactlyOnceAckHandler::nack()` have their own rules with respect to
142 * retrying. Check the documentation of these functions for details.
143 * @endparblock
144 *
145 * @par Example
146 * @snippet samples.cc exactly-once-subscribe
147 *
148 * @param cb the callable invoked when messages are received.
149 * @param opts any option overrides to use in this call. These options take
150 * precedence over the options passed in the constructor, and over any
151 * options provided in the `PublisherConnection` initialization.
152 * @return a future that is satisfied when the session will no longer receive
153 * messages. For example, because there was an unrecoverable error trying
154 * to receive data. Calling `.cancel()` in this object will (eventually)
155 * terminate the session and satisfy the future.
156 */
157 future<Status> Subscribe(ExactlyOnceApplicationCallback cb,
158 Options opts = {});
159
160 /**
161 * Pulls one message from @p subscription.
162 *
163 * @par Idempotency
164 * @parblock
165 * This is an idempotent operation; it only reads messages from the service.
166 * It will make multiple attempts to pull a message from the service, subject
167 * to the retry policies configured in the `SubscriberConnection`.
168 *
169 * Note that calling `PullAckHandler::ack()` and/or `PullAckHandler::nack()`
170 * have their own rules with respect to retrying.
171 * @endparblock
172 *
173 * @par Example
174 * @snippet samples.cc pull
175 *
176 * @param opts any option overrides to use in this call. These options take
177 * precedence over the options passed in the constructor, and over any
178 * options provided in the `PublisherConnection` initialization.
179 * @return a response including the message and a `PullAckHandler` to notify
180 * the library when the message has been successfully handled.
181 */
182 StatusOr<PullResponse> Pull(Options opts = {});
183
184 private:
185 std::shared_ptr<SubscriberConnection> connection_;
186 google::cloud::Options options_;
187};
188
189GOOGLE_CLOUD_CPP_INLINE_NAMESPACE_END
190} // namespace pubsub
191} // namespace cloud
192} // namespace google
193
194#endif // GOOGLE_CLOUD_CPP_GOOGLE_CLOUD_PUBSUB_SUBSCRIBER_H
friend friend class future
A connection to the Cloud Pub/Sub service to receive events.
Definition: subscriber_connection.h:57
Receive messages from the Cloud Pub/Sub service.
Definition: subscriber.h:91
future< Status > Subscribe(ApplicationCallback cb, Options opts={})
Creates a new session to receive messages from subscription.
Subscriber(std::shared_ptr< SubscriberConnection > connection, Options opts={})
future< Status > Subscribe(ExactlyOnceApplicationCallback cb, Options opts={})
Creates a new session to receive messages from subscription using exactly-once delivery.
StatusOr< PullResponse > Pull(Options opts={})
Pulls one message from subscription.
Contains all the Cloud Pub/Sub C++ client types and functions.
Definition: ack_handler.h:25
The namespace Google Cloud Platform C++ client libraries.
The response for a blocking pull.
Definition: pull_response.h:43