-
Notifications
You must be signed in to change notification settings - Fork 371
Expand file tree
/
Copy pathlibboostasio.cpp
More file actions
66 lines (50 loc) · 1.69 KB
/
Copy pathlibboostasio.cpp
File metadata and controls
66 lines (50 loc) · 1.69 KB
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
/**
* LibBoostAsio.cpp
*
* Test program to check AMQP functionality based on Boost's asio io_service.
*
* @author Gavin Smith <gavin.smith@coralbay.tv>
*
* Compile with
* g++ -std=c++17 libboostasio.cpp -o boost_test -lpthread -ldl -lboost_system -lamqpcpp
*/
/**
* Dependencies
*/
#include <boost/asio/io_context.hpp>
#include <amqpcpp.h>
#include <amqpcpp/libboostasio.h>
/**
* Main program
* @return int
*/
int main()
{
// access to the boost asio handler
boost::asio::io_context context;
// handler for libboostasio
AMQP::LibBoostAsioHandler handler(context);
// make a connection
AMQP::TcpConnection connection(&handler, AMQP::Address("amqp://guest:guest@localhost/"));
// we need a channel too
AMQP::TcpChannel channel(&connection);
channel.onReady([]() {
std::cout << "channel ready" << std::endl;
});
channel.onError([](const char *message) {
std::cout << "channel error: " << message << std::endl;
});
// create a temporary queue
channel.declareQueue(AMQP::exclusive).onSuccess([&connection](const std::string &name, uint32_t messagecount, uint32_t consumercount) {
// report the name of the temporary queue
std::cout << "declared queue " << name << std::endl;
// now we can close the connection
connection.close();
}).onError([](const char *message) {
// something went wrong creating the queue (or even before)
std::cerr << "declareQueue error: " << message << std::endl;
});
// run the handler
// at the moment, one will need SIGINT to stop. In time, should add signal handling through boost API.
return context.run();
}