Google Cloud Pub/Sub C++ Client 2.13.0
A C++ Client Library for Google Cloud Pub/Sub
Loading...
Searching...
No Matches
blocking_publisher.h
1// Copyright 2022 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_BLOCKING_PUBLISHER_H
16#define GOOGLE_CLOUD_CPP_GOOGLE_CLOUD_PUBSUB_BLOCKING_PUBLISHER_H
17
18#include "google/cloud/pubsub/blocking_publisher_connection.h"
19#include "google/cloud/pubsub/publisher_options.h"
20#include "google/cloud/pubsub/version.h"
21#include <string>
22
23namespace google {
24namespace cloud {
25namespace pubsub {
26GOOGLE_CLOUD_CPP_INLINE_NAMESPACE_BEGIN
27
28/**
29 * Publish messages to the Cloud Pub/Sub service.
30 *
31 * This class is used to publish messages to any given topic. It is intended
32 * for low-volume publishers. Applications sending less than one message per
33 * second may find this class easier to use than `Publisher`, which can handle
34 * thousands of messages per second.
35 *
36 * @see https://cloud.google.com/pubsub for an overview of the Cloud Pub/Sub
37 * service.
38 *
39 * @par Example
40 * @snippet blocking_samples.cc blocking-publish
41 *
42 * @par Performance
43 * `BlockingPublisher` objects are relatively cheap to create, copy, and move.
44 * However, each `BlockingPublisher` object must be created with a
45 * `std::shared_ptr<BlockingPublisherConnection>`, which itself is relatively
46 * expensive to create. Therefore, connection instances should be shared when
47 * possible. See the `MakeBlockingPublisherConnection()` method and the
48 * `BlockingPublisherConnection` interface for more details.
49 *
50 * @par Thread Safety
51 * Instances of this class created via copy-construction or copy-assignment
52 * share the underlying pool of connections. Access to these copies via multiple
53 * threads is guaranteed to work. Two threads operating on the same instance of
54 * this class is not guaranteed to work.
55 *
56 * @par Background Threads
57 * This class uses the background threads configured via the `Options` from
58 * `GrpcOptionList`. Applications can create their own pool of background
59 * threads by (a) creating their own #google::cloud::CompletionQueue, (b)
60 * passing this completion queue as a `GrpcCompletionQueueOption`, and (c)
61 * attaching any number of threads to the completion queue.
62 *
63 * @par Error Handling
64 * This class uses `StatusOr<T>` to report errors. When an operation fails to
65 * perform its work the returned `StatusOr<T>` contains the error details. If
66 * the `ok()` member function in the `StatusOr<T>` returns `true` then it
67 * contains the expected result. Please consult the #google::cloud::StatusOr
68 * documentation for more details.
69 */
71 public:
72 explicit BlockingPublisher(
73 std::shared_ptr<BlockingPublisherConnection> connection,
74 Options opts = {});
75
76 BlockingPublisher(BlockingPublisher const&) = default;
80
81 friend bool operator==(BlockingPublisher const& a,
82 BlockingPublisher const& b) {
83 return a.connection_ == b.connection_;
84 }
85 friend bool operator!=(BlockingPublisher const& a,
86 BlockingPublisher const& b) {
87 return !(a == b);
88 }
89
90 /**
91 * Publishes the @p message on the topic @p topic.
92 *
93 * @par Idempotency
94 * This is a non-idempotent operation, but the client library will
95 * automatically retry RPCs that fail with transient errors. As Cloud Pub/Sub
96 * has "at least once" delivery semantics applications are expected to handle
97 * duplicate messages without problems. The application can disable retries
98 * by changing the retry policy, please see the example below.
99 *
100 * @par Example
101 * @snippet blocking_samples.cc blocking-publish
102 *
103 * @par Example
104 * @snippet blocking_samples.cc blocking-publish-no-retry
105 *
106 * @return On success, the server-assigned ID of the message. IDs are
107 * guaranteed to be unique within the topic.
108 */
109 StatusOr<std::string> Publish(Topic topic, Message message,
110 Options opts = {});
111
112 private:
113 std::shared_ptr<BlockingPublisherConnection> connection_;
114 Options options_;
115};
116
117GOOGLE_CLOUD_CPP_INLINE_NAMESPACE_END
118} // namespace pubsub
119} // namespace cloud
120} // namespace google
121
122#endif // GOOGLE_CLOUD_CPP_GOOGLE_CLOUD_PUBSUB_BLOCKING_PUBLISHER_H
A connection to the Cloud Pub/Sub service to publish events.
Definition: blocking_publisher_connection.h:56
Publish messages to the Cloud Pub/Sub service.
Definition: blocking_publisher.h:70
BlockingPublisher & operator=(BlockingPublisher const &)=default
StatusOr< std::string > Publish(Topic topic, Message message, Options opts={})
Publishes the message on the topic topic.
BlockingPublisher(BlockingPublisher const &)=default
BlockingPublisher & operator=(BlockingPublisher &&)=default
BlockingPublisher(std::shared_ptr< BlockingPublisherConnection > connection, Options opts={})
friend bool operator!=(BlockingPublisher const &a, BlockingPublisher const &b)
Definition: blocking_publisher.h:85
BlockingPublisher(BlockingPublisher &&)=default
friend bool operator==(BlockingPublisher const &a, BlockingPublisher const &b)
Definition: blocking_publisher.h:81
The C++ representation for a Cloud Pub/Sub messages.
Definition: message.h:78
Objects of this class identify a Cloud Pub/Sub topic.
Definition: topic.h:37
Contains all the Cloud Pub/Sub C++ client types and functions.
Definition: ack_handler.h:25
The namespace Google Cloud Platform C++ client libraries.