Return-Path: Delivered-To: apmail-activemq-commits-archive@www.apache.org Received: (qmail 88258 invoked from network); 26 Oct 2007 16:06:41 -0000 Received: from hermes.apache.org (HELO mail.apache.org) (140.211.11.2) by minotaur.apache.org with SMTP; 26 Oct 2007 16:06:41 -0000 Received: (qmail 57099 invoked by uid 500); 26 Oct 2007 16:06:20 -0000 Delivered-To: apmail-activemq-commits-archive@activemq.apache.org Received: (qmail 57069 invoked by uid 500); 26 Oct 2007 16:06:20 -0000 Mailing-List: contact commits-help@activemq.apache.org; run by ezmlm Precedence: bulk List-Help: List-Unsubscribe: List-Post: List-Id: Reply-To: dev@activemq.apache.org Delivered-To: mailing list commits@activemq.apache.org Received: (qmail 57056 invoked by uid 99); 26 Oct 2007 16:06:20 -0000 Received: from athena.apache.org (HELO athena.apache.org) (140.211.11.136) by apache.org (qpsmtpd/0.29) with ESMTP; Fri, 26 Oct 2007 09:06:20 -0700 X-ASF-Spam-Status: No, hits=-100.0 required=10.0 tests=ALL_TRUSTED X-Spam-Check-By: apache.org Received: from [140.211.11.3] (HELO eris.apache.org) (140.211.11.3) by apache.org (qpsmtpd/0.29) with ESMTP; Fri, 26 Oct 2007 16:06:21 +0000 Received: by eris.apache.org (Postfix, from userid 65534) id 87EA71A9832; Fri, 26 Oct 2007 09:06:01 -0700 (PDT) Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit Subject: svn commit: r588694 - in /activemq/activemq-cpp/trunk/src/examples: Makefile.am consumers/SimpleAsyncConsumer.cpp Date: Fri, 26 Oct 2007 16:06:00 -0000 To: commits@activemq.apache.org From: tabish@apache.org X-Mailer: svnmailer-1.0.8 Message-Id: <20071026160601.87EA71A9832@eris.apache.org> X-Virus-Checked: Checked by ClamAV on apache.org Author: tabish Date: Fri Oct 26 09:05:58 2007 New Revision: 588694 URL: http://svn.apache.org/viewvc?rev=588694&view=rev Log: Adding more examples for the CPP client, this is a simple async consumer example. Added: activemq/activemq-cpp/trunk/src/examples/consumers/SimpleAsyncConsumer.cpp (with props) Modified: activemq/activemq-cpp/trunk/src/examples/Makefile.am Modified: activemq/activemq-cpp/trunk/src/examples/Makefile.am URL: http://svn.apache.org/viewvc/activemq/activemq-cpp/trunk/src/examples/Makefile.am?rev=588694&r1=588693&r2=588694&view=diff ============================================================================== --- activemq/activemq-cpp/trunk/src/examples/Makefile.am (original) +++ activemq/activemq-cpp/trunk/src/examples/Makefile.am Fri Oct 26 09:05:58 2007 @@ -15,7 +15,7 @@ # limitations under the License. # --------------------------------------------------------------------------- -cc_sources = main.cpp +main_example_sources = main.cpp ## ## Compiler / Linker Info @@ -23,7 +23,13 @@ INCLUDES = -I$(srcdir)/../main -bin_PROGRAMS = example -example_SOURCES = $(cc_sources) - +## Main Example +bin_PROGRAMS = example +example_SOURCES = $(main_example_sources) example_LDADD= ../main/libactivemq-cpp.la + +## Simple Consumer +simple_async_consumer_sources = ./consumers/SimpleAsyncConsumer.cpp +bin_PROGRAMS += simple_async_consumer +simple_async_consumer_SOURCES = $(simple_async_consumer_sources) +simple_async_consumer_LDADD= ../main/libactivemq-cpp.la Added: activemq/activemq-cpp/trunk/src/examples/consumers/SimpleAsyncConsumer.cpp URL: http://svn.apache.org/viewvc/activemq/activemq-cpp/trunk/src/examples/consumers/SimpleAsyncConsumer.cpp?rev=588694&view=auto ============================================================================== --- activemq/activemq-cpp/trunk/src/examples/consumers/SimpleAsyncConsumer.cpp (added) +++ activemq/activemq-cpp/trunk/src/examples/consumers/SimpleAsyncConsumer.cpp Fri Oct 26 09:05:58 2007 @@ -0,0 +1,231 @@ +/* + * 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 +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +using namespace activemq; +using namespace activemq::core; +using namespace activemq::util; +using namespace activemq::concurrent; +using namespace cms; +using namespace std; + +//////////////////////////////////////////////////////////////////////////////// +class SimpleAsyncConsumer : public ExceptionListener, + public MessageListener { +private: + + Connection* connection; + Session* session; + Destination* destination; + MessageConsumer* consumer; + bool useTopic; + std::string brokerURI; + std::string destURI; + +public: + + SimpleAsyncConsumer( const std::string& brokerURI, + const std::string& destURI, + bool useTopic = false ) { + connection = NULL; + session = NULL; + destination = NULL; + consumer = NULL; + this->useTopic = useTopic; + this->brokerURI = brokerURI; + this->destURI = destURI; + } + + virtual ~SimpleAsyncConsumer(){ + cleanup(); + } + + void runConsumer() { + + try { + + // Create a ConnectionFactory + ActiveMQConnectionFactory* connectionFactory = + new ActiveMQConnectionFactory( brokerURI ); + + // Create a Connection + connection = connectionFactory->createConnection(); + delete connectionFactory; + connection->start(); + + connection->setExceptionListener(this); + + // Create a Session + session = connection->createSession( Session::AUTO_ACKNOWLEDGE ); + + // Create the destination (Topic or Queue) + if( useTopic ) { + destination = session->createTopic( destURI ); + } else { + destination = session->createQueue( destURI ); + } + + // Create a MessageConsumer from the Session to the Topic or Queue + consumer = session->createConsumer( destination ); + consumer->setMessageListener( this ); + + } catch (CMSException& e) { + e.printStackTrace(); + } + } + + // Called from the consumer since this class is a registered MessageListener. + virtual void onMessage( const Message* message ){ + + static int count = 0; + + try + { + count++; + const TextMessage* textMessage = + dynamic_cast< const TextMessage* >( message ); + string text = ""; + + if( textMessage != NULL ) { + text = textMessage->getText(); + } else { + text = "NOT A TEXTMESSAGE!"; + } + + printf( "Message #%d Received: %s\n", count, text.c_str() ); + } catch (CMSException& e) { + e.printStackTrace(); + } + } + + // If something bad happens you see it here as this class is also been + // registered as an ExceptionListener with the connection. + virtual void onException( const CMSException& ex AMQCPP_UNUSED) { + printf("CMS Exception occured. Shutting down client.\n"); + } + +private: + + void cleanup(){ + + //************************************************* + // Always close destination, consumers and producers before + // you destroy their sessions and connection. + //************************************************* + + // Destroy resources. + try{ + if( destination != NULL ) delete destination; + }catch (CMSException& e) { e.printStackTrace(); } + destination = NULL; + + try{ + if( consumer != NULL ) delete consumer; + }catch (CMSException& e) { e.printStackTrace(); } + consumer = NULL; + + // Close open resources. + try{ + if( session != NULL ) session->close(); + if( connection != NULL ) connection->close(); + }catch (CMSException& e) { e.printStackTrace(); } + + // Now Destroy them + try{ + if( session != NULL ) delete session; + }catch (CMSException& e) { e.printStackTrace(); } + session = NULL; + + try{ + if( connection != NULL ) delete connection; + }catch (CMSException& e) { e.printStackTrace(); } + connection = NULL; + } +}; + +//////////////////////////////////////////////////////////////////////////////// +int main(int argc AMQCPP_UNUSED, char* argv[] AMQCPP_UNUSED) { + + std::cout << "=====================================================\n"; + std::cout << "Starting the example:" << std::endl; + std::cout << "-----------------------------------------------------\n"; + + // Set the URI to point to the IPAddress of your broker. + // add any optional params to the url to enable things like + // tightMarshalling or tcp logging etc. See the CMS website for + // a full list of configuration options. + // + // http://activemq.apache.org/cms/ + // + // Wire Foormat Options: + // ===================== + // Use either stomp or openwire, the default ports are different for each + // + // Examples: + // tcp://127.0.0.1:61616 default to openwire + // tcp://127.0.0.1:61616?wireFormat=openwire same as above + // tcp://127.0.0.1:61613?wireFormat=stomp use stomp instead + // + std::string brokerURI = + "tcp://127.0.0.1:61616" + "?wireFormat=openwire" + "&transport.useAsyncSend=true"; +// "&transport.commandTracingEnabled=true" +// "&transport.tcpTracingEnabled=true"; +// "&wireFormat.tightEncodingEnabled=true"; + + + //============================================================ + // This is the Destination Name and URI options. Use this to + // customize where the consumer listens, to have the consumer + // use a topic or queue set the 'useTopics' flag. + //============================================================ + std::string destURI = "TEST.FOO"; + + //============================================================ + // set to true to use topics instead of queues + // Note in the code above that this causes createTopic or + // createQueue to be used in both consumer an producer. + //============================================================ + bool useTopics = true; + + // Create the consumer + SimpleAsyncConsumer consumer( brokerURI, destURI, useTopics ); + + // Start it up and it will listen forever. + consumer.runConsumer(); + + // Wait to exit. + std::cout << "Press 'q' to quit" << std::endl; + while( std::cin.get() != 'q') {} +} Propchange: activemq/activemq-cpp/trunk/src/examples/consumers/SimpleAsyncConsumer.cpp ------------------------------------------------------------------------------ svn:eol-style = native