summaryrefslogtreecommitdiff
path: root/cpp/examples
diff options
context:
space:
mode:
authorAlan Conway <aconway@apache.org>2008-05-06 17:52:03 +0000
committerAlan Conway <aconway@apache.org>2008-05-06 17:52:03 +0000
commit8d8a9162f7ba5a99a7c8b8b57aae860ab3028078 (patch)
treeaa17b9f56448a5280e13441a7b0473dd5faee283 /cpp/examples
parente4a3049c22b36171d204a8f84ddbcbf5accf797f (diff)
downloadqpid-python-8d8a9162f7ba5a99a7c8b8b57aae860ab3028078.tar.gz
From https://issues.apache.org/jira/browse/QPID-879 contributed by Jonathan Robie.
XML exchange allowing messages to be routed base on XQuery expressions. git-svn-id: https://svn.apache.org/repos/asf/incubator/qpid/trunk/qpid@653854 13f79535-47bb-0310-9956-ffa450edef68
Diffstat (limited to 'cpp/examples')
-rw-r--r--cpp/examples/Makefile.am9
-rw-r--r--cpp/examples/examples/xml-exchange/Makefile7
-rw-r--r--cpp/examples/examples/xml-exchange/README48
-rw-r--r--cpp/examples/examples/xml-exchange/declare_queues.cpp76
-rw-r--r--cpp/examples/examples/xml-exchange/listener.cpp90
-rw-r--r--cpp/examples/examples/xml-exchange/xml_producer.cpp88
6 files changed, 314 insertions, 4 deletions
diff --git a/cpp/examples/Makefile.am b/cpp/examples/Makefile.am
index 6d1d122744..54c59810ed 100644
--- a/cpp/examples/Makefile.am
+++ b/cpp/examples/Makefile.am
@@ -15,7 +15,11 @@ nobase_pkgdata_DATA= \
examples/direct/Makefile \
examples/direct/direct_producer.cpp \
examples/direct/listener.cpp \
- examples/direct/declare_queues.cpp
+ examples/direct/declare_queues.cpp \
+ examples/xml-exchange/Makefile \
+ examples/xml-exchange/declare_queues.cpp \
+ examples/xml-exchange/xml_producer.cpp \
+ examples/xml-exchange/listener.cpp
VERIFY_FILES= verify verify_all \
examples/request-response/verify \
@@ -60,9 +64,6 @@ all-local:
test -d examples || cp -R $(srcdir)/examples .
cd examples && $(MAKE) CXX="$(CXX)" CXXFLAGS="$(CXXFLAGS) -I$(abs_top_srcdir)/src -I$(abs_top_srcdir)/src/gen -I$(abs_top_builddir)/src -I$(abs_top_builddir)/src/gen -L$(abs_top_builddir)/src/.libs -Wl,-rpath,$(abs_top_builddir)/src/.libs" all
-# FIXME aconway 2008-03-25: Re-enable when python client has been fixed
-# to find .spec via PYTHONPATH.
-#
# Verify the examples in the buid tree.
check-local: all-local verify
$(srcdir)/verify_all $(abs_top_srcdir)/..
diff --git a/cpp/examples/examples/xml-exchange/Makefile b/cpp/examples/examples/xml-exchange/Makefile
new file mode 100644
index 0000000000..e598dd3be3
--- /dev/null
+++ b/cpp/examples/examples/xml-exchange/Makefile
@@ -0,0 +1,7 @@
+CXX=g++
+CXXFLAGS=
+LDFLAGS=-lqpidclient
+PROGRAMS=declare_queues xml_producer listener
+all: $(PROGRAMS)
+clean:
+ rm -f $(PROGRAMS)
diff --git a/cpp/examples/examples/xml-exchange/README b/cpp/examples/examples/xml-exchange/README
new file mode 100644
index 0000000000..26b9fac97a
--- /dev/null
+++ b/cpp/examples/examples/xml-exchange/README
@@ -0,0 +1,48 @@
+This example shows how to program a simple application
+using the XML Exchange.
+
+To run the example, execute the programs in the
+following order:
+
+1 ./declare_queues
+2 ./listener
+3 ./message_producer (in a separate window)
+
+The XML Exchange must be explicitly declared. Bindings
+are established using queries in XQuery. These queries
+can reference message content, message application
+properties (which are declared as external variables
+in the XQuery), or both.
+
+Once this is done, message producers publish to the
+exchange using the exchange name and a routing key,
+just as for other exchange types. Message consumers
+read from the queues to which messages are routed.
+If a message does not have XML content, or is
+missing message application properties needed by
+the query, the query is not routed.
+
+Queries can use message application headers to
+provide functionality similar to JMS selectors.
+If a query does not use the content of a message,
+the message content is not parsed, and need not
+be XML.
+
+The XQuery processor, XQilla, does path-based
+document projection, so once the portion of
+a document needed to evaluate a query has
+been read, it stops parsing the document.
+Suppose a long document has a header section.
+You can indicate in the query that only
+one header section needs to be queried,
+and there is no need to parse the entire
+document to see if there are further header
+sections, using a path like this:
+
+./message/header[1]/date
+
+If you used a path like this, all children
+of the message element would be read to
+see if there are further headers:
+
+./message/header/date
diff --git a/cpp/examples/examples/xml-exchange/declare_queues.cpp b/cpp/examples/examples/xml-exchange/declare_queues.cpp
new file mode 100644
index 0000000000..44e3de33f8
--- /dev/null
+++ b/cpp/examples/examples/xml-exchange/declare_queues.cpp
@@ -0,0 +1,76 @@
+/*
+ *
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ *
+ */
+
+#include <qpid/client/Connection.h>
+#include <qpid/client/Session.h>
+
+#include <unistd.h>
+#include <cstdlib>
+#include <iostream>
+
+using namespace qpid::client;
+using namespace qpid::framing;
+
+using std::string;
+
+
+int main(int argc, char** argv) {
+ const char* host = argc>1 ? argv[1] : "127.0.0.1";
+ int port = argc>2 ? atoi(argv[2]) : 5672;
+ Connection connection;
+ Message msg;
+ try {
+ connection.open(host, port);
+ Session session = connection.newSession(ASYNC);
+
+
+ //--------- Main body of program --------------------------------------------
+
+ // Set up queues, bind them with queries. Note that the XML exchange
+ // is not in the AMQP specification, so it is called "xml", not "amq.xml".
+ // Note that the XML exchange is not predeclared in Qpid, it must
+ // be declared by the application.
+
+ session.queueDeclare(arg::queue="message_queue");
+ session.exchangeDeclare(arg::exchange="xml", arg::type="xml");
+
+ // Application message properties are mapped to external variables
+ // in the XQuery. An XML Exchange can query message properties much
+ // like JMS, query the XML content of the message, or both.
+
+ FieldTable binding;
+ binding.setString("xquery", "declare variable $control external;"
+ "./message/id mod 2 = 1 or $control = 'end'");
+ session.exchangeBind(arg::exchange="xml", arg::queue="message_queue", arg::bindingKey="query_name", arg::arguments=binding);
+
+ //-----------------------------------------------------------------------------
+
+ connection.close();
+ return 0;
+ } catch(const std::exception& error) {
+ std::cout << error.what() << std::endl;
+ }
+ return 1;
+
+}
+
+
+
diff --git a/cpp/examples/examples/xml-exchange/listener.cpp b/cpp/examples/examples/xml-exchange/listener.cpp
new file mode 100644
index 0000000000..b5e057c4b1
--- /dev/null
+++ b/cpp/examples/examples/xml-exchange/listener.cpp
@@ -0,0 +1,90 @@
+/*
+ *
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ *
+ */
+
+/**
+ * listener.cpp: This program reads messages fro a queue on
+ * the broker using a message listener.
+ */
+
+#include <qpid/client/Dispatcher.h>
+#include <qpid/client/Connection.h>
+#include <qpid/client/Session.h>
+#include <qpid/client/Message.h>
+#include <qpid/client/SubscriptionManager.h>
+
+#include <unistd.h>
+#include <cstdlib>
+#include <iostream>
+
+using namespace qpid::client;
+using namespace qpid::framing;
+
+
+class Listener : public MessageListener{
+ private:
+ SubscriptionManager& subscriptions;
+ public:
+ Listener(SubscriptionManager& subscriptions);
+ virtual void received(Message& message);
+};
+
+Listener::Listener(SubscriptionManager& subs) : subscriptions(subs)
+{}
+
+void Listener::received(Message& message) {
+ std::cout << "Message: " << message.getData() << std::endl;
+ if (message.getHeaders().getString("control") == "end") {
+ std::cout << "Shutting down listener for " << message.getDestination()
+ << std::endl;
+ subscriptions.cancel(message.getDestination());
+ }
+}
+
+int main(int argc, char** argv) {
+ const char* host = argc>1 ? argv[1] : "127.0.0.1";
+ int port = argc>2 ? atoi(argv[2]) : 5672;
+ Connection connection;
+ Message msg;
+ try {
+ connection.open(host, port);
+ Session session = connection.newSession(ASYNC);
+
+ //--------- Main body of program --------------------------------------------
+
+ SubscriptionManager subscriptions(session);
+ // Create a listener and subscribe it to the queue named "message_queue"
+ Listener listener(subscriptions);
+ subscriptions.subscribe(listener, "message_queue");
+ // Deliver messages until the subscription is cancelled
+ // by Listener::received()
+ subscriptions.run();
+
+ //---------------------------------------------------------------------------
+
+ connection.close();
+ return 0;
+ } catch(const std::exception& error) {
+ std::cout << error.what() << std::endl;
+ }
+ return 1;
+}
+
+
diff --git a/cpp/examples/examples/xml-exchange/xml_producer.cpp b/cpp/examples/examples/xml-exchange/xml_producer.cpp
new file mode 100644
index 0000000000..9333e20438
--- /dev/null
+++ b/cpp/examples/examples/xml-exchange/xml_producer.cpp
@@ -0,0 +1,88 @@
+/*
+ *
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ *
+ */
+
+
+#include <qpid/client/Connection.h>
+#include <qpid/client/Session.h>
+#include <qpid/client/Message.h>
+
+
+#include <unistd.h>
+#include <cstdlib>
+#include <iostream>
+
+#include <sstream>
+
+using namespace qpid::client;
+using namespace qpid::framing;
+
+using std::stringstream;
+using std::string;
+
+int main(int argc, char** argv) {
+ const char* host = argc>1 ? argv[1] : "127.0.0.1";
+ int port = argc>2 ? atoi(argv[2]) : 5672;
+ Connection connection;
+ Message message;
+ try {
+ connection.open(host, port);
+ Session session = connection.newSession(ASYNC);
+
+ //--------- Main body of program --------------------------------------------
+
+ // Publish some XML messages. Use the control property to
+ // indicate when we are finished.
+ //
+ // In the XML exchange, the routing key and the name of
+ // the query match.
+
+ message.getDeliveryProperties().setRoutingKey("query_name");
+ message.getHeaders().setString("control","continue");
+
+ // Now send some messages ...
+
+ for (int i=0; i<10; i++) {
+ stringstream message_data;
+ message_data << "<message><id>" << i << "</id></message>";
+
+ std::cout << "Message data: " << message_data.str() << std::endl;
+
+ message.setData(message_data.str());
+ session.messageTransfer(arg::content=message, arg::destination="xml");
+ }
+
+ // And send a final message to indicate termination.
+
+ message.getHeaders().setString("control","end");
+ message.setData("<end>That's all, folks!</end>");
+ session.messageTransfer(arg::content=message, arg::destination="xml");
+
+ //-----------------------------------------------------------------------------
+
+ connection.close();
+ return 0;
+ } catch(const std::exception& error) {
+ std::cout << error.what() << std::endl;
+ }
+ return 1;
+}
+
+