| 1 |
/* |
| 2 |
* |
| 3 |
* Licensed to the Apache Software Foundation (ASF) under one |
| 4 |
* or more contributor license agreements. See the NOTICE file |
| 5 |
* distributed with this work for additional information |
| 6 |
* regarding copyright ownership. The ASF licenses this file |
| 7 |
* to you under the Apache License, Version 2.0 (the |
| 8 |
* "License"); you may not use this file except in compliance |
| 9 |
* with the License. You may obtain a copy of the License at |
| 10 |
* |
| 11 |
* http://www.apache.org/licenses/LICENSE-2.0 |
| 12 |
* |
| 13 |
* Unless required by applicable law or agreed to in writing, |
| 14 |
* software distributed under the License is distributed on an |
| 15 |
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 16 |
* KIND, either express or implied. See the License for the |
| 17 |
* specific language governing permissions and limitations |
| 18 |
* under the License. |
| 19 |
* |
| 20 |
*/ |
| 21 |
|
| 22 |
/* |
| 23 |
* Service Bus example. |
| 24 |
* |
| 25 |
* This is an example of using "Service Bus sessions" (not the same thing as an |
| 26 |
* AMQP session) to selectively retrieve messages from a queue. The queue must |
| 27 |
* be configured within Service Bus to support sessions. Service Bus uses the |
| 28 |
* AMQP group_id message property to associate messages with a particular |
| 29 |
* Service Bus session. It uses AMQP filters to specify which session is |
| 30 |
* associated with a receiver. |
| 31 |
* |
| 32 |
* The mechanics for sending and receiving to other types of service bus queue |
| 33 |
* are broadly the same, as long as the step using the |
| 34 |
* receiver.source().filters() is omitted. |
| 35 |
* |
| 36 |
* Other Service Bus notes: There is no drain support, hence the need to to use |
| 37 |
* timeouts in this example to detect the end of the message stream. There is |
| 38 |
* no browse support when setting the AMQP link distribution mode to COPY. |
| 39 |
* Service Bus claims to support browsing, but it is unclear how to manage that |
| 40 |
* with an AMQP client. Maximum message sizes (for body and headers) vary |
| 41 |
* between queue types and fee tier ranging from 64KB to 1MB. Due to the |
| 42 |
* distributed nature of Service Bus, queues do not automatically preserve FIFO |
| 43 |
* order of messages unless the user takes steps to force the message stream to |
| 44 |
* a single partition of the queue or creates the queue with partitioning disabled. |
| 45 |
* |
| 46 |
* This example shows use of the simpler SAS (Shared Access Signature) |
| 47 |
* authentication scheme where the credentials are supplied on the connection. |
| 48 |
* Service Bus does not actually check these credentials when setting up the |
| 49 |
* connection, it merely caches the SAS key and policy (AKA key name) for later |
| 50 |
* access authorization when creating senders and receivers. There is a second |
| 51 |
* authentication scheme that allows for multiple tokens and even updating them |
| 52 |
* within a long-lived connection which uses special management request-response |
| 53 |
* queues in Service Bus. The format of this exchange may be documented |
| 54 |
* somewhere but is also available by working through the CbsAsyncExample.cs |
| 55 |
* program in the Amqp.Net Lite project. |
| 56 |
* |
| 57 |
* The sample output for this program is: |
| 58 |
|
| 59 |
sent message: message 0 in service bus session "red" |
| 60 |
sent message: message 1 in service bus session "green" |
| 61 |
sent message: message 2 in service bus session "blue" |
| 62 |
sent message: message 3 in service bus session "red" |
| 63 |
sent message: message 4 in service bus session "black" |
| 64 |
sent message: message 5 in service bus session "blue" |
| 65 |
sent message: message 6 in service bus session "yellow" |
| 66 |
receiving messages with session identifier "green" from queue ses_q1 |
| 67 |
received message: message 1 in service bus session "green" |
| 68 |
receiving messages with session identifier "red" from queue ses_q1 |
| 69 |
received message: message 0 in service bus session "red" |
| 70 |
received message: message 3 in service bus session "red" |
| 71 |
receiving messages with session identifier "blue" from queue ses_q1 |
| 72 |
received message: message 2 in service bus session "blue" |
| 73 |
received message: message 5 in service bus session "blue" |
| 74 |
receiving messages with session identifier "black" from queue ses_q1 |
| 75 |
received message: message 4 in service bus session "black" |
| 76 |
receiving messages with session identifier "yellow" from queue ses_q1 |
| 77 |
received message: message 6 in service bus session "yellow" |
| 78 |
Done. No more messages. |
| 79 |
|
| 80 |
* |
| 81 |
*/ |
| 82 |
|
| 83 |
#include "options.hpp" |
| 84 |
|
| 85 |
#include <proton/connection.hpp> |
| 86 |
#include <proton/connection_options.hpp> |
| 87 |
#include <proton/container.hpp> |
| 88 |
#include <proton/delivery.hpp> |
| 89 |
#include <proton/message.hpp> |
| 90 |
#include <proton/messaging_handler.hpp> |
| 91 |
#include <proton/receiver_options.hpp> |
| 92 |
#include <proton/ssl.hpp> |
| 93 |
#include <proton/sender.hpp> |
| 94 |
#include <proton/sender_options.hpp> |
| 95 |
#include <proton/source_options.hpp> |
| 96 |
#include <proton/tracker.hpp> |
| 97 |
#include <proton/work_queue.hpp> |
| 98 |
|
| 99 |
#include <iostream> |
| 100 |
#include <sstream> |
| 101 |
|
| 102 |
|
| 103 |
using proton::source_options; |
| 104 |
using proton::connection_options; |
| 105 |
using proton::sender_options; |
| 106 |
using proton::receiver_options; |
| 107 |
using proton::ssl_client_options; |
| 108 |
|
| 109 |
void do_next_sequence(); |
| 110 |
|
| 111 |
namespace { |
| 112 |
void check_arg(const std::string &value, const std::string &name) { |
| 113 |
if (value.empty()) |
| 114 |
throw std::runtime_error("missing argument for \"" + name + "\""); |
| 115 |
} |
| 116 |
} |
| 117 |
|
| 118 |
/// Connect to Service Bus queue and retrieve messages in a particular session. |
| 119 |
class session_receiver : public proton::messaging_handler { |
| 120 |
private: |
| 121 |
const std::string &connection_url; |
| 122 |
const std::string &entity; |
| 123 |
connection_options coptions; |
| 124 |
proton::value session_identifier; // AMQP null type by default, matches any Service Bus sequence identifier |
| 125 |
int message_count; |
| 126 |
bool closed; |
| 127 |
proton::duration read_timeout; |
| 128 |
proton::timestamp last_read; |
| 129 |
proton::container *container; |
| 130 |
proton::receiver receiver; |
| 131 |
|
| 132 |
public: |
| 133 |
session_receiver(const std::string &c, const std::string &e, const connection_options &co, |
| 134 |
const char *sid) : |
| 135 |
connection_url(c), entity(e), coptions(co), |
| 136 |
message_count(0), closed(false), read_timeout(5000), last_read(0), container(0) { |
| 137 |
if (sid) |
| 138 |
session_identifier = std::string(sid); |
| 139 |
// session_identifier is now either empty/null or an AMQP string type. |
| 140 |
// If null, Service Bus will pick the first available message and create |
| 141 |
// a filter at its end with that message's session identifier. |
| 142 |
// Technically, an AMQP string is not a valid filter-set value unless it |
| 143 |
// is annotated as an AMQP described type, so this may change. |
| 144 |
|
| 145 |
} |
| 146 |
|
| 147 |
void run (proton::container &c) { |
| 148 |
message_count = 0; |
| 149 |
closed = false; |
| 150 |
c.connect(connection_url, coptions.handler(*this)); |
| 151 |
container = &c; |
| 152 |
} |
| 153 |
|
| 154 |
void on_connection_open(proton::connection &connection) override { |
| 155 |
proton::source::filter_map sb_filter_map; |
| 156 |
proton::symbol key("com.microsoft:session-filter"); |
| 157 |
sb_filter_map.put(key, session_identifier); |
| 158 |
receiver = connection.open_receiver(entity, receiver_options().source(source_options().filters(sb_filter_map))); |
| 159 |
|
| 160 |
// Start timeout processing here. If Service Bus has no pending |
| 161 |
// messages, it may defer completing the receiver open until a message |
| 162 |
// becomes available (e.g. to be able to set the actual session |
| 163 |
// identifier if none was specified). |
| 164 |
last_read = proton::timestamp::now(); |
| 165 |
// Call this->process_timeout after read_timeout. |
| 166 |
connection.work_queue().schedule(read_timeout, [this]() { this->process_timeout(); }); |
| 167 |
} |
| 168 |
|
| 169 |
void on_receiver_open(proton::receiver &r) override { |
| 170 |
if (closed) return; // PROTON-1264 |
| 171 |
proton::value actual_session_id = r.source().filters().get("com.microsoft:session-filter"); |
| 172 |
std::cout << "receiving messages with session identifier \"" << actual_session_id |
| 173 |
<< "\" from queue " << entity << std::endl; |
| 174 |
last_read = proton::timestamp::now(); |
| 175 |
} |
| 176 |
|
| 177 |
void on_message(proton::delivery &, proton::message &m) override { |
| 178 |
message_count++; |
| 179 |
std::cout << " received message: " << m.body() << std::endl; |
| 180 |
last_read = proton::timestamp::now(); |
| 181 |
} |
| 182 |
|
| 183 |
void process_timeout() { |
| 184 |
proton::timestamp deadline = last_read + read_timeout; |
| 185 |
proton::timestamp now = proton::timestamp::now(); |
| 186 |
if (now >= deadline) { |
| 187 |
receiver.close(); |
| 188 |
closed = true; |
| 189 |
receiver.connection().close(); |
| 190 |
if (message_count) |
| 191 |
do_next_sequence(); |
| 192 |
else |
| 193 |
std::cout << "Done. No more messages." << std::endl; |
| 194 |
} else { |
| 195 |
proton::duration next = deadline - now; |
| 196 |
receiver.work_queue().schedule(next, [this]() { this->process_timeout(); }); |
| 197 |
} |
| 198 |
} |
| 199 |
}; |
| 200 |
|
| 201 |
|
| 202 |
/// Connect to Service Bus queue and send messages divided into different sessions. |
| 203 |
class session_sender : public proton::messaging_handler { |
| 204 |
private: |
| 205 |
const std::string &connection_url; |
| 206 |
const std::string &entity; |
| 207 |
connection_options coptions; |
| 208 |
int msg_count; |
| 209 |
int total; |
| 210 |
int accepts; |
| 211 |
|
| 212 |
public: |
| 213 |
session_sender(const std::string &c, const std::string &e, const connection_options &co) : |
| 214 |
connection_url(c), entity(e), coptions(co), |
| 215 |
msg_count(0), total(7), accepts(0) {} |
| 216 |
|
| 217 |
void run(proton::container &c) { |
| 218 |
c.open_sender(connection_url + "/" + entity, sender_options(), coptions.handler(*this)); |
| 219 |
} |
| 220 |
|
| 221 |
void send_remaining_messages(proton::sender &s) { |
| 222 |
std::string gid; |
| 223 |
for (; msg_count < total && s.credit() > 0; msg_count++) { |
| 224 |
switch (msg_count) { |
| 225 |
case 0: gid = "red"; break; |
| 226 |
case 1: gid = "green"; break; |
| 227 |
case 2: gid = "blue"; break; |
| 228 |
case 3: gid = "red"; break; |
| 229 |
case 4: gid = "black"; break; |
| 230 |
case 5: gid = "blue"; break; |
| 231 |
case 6: gid = "yellow"; break; |
| 232 |
} |
| 233 |
|
| 234 |
std::ostringstream mbody; |
| 235 |
mbody << "message " << msg_count << " in service bus session \"" << gid << "\""; |
| 236 |
proton::message m(mbody.str()); |
| 237 |
m.group_id(gid); // Service Bus uses the group_id property to as the session identifier. |
| 238 |
s.send(m); |
| 239 |
std::cout << " sent message: " << m.body() << std::endl; |
| 240 |
} |
| 241 |
} |
| 242 |
|
| 243 |
void on_sendable(proton::sender &s) override { |
| 244 |
send_remaining_messages(s); |
| 245 |
} |
| 246 |
|
| 247 |
void on_tracker_accept(proton::tracker &t) override { |
| 248 |
accepts++; |
| 249 |
if (accepts == total) { |
| 250 |
// upload complete |
| 251 |
t.sender().close(); |
| 252 |
t.sender().connection().close(); |
| 253 |
do_next_sequence(); |
| 254 |
} |
| 255 |
} |
| 256 |
}; |
| 257 |
|
| 258 |
|
| 259 |
/// Orchestrate the sequential actions of sending and receiving session-based messages. |
| 260 |
class sequence : public proton::messaging_handler { |
| 261 |
private: |
| 262 |
proton::container *container; |
| 263 |
int sequence_no; |
| 264 |
session_sender snd; |
| 265 |
session_receiver rcv_red, rcv_green, rcv_null; |
| 266 |
|
| 267 |
public: |
| 268 |
static sequence *the_sequence; |
| 269 |
|
| 270 |
sequence (const std::string &c, const std::string &e, const connection_options &co) : |
| 271 |
container(0), sequence_no(0), |
| 272 |
snd(c, e, co), rcv_red(c, e, co, "red"), rcv_green(c, e, co, "green"), rcv_null(c, e, co, NULL) { |
| 273 |
the_sequence = this; |
| 274 |
} |
| 275 |
|
| 276 |
void on_container_start(proton::container &c) override { |
| 277 |
container = &c; |
| 278 |
next_sequence(); |
| 279 |
} |
| 280 |
|
| 281 |
void next_sequence() { |
| 282 |
switch (sequence_no++) { |
| 283 |
// run these in order exactly once |
| 284 |
case 0: snd.run(*container); break; |
| 285 |
case 1: rcv_green.run(*container); break; |
| 286 |
case 2: rcv_red.run(*container); break; |
| 287 |
// Run this until the receiver decides there is no messages left to sequence through |
| 288 |
default: rcv_null.run(*container); break; |
| 289 |
} |
| 290 |
} |
| 291 |
}; |
| 292 |
|
| 293 |
sequence *sequence::the_sequence = NULL; |
| 294 |
|
| 295 |
void do_next_sequence() { sequence::the_sequence->next_sequence(); } |
| 296 |
|
| 297 |
|
| 298 |
int main(int argc, char **argv) { |
| 299 |
std::string sb_namespace; // i.e. "foo.servicebus.windows.net" |
| 300 |
std::string sb_key_name; // shared access key name for entity (AKA "Policy Name") |
| 301 |
std::string sb_key; // shared access key |
| 302 |
std::string sb_entity; // AKA the service bus queue. Must enable |
| 303 |
// sessions on it for this example. |
| 304 |
|
| 305 |
example::options opts(argc, argv); |
| 306 |
opts.add_value(sb_namespace, 'n', "namespace", "Service Bus full namespace", "NAMESPACE"); |
| 307 |
opts.add_value(sb_key_name, 'p', "policy", "policy name that specifies access rights (key name)", "POLICY"); |
| 308 |
opts.add_value(sb_key, 'k', "key", "secret key for the policy", "key"); |
| 309 |
opts.add_value(sb_entity, 'e', "entity", "entity path (queue name)", "ENTITY"); |
| 310 |
|
| 311 |
try { |
| 312 |
opts.parse(); |
| 313 |
check_arg(sb_namespace, "namespace"); |
| 314 |
check_arg(sb_key_name, "policy"); |
| 315 |
check_arg(sb_key, "key"); |
| 316 |
check_arg(sb_entity, "entity"); |
| 317 |
std::string connection_string("amqps://" + sb_namespace); |
| 318 |
|
| 319 |
sequence seq(connection_string, sb_entity, |
| 320 |
connection_options() |
| 321 |
.user(sb_key_name) |
| 322 |
.password(sb_key) |
| 323 |
.sasl_allowed_mechs("PLAIN")); |
| 324 |
proton::container(seq).run(); |
| 325 |
return 0; |
| 326 |
} catch (const std::exception& e) { |
| 327 |
std::cerr << e.what() << std::endl; |
| 328 |
} |
| 329 |
|
| 330 |
return 1; |
| 331 |
} |