-
Notifications
You must be signed in to change notification settings - Fork 0
/
example.cpp
72 lines (56 loc) · 2.51 KB
/
example.cpp
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
#include "Connection.hpp"
#include <iostream>
void receivedCb(AMQP::TcpChannel& channel, const std::string& queue, const AMQP::Message& message, uint64_t deliveryTag)
{
std::cerr << "Consumer: receiveCb - queue = " << queue
<< " message = " << std::string(message.body(), message.bodySize()) << std::endl;
channel.ack(deliveryTag);
}
void onAckCb(const std::string& queue, const std::string& message)
{
std::cerr << "Publisher: onAckCb - queue = " << queue << " message = " << message << std::endl;
}
void onLostCb(const std::string& queue, const std::string& message)
{
std::cerr << "Publisher: onLostCb - queue = " << queue << " message = " << message << std::endl;
}
int main(int argc, char** argv)
{
if (argc != 2)
{
std::cerr << "Broker address (and login credentials) not provided" << std::endl;
}
std::cout << "Creating connection" << std::endl;
// address string format: amqp[s]://[<user>[:<password>]@]<broker_hostname>[:<port>]
auto connection = std::make_unique<simple_amqpcpp::Connection>(
AMQP::Address(argv[1]), [] { std::cout << "Connection detached" << std::endl; });
if (!connection->connected())
{
std::cerr << "Failed to establish connection to broker" << std::endl;
return 1;
}
std::cout << std::endl << "Declaring queues and consuming" << std::endl;
connection->useChannel([](simple_amqpcpp::Channel& channel) {
channel.declareQueue("queue_1");
channel.declareQueue("queue_2");
channel.consume("queue_1").onReceived(
[&](const auto& message, auto tag, bool) { receivedCb(channel, "queue_1", message, tag); });
channel.consume("queue_2").onReceived(
[&](const auto& message, auto tag, bool) { receivedCb(channel, "queue_2", message, tag); });
});
std::cout << std::endl << "Sending messages" << std::endl;
for (size_t i = 0; i < 5; ++i)
{
std::string message = "message_" + std::to_string(i);
// simple channel
connection->useChannel([=](simple_amqpcpp::Channel& channel) { channel.publish("", "queue_1", message); });
// robust channel - with AMQP::Reliable and AMQP::Throttle
connection->useChannel([=](simple_amqpcpp::ReliableWrapper& channel) {
channel.publish("", "queue_2", message).onAck([=] { onAckCb("queue_2", message); }).onLost([=] {
onLostCb("queue_2", message);
});
});
std::this_thread::sleep_for(std::chrono::seconds(1));
}
return 0;
}