summaryrefslogtreecommitdiff
path: root/cpp/examples/xml-exchange
diff options
context:
space:
mode:
authorAlan Conway <aconway@apache.org>2008-06-20 21:23:23 +0000
committerAlan Conway <aconway@apache.org>2008-06-20 21:23:23 +0000
commit11ccae93c7550828f7083a74f90a691769271b66 (patch)
tree3d4ef5541f1261daca012ab926ba3fdffcee7315 /cpp/examples/xml-exchange
parent7566d73d9c3e1226728ac3699d85ae0ad9715cee (diff)
downloadqpid-python-11ccae93c7550828f7083a74f90a691769271b66.tar.gz
Patch from Manuel Teira: https://issues.apache.org/jira/secure/CommentAssignIssue!default.jspa?action=5&id=12398038
- Use standard automake makefiles to build cpp/examples. - Rationalize examples directory structure. Additions to patch: - Fix for VPATH builds. - Fix make distcheck (finally!) git-svn-id: https://svn.apache.org/repos/asf/incubator/qpid/trunk/qpid@670066 13f79535-47bb-0310-9956-ffa450edef68
Diffstat (limited to 'cpp/examples/xml-exchange')
-rw-r--r--cpp/examples/xml-exchange/Makefile.am29
-rw-r--r--cpp/examples/xml-exchange/README48
-rw-r--r--cpp/examples/xml-exchange/declare_queues.cpp76
-rw-r--r--cpp/examples/xml-exchange/listener.cpp89
-rw-r--r--cpp/examples/xml-exchange/xml_producer.cpp91
5 files changed, 333 insertions, 0 deletions
diff --git a/cpp/examples/xml-exchange/Makefile.am b/cpp/examples/xml-exchange/Makefile.am
new file mode 100644
index 0000000000..1b25a8750d
--- /dev/null
+++ b/cpp/examples/xml-exchange/Makefile.am
@@ -0,0 +1,29 @@
+examplesdir=$(pkgdatadir)/examples/xml-exchange
+
+include $(top_srcdir)/examples/makedist.mk
+
+noinst_PROGRAMS=declare_queues xml_producer listener
+
+declare_queues_SOURCES=declare_queues.cpp
+declare_queues_LDADD=$(CLIENT_LIB)
+
+xml_producer_SOURCES=xml_producer.cpp
+xml_producer_LDADD=$(CLIENT_LIB)
+
+listener_SOURCES=listener.cpp
+listener_LDADD=$(CLIENT_LIB)
+
+EXTRA_DIST= \
+ README
+
+examples_DATA= \
+ $(EXTRA_DIST) \
+ declare_queues.cpp \
+ listener.cpp \
+ xml_producer.cpp \
+ $(MAKEDIST)
+
+
+
+
+
diff --git a/cpp/examples/xml-exchange/README b/cpp/examples/xml-exchange/README
new file mode 100644
index 0000000000..26b9fac97a
--- /dev/null
+++ b/cpp/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/xml-exchange/declare_queues.cpp b/cpp/examples/xml-exchange/declare_queues.cpp
new file mode 100644
index 0000000000..1307c473c5
--- /dev/null
+++ b/cpp/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;
+
+ try {
+ connection.open(host, port);
+ Session session = connection.newSession();
+
+
+ //--------- 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/xml-exchange/listener.cpp b/cpp/examples/xml-exchange/listener.cpp
new file mode 100644
index 0000000000..98646bea95
--- /dev/null
+++ b/cpp/examples/xml-exchange/listener.cpp
@@ -0,0 +1,89 @@
+/*
+ *
+ * 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/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();
+
+ //--------- 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");
+ // Receive 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/xml-exchange/xml_producer.cpp b/cpp/examples/xml-exchange/xml_producer.cpp
new file mode 100644
index 0000000000..19889e4891
--- /dev/null
+++ b/cpp/examples/xml-exchange/xml_producer.cpp
@@ -0,0 +1,91 @@
+/*
+ *
+ * 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/AsyncSession.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();
+
+ //--------- 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());
+ // Asynchronous transfer sends messages as quickly as
+ // possible without waiting for confirmation.
+ async(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;
+}
+
+