|
| 1 | +/** |
| 2 | + * LibBoostAsio.cpp |
| 3 | + * |
| 4 | + * Test program to check AMQP functionality based on Boost's asio io_service. |
| 5 | + * |
| 6 | + * @author Gavin Smith <gavin.smith@coralbay.tv> |
| 7 | + * |
| 8 | + * Compile with g++ -std=c++14 libboostasio.cpp -o boost_test -lpthread -lboost_system -lamqpcpp |
| 9 | + */ |
| 10 | + |
| 11 | +/** |
| 12 | + * Dependencies |
| 13 | + */ |
| 14 | + |
| 15 | +#include <array> |
| 16 | +#include <chrono> |
| 17 | +#include <thread> |
| 18 | + |
| 19 | +#include <boost/asio/io_context.hpp> |
| 20 | +#include <boost/asio/ip/tcp.hpp> |
| 21 | +#include <boost/asio/connect.hpp> |
| 22 | +#include <boost/asio/signal_set.hpp> |
| 23 | + |
| 24 | +#include <amqpcpp.h> |
| 25 | +#include <amqpcpp/libboostasio.h> |
| 26 | + |
| 27 | + |
| 28 | +/** |
| 29 | + * Main program |
| 30 | + * @return int |
| 31 | + */ |
| 32 | +int main() |
| 33 | +{ |
| 34 | + using namespace std::chrono_literals; |
| 35 | + |
| 36 | + boost::asio::io_context io_context; |
| 37 | + |
| 38 | + boost::asio::signal_set signal_set{io_context, SIGINT, SIGTERM}; |
| 39 | + |
| 40 | + signal_set.async_wait([&io_context] (const boost::system::error_code& error, int signal_number) { |
| 41 | + std::cerr << "Got signal " << signal_number << ", terminating..." << std::endl; |
| 42 | + |
| 43 | + io_context.stop(); |
| 44 | + }); |
| 45 | + |
| 46 | + boost::asio::strand<boost::asio::io_context::executor_type> strand(io_context.get_executor()); |
| 47 | + |
| 48 | + const AMQP::Address address("amqp://guest:guest@localhost/"); |
| 49 | + |
| 50 | + boost::asio::ip::tcp::resolver resolver(io_context); |
| 51 | + boost::asio::ip::tcp::resolver::results_type endpoints = resolver.resolve(address.hostname(), address.secure() ? "amqps" : "amqp"); |
| 52 | + |
| 53 | + boost::asio::ip::tcp::socket socket(strand); |
| 54 | + boost::asio::connect(socket, endpoints); |
| 55 | + |
| 56 | + // make a connection |
| 57 | + AMQP::LibBoostAsioConnection connection(std::move(socket), address.login(), address.vhost()); |
| 58 | + |
| 59 | + std::array<AMQP::LibBoostAsioChannel, 4> channels {{ |
| 60 | + AMQP::LibBoostAsioChannel{&connection}, |
| 61 | + AMQP::LibBoostAsioChannel{&connection}, |
| 62 | + AMQP::LibBoostAsioChannel{&connection}, |
| 63 | + AMQP::LibBoostAsioChannel{&connection} |
| 64 | + }}; |
| 65 | + |
| 66 | + // create a temporary queue |
| 67 | + channels[0].declareQueue(AMQP::exclusive).onSuccess([&io_context, &strand, &connection, &channels](const std::string &name, uint32_t messagecount, uint32_t consumercount) { |
| 68 | + // report the name of the temporary queue |
| 69 | + std::cout << "declared queue " << name << std::endl; |
| 70 | + |
| 71 | + // fill the queue |
| 72 | + for (std::size_t i = 0; i < 100; ++i) { |
| 73 | + channels[0].publish("", name, std::to_string(i).c_str()); |
| 74 | + } |
| 75 | + |
| 76 | + for (auto& channel: channels) { |
| 77 | + channel.setQos(1).onSuccess([&io_context, &strand, &channel, name] () { |
| 78 | + channel.consume(name).onReceived([&io_context, &strand, &channel] (const AMQP::Message &message, uint64_t deliveryTag, bool redelivered) { |
| 79 | + std::cout << "delivery tag " << deliveryTag << " by " << channel.id() << " body " << std::string(message.body(), message.bodySize()) << std::endl; |
| 80 | + |
| 81 | + boost::asio::post(io_context, [&channel, &strand, deliveryTag] () { |
| 82 | + std::this_thread::sleep_for(1s); |
| 83 | + |
| 84 | + boost::asio::post(strand, [&channel, deliveryTag] () { |
| 85 | + channel.ack(deliveryTag); |
| 86 | + |
| 87 | + std::cout << "ack " << deliveryTag << " by " << channel.id() << std::endl; |
| 88 | + }); |
| 89 | + }); |
| 90 | + }); |
| 91 | + }); |
| 92 | + } |
| 93 | + }); |
| 94 | + |
| 95 | + const auto tf = [&io_context] () { |
| 96 | + io_context.run(); |
| 97 | + }; |
| 98 | + std::array<std::thread, 4> pool {{ |
| 99 | + std::thread{tf}, |
| 100 | + std::thread{tf}, |
| 101 | + std::thread{tf}, |
| 102 | + std::thread{tf} |
| 103 | + }}; |
| 104 | + |
| 105 | + for (auto& thread: pool) { |
| 106 | + thread.join(); |
| 107 | + } |
| 108 | + |
| 109 | + return 0; |
| 110 | +} |
| 111 | + |
0 commit comments