diff options
author | Jonathan Robie <jonathan@apache.org> | 2010-04-15 20:04:00 +0000 |
---|---|---|
committer | Jonathan Robie <jonathan@apache.org> | 2010-04-15 20:04:00 +0000 |
commit | c436409fa83c900394edf0fc1756ac9b653bec29 (patch) | |
tree | 19e680ff0170dc3985a1d210db80051a18f44eb3 /cpp/examples/messaging/hello_world.cpp | |
parent | 7836498914eab08ec0eb7b10824deaa4d78db976 (diff) | |
download | qpid-python-c436409fa83c900394edf0fc1756ac9b653bec29.tar.gz |
Added hello_world.cpp example for tutorial.
git-svn-id: https://svn.apache.org/repos/asf/qpid/trunk/qpid@934573 13f79535-47bb-0310-9956-ffa450edef68
Diffstat (limited to 'cpp/examples/messaging/hello_world.cpp')
-rw-r--r-- | cpp/examples/messaging/hello_world.cpp | 49 |
1 files changed, 49 insertions, 0 deletions
diff --git a/cpp/examples/messaging/hello_world.cpp b/cpp/examples/messaging/hello_world.cpp new file mode 100644 index 0000000000..f861a02d97 --- /dev/null +++ b/cpp/examples/messaging/hello_world.cpp @@ -0,0 +1,49 @@ +#include <qpid/messaging/Connection.h> +#include <qpid/messaging/Message.h> +#include <qpid/messaging/Receiver.h> +#include <qpid/messaging/Sender.h> +#include <qpid/messaging/Session.h> + +#include <iostream> +#include <sstream> + +using namespace qpid::messaging; + +int main() { + + Connection connection("localhost:5672"); + try { + connection.open(); + Session session = connection.createSession(); + + std::string queueName = "message_queue"; + + Sender sender = session.createSender(queueName); + + for (int i=0; i<5; i++) { + std::stringstream content; + content << "Message " << i; + std::cout << "Sending " << content.str() << std::endl; + sender.send(Message(content.str())); + } + sender.close(); + + Receiver receiver = session.createReceiver(queueName); + + Message message; + Duration timeout(1000); /* in milliseconds */ + while (receiver.fetch(message, timeout)) { + std::cout << "Received " << message.getContent() << std::endl; + session.acknowledge(); + } + + receiver.close(); + + connection.close(); + return 0; + } catch(const std::exception& error) { + std::cerr << error.what() << std::endl; + connection.close(); + return 1; + } +} |