#include <qpid/messaging/Connection.h>
#include <qpid/messaging/Message.h>
#include <qpid/messaging/Message_io.h>
#include <qpid/messaging/Receiver.h>
#include <qpid/messaging/Session.h>
#include <iostream>
#include "OptionParser.h"
using namespace qpid::messaging;
using namespace qpid::types;
struct Options : OptionParser
{
std::string url;
std::string address;
std::string connectionOptions;
int timeout;
bool forever;
int count;
Options()
: OptionParser("Usage: drain [OPTIONS] ADDRESS", "Drains messages from the specified address"),
url("127.0.0.1"),
timeout(0),
forever(false),
count(0)
{
add("broker,b", url, "url of broker to connect to");
add("timeout,t", timeout, "timeout in seconds to wait before exiting");
add("forever,f", forever, "ignore timeout and wait forever");
add("connection-options", connectionOptions, "connection options string in the form {name1:value1, name2:value2}");
add("count,c", count, "number of messages to read before exiting");
}
Duration getTimeout()
{
if (forever) return Duration::FOREVER;
else return timeout*Duration::SECOND;
}
int getCount()
{
return count;
}
bool checkAddress()
{
if (getArguments().empty()) {
error("Address is required");
return false;
} else {
address = getArguments()[0];
return true;
}
}
};
int main(int argc, char** argv)
{
Options options;
if (options.parse(argc, argv) && options.checkAddress()) {
Connection connection;
try {
connection = Connection(options.url, options.connectionOptions);
connection.open();
Session session = connection.createSession();
Receiver receiver = session.createReceiver(options.address);
Duration timeout = options.getTimeout();
int count = options.getCount();
Message message;
int i = 0;
while (receiver.fetch(message, timeout)) {
std::cout << message << std::endl;
session.acknowledge();
if (count && (++i == count))
break;
}
receiver.close();
session.close();
connection.close();
return 0;
} catch(const std::exception& error) {
std::cout << "Error: " << error.what() << std::endl;
connection.close();
}
}
return 1;
}
Apache Qpid, Messaging built on AMQP; Copyright © 2015 The Apache Software Foundation; Licensed under the Apache License, Version 2.0; Apache Qpid, Qpid, Qpid Proton, Proton, Apache, the Apache feather logo, and the Apache Qpid project logo are trademarks of The Apache Software Foundation; All other marks mentioned may be trademarks or registered trademarks of their respective owners