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 | b7b7d01af76780a87d0efff2a6e0485666b54870 (patch) | |
tree | 3cab12b398ce774e59dda3d782b01626bbf7a931 /qpid/cpp/examples/messaging/hello_world.cpp | |
parent | e646f6b4797f70ba7b07df073f4ec6183d4b636d (diff) | |
download | qpid-python-b7b7d01af76780a87d0efff2a6e0485666b54870.tar.gz |
Added hello_world.cpp example for tutorial.
git-svn-id: https://svn.apache.org/repos/asf/qpid/trunk@934573 13f79535-47bb-0310-9956-ffa450edef68
Diffstat (limited to 'qpid/cpp/examples/messaging/hello_world.cpp')
-rw-r--r-- | qpid/cpp/examples/messaging/hello_world.cpp | 49 |
1 files changed, 49 insertions, 0 deletions
diff --git a/qpid/cpp/examples/messaging/hello_world.cpp b/qpid/cpp/examples/messaging/hello_world.cpp new file mode 100644 index 0000000000..f861a02d97 --- /dev/null +++ b/qpid/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; + } +} |