-
Notifications
You must be signed in to change notification settings - Fork 18
/
rmqp_producer.h
228 lines (208 loc) · 10.5 KB
/
rmqp_producer.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
// Copyright 2020-2023 Bloomberg Finance L.P.
// SPDX-License-Identifier: Apache-2.0
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
// rmqp_producer.h
#ifndef INCLUDED_RMQP_PRODUCER
#define INCLUDED_RMQP_PRODUCER
#include <rmqt_queue.h>
#include <rmqp_topologyupdate.h>
#include <rmqt_confirmresponse.h>
#include <rmqt_exchange.h>
#include <rmqt_message.h>
#include <rmqt_result.h>
#include <rmqt_topology.h>
#include <bsls_timeinterval.h>
#include <bsl_functional.h>
#include <bsl_string.h>
#include <rmqt_future.h>
namespace BloombergLP {
namespace rmqp {
/// \class Producer
/// \brief RabbitMQ Producer API for publishing to a specific
/// Exchange.
///
/// A RabbitMQ Message Producer API object. A Producer is bound to a specific
/// Exchange. These objects are constructed by the Connection
class Producer {
public:
// TYPES
/// \brief Possible results of rmqp::Producer#send.
enum SendStatus { SENDING, DUPLICATE, TIMEOUT, INFLIGHT_LIMIT };
/// \brief Invoked on receipt of message confirmation.
///
/// The user-provided ConfirmationCallback is invoked once RabbitMQ broker
/// provides a guarantee (publisher confirm) that the message is enqueued.
///
/// A ConfirmationCallback implementation should perform any commit action,
/// such as confirming to the sender of the message that the action will be
/// completed. For example, an application which consumes from one queue
/// and produces to another should send the acknowledgement to the first
/// queue once the ConfirmationCallback is invoked from the publish.
typedef bsl::function<void(const rmqt::Message&,
const bsl::string& routingKey,
const rmqt::ConfirmResponse&)>
ConfirmationCallback;
// CREATORS
virtual ~Producer();
// MANIPULATORS
/// \brief Send a message with the given `routingKey` to the exchange
/// targeted by the producer.
///
/// The behavior of this method depends on the the number of unconfirmed
/// messages (sent but not yet confirmed by the broker). If this number is
/// smaller than the limit configured when calling
/// rmqa::VHost#createProducer, this method will return immediately.
/// Otherwise it will block until the unconfirmed message count drops below
/// the limit.
///
/// \param message The message to be sent.
/// \param routingKey The routing key (e.g. topic or queue name)
/// passed to the exchange.
/// \param confirmCallback Called when the broker explicitly
/// confirms/rejects
/// the message. Messages are automatically retried
/// on reconnection, in which case this method may
/// be called some time after invoking `send`.
/// \param timeout How long to wait for as a relative timeout. If
/// timeout is 0, the method will wait to send
/// message indefinitely
///
/// \return SENDING Returned when the library accepts the message for
/// sending.
/// If the connection is lost before receiving the
/// publisher confirm from the broker, the library will
/// retry sending the message.
/// \return DUPLICATE Returned if a message with the same GUID has
/// already been sent and is awaiting a confirm from the
/// broker. This indicates an issue with the application.
/// To send the same message multiple times, a new
/// rmqt::Message object must be created every time.
/// \return TIMEOUT Returned if a message couldn't be enqueued within the
/// timeout time.
virtual SendStatus
send(const rmqt::Message& message,
const bsl::string& routingKey,
const rmqp::Producer::ConfirmationCallback& confirmCallback,
const bsls::TimeInterval& timeout) = 0;
/// \brief Send a message with the given `routingKey` to the exchange
/// targeted by the producer.
///
/// The behavior of this method depends on the the number of unconfirmed
/// messages (sent but not yet confirmed by the broker). If this number is
/// smaller than the limit configured when calling
/// rmqa::VHost#createProducer, this method will return immediately.
/// Otherwise it will block until the unconfirmed message count drops below
/// the limit.
///
/// \param message The message to be sent.
/// \param routingKey The routing key (e.g. topic or queue name)
/// passed to the exchange.
/// \param mandatory Specify the mandatory flag:
/// RETURN_UNROUTABLE (Recommended): Any messages not
/// passed to a queue are returned to the sender.
/// confirmCallback will be invoked with a RETURN status.
/// DISCARD_UNROUTABLE (**Dangerous**): Any messages not
/// passed to a queue are confirmed by the broker. This
/// will cause silent message loss in the event bindings
/// aren't setup as expected.
/// \param confirmCallback Called when the broker explicitly
/// confirms/rejects
/// the message. Messages are automatically retried
/// on reconnection, in which case this method may
/// be called some time after invoking `send`.
/// \param timeout How long to wait for as a relative timeout. If
/// timeout is 0, the method will wait to send
/// message indefinitely
///
/// \return SENDING Returned when the library accepts the message for
/// sending.
/// If the connection is lost before receiving the
/// publisher confirm from the broker, the library will
/// retry sending the message.
/// \return DUPLICATE Returned if a message with the same GUID has
/// already been sent and is awaiting a confirm from the
/// broker. This indicates an issue with the application.
/// To send the same message multiple times, a new
/// rmqt::Message object must be created every time.
/// \return TIMEOUT Returned if a message couldn't be enqueued within the
/// timeout time.
virtual SendStatus
send(const rmqt::Message& message,
const bsl::string& routingKey,
rmqt::Mandatory::Value mandatoryFlag,
const rmqp::Producer::ConfirmationCallback& confirmCallback,
const bsls::TimeInterval& timeout) = 0;
/// \brief Send a message with the given `routingKey` to the exchange
/// targeted by the producer.
///
/// The behavior of this method depends on the the number of unconfirmed
/// messages (sent but not yet confirmed by the broker). If this number is
/// smaller than the limit configured when calling
/// rmqa::VHost#createProducer, this method behaves exactly as the method
/// `send`. Otherwise, unlike `send`, this method returns immediately with
/// a result indicating that the unconfirmed message limit has been
/// reached.
///
/// \param message The message to be sent.
/// \param routingKey The routing key (e.g. topic or queue name)
/// passed to the exchange.
/// \param confirmCallback Called when the broker explicitly
/// confirms/rejects
/// the message. Messages are automatically retried
/// on reconnection, in which case this method may
/// be called some time after invoking `send`.
///
/// \return SENDING Returned when the library accepts the message
/// for sending.
/// If the connection is lost before receiving the
/// publisher confirm from the broker, the library
/// will retry sending the message.
/// \return DUPLICATE Returned if a message with the same GUID has
/// already been sent and is awaiting a confirm from
/// the broker. This indicates an issue with the
/// application. To send the same message multiple
/// times, a new rmqt::Message object must be
/// created every time.
/// \return INFLIGHT_LIMIT Returned if the unconfirmed message limit has
/// been reached.
virtual SendStatus
trySend(const rmqt::Message& message,
const bsl::string& routingKey,
const rmqp::Producer::ConfirmationCallback& confirmCallback) = 0;
/// \brief Wait for all outstanding publisher confirms to arrive.
///
/// This method allows
///
/// \param timeout How long to wait for. If timeout is 0, the method will
/// wait for confirms indefinitely.
///
/// \return true if all outstanding confirms have arrived.
/// \return false if waiting timed out.
virtual rmqt::Result<>
waitForConfirms(const bsls::TimeInterval& timeout) = 0;
/// \brief Updates topology
///
/// \return A Future which, when resolved, will contain the result of the
/// update.
virtual rmqt::Future<>
updateTopologyAsync(const rmqt::TopologyUpdate& topologyUpdate) = 0;
protected:
Producer();
private:
Producer(const Producer&) BSLS_KEYWORD_DELETED;
Producer& operator=(const Producer&) BSLS_KEYWORD_DELETED;
}; // class Producer
} // namespace rmqp
} // namespace BloombergLP
#endif // ! INCLUDED_RMQP_PRODUCER