diff options
author | Alan Conway <aconway@apache.org> | 2008-06-20 21:23:23 +0000 |
---|---|---|
committer | Alan Conway <aconway@apache.org> | 2008-06-20 21:23:23 +0000 |
commit | 11ccae93c7550828f7083a74f90a691769271b66 (patch) | |
tree | 3d4ef5541f1261daca012ab926ba3fdffcee7315 /cpp/examples/direct | |
parent | 7566d73d9c3e1226728ac3699d85ae0ad9715cee (diff) | |
download | qpid-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/direct')
-rw-r--r-- | cpp/examples/direct/Makefile.am | 29 | ||||
-rw-r--r-- | cpp/examples/direct/declare_queues.cpp | 86 | ||||
-rw-r--r-- | cpp/examples/direct/direct_producer.cpp | 109 | ||||
-rw-r--r-- | cpp/examples/direct/listener.cpp | 89 | ||||
-rw-r--r-- | cpp/examples/direct/verify | 3 | ||||
-rw-r--r-- | cpp/examples/direct/verify.in | 15 | ||||
-rw-r--r-- | cpp/examples/direct/verify_cpp_python | 4 | ||||
-rw-r--r-- | cpp/examples/direct/verify_cpp_python.in | 14 | ||||
-rw-r--r-- | cpp/examples/direct/verify_python_cpp | 5 | ||||
-rw-r--r-- | cpp/examples/direct/verify_python_cpp.in | 15 |
10 files changed, 369 insertions, 0 deletions
diff --git a/cpp/examples/direct/Makefile.am b/cpp/examples/direct/Makefile.am new file mode 100644 index 0000000000..7a0cf65968 --- /dev/null +++ b/cpp/examples/direct/Makefile.am @@ -0,0 +1,29 @@ +examplesdir=$(pkgdatadir)/examples/direct + +include $(top_srcdir)/examples/makedist.mk + +noinst_PROGRAMS=direct_producer listener declare_queues +direct_producer_SOURCES=direct_producer.cpp +direct_producer_LDADD=$(CLIENT_LIB) + +listener_SOURCES=listener.cpp +listener_LDADD=$(CLIENT_LIB) + +declare_queues_SOURCES=declare_queues.cpp +declare_queues_LDADD=$(CLIENT_LIB) + +examples_DATA= \ + direct_producer.cpp \ + listener.cpp \ + declare_queues.cpp \ + $(MAKEDIST) + +EXTRA_DIST= \ + $(examples_DATA) \ + verify \ + verify.in \ + verify_cpp_python \ + verify_cpp_python.in \ + verify_python_cpp \ + verify_python_cpp.in + diff --git a/cpp/examples/direct/declare_queues.cpp b/cpp/examples/direct/declare_queues.cpp new file mode 100644 index 0000000000..0cdb472665 --- /dev/null +++ b/cpp/examples/direct/declare_queues.cpp @@ -0,0 +1,86 @@ +/* + * + * 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. + * + */ + +/** + * declare_queues.cpp + * + * This program is one of three programs designed to be used + * together. These programs use the "amq.direct" exchange. + * + * direct_config_queues.cpp (this program): + * + * Creates a queue on a broker, binding a routing key to route + * messages to that queue. + * + * direct_producer.cpp: + * + * Publishes to a broker, specifying a routing key. + * + * listener.cpp + * + * Reads from a queue on the broker using a message listener. + * + */ + +#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 -------------------------------------------- + + // Create a queue named "message_queue", and route all messages whose + // routing key is "routing_key" to this newly created queue. + + session.queueDeclare(arg::queue="message_queue"); + session.exchangeBind(arg::exchange="amq.direct", arg::queue="message_queue", arg::bindingKey="routing_key"); + + //----------------------------------------------------------------------------- + + connection.close(); + return 0; + } catch(const std::exception& error) { + std::cout << error.what() << std::endl; + } + return 1; + +} + + + diff --git a/cpp/examples/direct/direct_producer.cpp b/cpp/examples/direct/direct_producer.cpp new file mode 100644 index 0000000000..40fc644bf3 --- /dev/null +++ b/cpp/examples/direct/direct_producer.cpp @@ -0,0 +1,109 @@ +/* + * + * 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. + * + */ + + +/** + * direct_producer.cpp: + * + * This program is one of three programs designed to be used + * together. These programs do not specify the exchange type - the + * default exchange type is the direct exchange. + * + * create_queues.cpp: + * + * Creates a queue on a broker, binding a routing key to route + * messages to that queue. + * + * direct_producer.cpp (this program): + * + * Publishes to a broker, specifying a routing key. + * + * listener.cpp + * + * Reads from a queue on the broker using a message listener. + * + */ + + +#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 -------------------------------------------- + + // The routing key is a message property. We will use the same + // routing key for each message, so we'll set this property + // just once. (In most simple cases, there is no need to set + // other message properties.) + + message.getDeliveryProperties().setRoutingKey("routing_key"); + + // Now send some messages ... + + for (int i=0; i<10; i++) { + stringstream message_data; + message_data << "Message " << i; + + 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="amq.direct"); + } + + // And send a final message to indicate termination. + + message.setData("That's all, folks!"); + session.messageTransfer(arg::content=message, arg::destination="amq.direct"); + + //----------------------------------------------------------------------------- + + connection.close(); + return 0; + } catch(const std::exception& error) { + std::cout << error.what() << std::endl; + } + return 1; +} + + diff --git a/cpp/examples/direct/listener.cpp b/cpp/examples/direct/listener.cpp new file mode 100644 index 0000000000..d199b5c0bb --- /dev/null +++ b/cpp/examples/direct/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 from 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.getData() == "That's all, folks!") { + 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/direct/verify b/cpp/examples/direct/verify new file mode 100644 index 0000000000..ac0464ef80 --- /dev/null +++ b/cpp/examples/direct/verify @@ -0,0 +1,3 @@ +# See https://svn.apache.org/repos/asf/incubator/qpid/trunk/qpid/bin/verify +clients ./declare_queues ./direct_producer ./listener +outputs ./declare_queues.out ./direct_producer.out ./listener.out diff --git a/cpp/examples/direct/verify.in b/cpp/examples/direct/verify.in new file mode 100644 index 0000000000..d1e95f1151 --- /dev/null +++ b/cpp/examples/direct/verify.in @@ -0,0 +1,15 @@ +==== declare_queues.out +==== direct_producer.out +==== listener.out +Message: Message 0 +Message: Message 1 +Message: Message 2 +Message: Message 3 +Message: Message 4 +Message: Message 5 +Message: Message 6 +Message: Message 7 +Message: Message 8 +Message: Message 9 +Message: That's all, folks! +Shutting down listener for message_queue diff --git a/cpp/examples/direct/verify_cpp_python b/cpp/examples/direct/verify_cpp_python new file mode 100644 index 0000000000..4dc445ba27 --- /dev/null +++ b/cpp/examples/direct/verify_cpp_python @@ -0,0 +1,4 @@ +# See https://svn.apache.org/repos/asf/incubator/qpid/trunk/qpid/bin/verify +py=$PYTHON_EXAMPLES/direct +clients ./declare_queues ./direct_producer $py/direct_consumer.py +outputs ./declare_queues.out ./direct_producer.out $py/direct_consumer.py.out diff --git a/cpp/examples/direct/verify_cpp_python.in b/cpp/examples/direct/verify_cpp_python.in new file mode 100644 index 0000000000..1a329be59a --- /dev/null +++ b/cpp/examples/direct/verify_cpp_python.in @@ -0,0 +1,14 @@ +==== declare_queues.out +==== direct_producer.out +==== direct_consumer.py.out +Message 0 +Message 1 +Message 2 +Message 3 +Message 4 +Message 5 +Message 6 +Message 7 +Message 8 +Message 9 +That's all, folks! diff --git a/cpp/examples/direct/verify_python_cpp b/cpp/examples/direct/verify_python_cpp new file mode 100644 index 0000000000..fe4893e120 --- /dev/null +++ b/cpp/examples/direct/verify_python_cpp @@ -0,0 +1,5 @@ +# See https://svn.apache.org/repos/asf/incubator/qpid/trunk/qpid/bin/verify +py=$PYTHON_EXAMPLES/direct +clients $py/declare_queues.py $py/direct_producer.py ./listener +outputs $py/declare_queues.py.out $py/direct_producer.py.out ./listener.out + diff --git a/cpp/examples/direct/verify_python_cpp.in b/cpp/examples/direct/verify_python_cpp.in new file mode 100644 index 0000000000..6f35255b18 --- /dev/null +++ b/cpp/examples/direct/verify_python_cpp.in @@ -0,0 +1,15 @@ +==== declare_queues.py.out +==== direct_producer.py.out +==== listener.out +Message: message 0 +Message: message 1 +Message: message 2 +Message: message 3 +Message: message 4 +Message: message 5 +Message: message 6 +Message: message 7 +Message: message 8 +Message: message 9 +Message: That's all, folks! +Shutting down listener for message_queue |