From c436409fa83c900394edf0fc1756ac9b653bec29 Mon Sep 17 00:00:00 2001 From: Jonathan Robie Date: Thu, 15 Apr 2010 20:04:00 +0000 Subject: 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 --- cpp/examples/messaging/Makefile.am | 6 ++++- cpp/examples/messaging/hello_world.cpp | 49 ++++++++++++++++++++++++++++++++++ 2 files changed, 54 insertions(+), 1 deletion(-) create mode 100644 cpp/examples/messaging/hello_world.cpp (limited to 'cpp/examples/messaging') diff --git a/cpp/examples/messaging/Makefile.am b/cpp/examples/messaging/Makefile.am index 53a9e4773a..7e1ada0e2e 100644 --- a/cpp/examples/messaging/Makefile.am +++ b/cpp/examples/messaging/Makefile.am @@ -21,7 +21,10 @@ examplesdir=$(pkgdatadir)/examples/messaging MAKELDFLAGS=$(CLIENTFLAGS) include $(top_srcdir)/examples/makedist.mk -noinst_PROGRAMS=drain spout queue_sender queue_receiver topic_sender topic_receiver client server map_sender map_receiver +noinst_PROGRAMS=drain spout queue_sender queue_receiver topic_sender topic_receiver client server map_sender map_receiver hello_world + +hello_world_SOURCES=hello_world.cpp +hello_world_LDADD=$(CLIENT_LIB) drain_SOURCES=drain.cpp drain_LDADD=$(CLIENT_LIB) @@ -55,6 +58,7 @@ map_receiver_LDADD=$(CLIENT_LIB) EXTRA_DIST= \ CMakeLists.txt \ + messaging_hello_world.vcproj \ messaging_client.vcproj \ messaging_drain.vcproj \ messaging_map_receiver.vcproj \ 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 +#include +#include +#include +#include + +#include +#include + +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; + } +} -- cgit v1.2.1