You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@activemq.apache.org by ta...@apache.org on 2010/09/20 23:43:45 UTC

svn commit: r999128 - in /activemq/activemq-cpp/trunk/activemq-cpp/src/examples: Makefile.am cmstemplate/ cmstemplate/CMSTemplateReceiver.cpp cmstemplate/CMSTemplateSender.cpp

Author: tabish
Date: Mon Sep 20 21:43:44 2010
New Revision: 999128

URL: http://svn.apache.org/viewvc?rev=999128&view=rev
Log:
Adapt some CMSTemplate code to use as examples of CMSTemplate send and receiver for demo and testing purposes.

Added:
    activemq/activemq-cpp/trunk/activemq-cpp/src/examples/cmstemplate/
    activemq/activemq-cpp/trunk/activemq-cpp/src/examples/cmstemplate/CMSTemplateReceiver.cpp   (with props)
    activemq/activemq-cpp/trunk/activemq-cpp/src/examples/cmstemplate/CMSTemplateSender.cpp   (with props)
Modified:
    activemq/activemq-cpp/trunk/activemq-cpp/src/examples/Makefile.am

Modified: activemq/activemq-cpp/trunk/activemq-cpp/src/examples/Makefile.am
URL: http://svn.apache.org/viewvc/activemq/activemq-cpp/trunk/activemq-cpp/src/examples/Makefile.am?rev=999128&r1=999127&r2=999128&view=diff
==============================================================================
--- activemq/activemq-cpp/trunk/activemq-cpp/src/examples/Makefile.am (original)
+++ activemq/activemq-cpp/trunk/activemq-cpp/src/examples/Makefile.am Mon Sep 20 21:43:44 2010
@@ -48,6 +48,20 @@ chat_SOURCES = $(chat_sources)
 chat_LDADD= $(AMQ_TEST_LIBS)
 chat_CXXFLAGS = $(AMQ_TEST_CXXFLAGS) -I$(srcdir)/../main
 
+## CMSTemplate Message send Sample
+cmstemplate_sender_sources = ./cmstemplate/CMSTemplateSender.cpp
+noinst_PROGRAMS += cmstemplate_sender
+cmstemplate_sender_SOURCES = $(cmstemplate_sender_sources)
+cmstemplate_sender_LDADD= $(AMQ_TEST_LIBS)
+cmstemplate_sender_CXXFLAGS = $(AMQ_TEST_CXXFLAGS) -I$(srcdir)/../main
+
+## CMSTemplate Message receive Sample
+cmstemplate_receiver_sources = ./cmstemplate/CMSTemplateReceiver.cpp
+noinst_PROGRAMS += cmstemplate_receiver
+cmstemplate_receiver_SOURCES = $(cmstemplate_receiver_sources)
+cmstemplate_receiver_LDADD= $(AMQ_TEST_LIBS)
+cmstemplate_receiver_CXXFLAGS = $(AMQ_TEST_CXXFLAGS) -I$(srcdir)/../main
+
 ## Advisory Producer Example
 advisory_producer_sources = advisories/AdvisoryProducer.cpp \
                             advisories/AdvisoryProducerMain.cpp

Added: activemq/activemq-cpp/trunk/activemq-cpp/src/examples/cmstemplate/CMSTemplateReceiver.cpp
URL: http://svn.apache.org/viewvc/activemq/activemq-cpp/trunk/activemq-cpp/src/examples/cmstemplate/CMSTemplateReceiver.cpp?rev=999128&view=auto
==============================================================================
--- activemq/activemq-cpp/trunk/activemq-cpp/src/examples/cmstemplate/CMSTemplateReceiver.cpp (added)
+++ activemq/activemq-cpp/trunk/activemq-cpp/src/examples/cmstemplate/CMSTemplateReceiver.cpp Mon Sep 20 21:43:44 2010
@@ -0,0 +1,151 @@
+/*
+ * 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 <decaf/lang/Thread.h>
+#include <decaf/lang/Runnable.h>
+#include <decaf/util/concurrent/CountDownLatch.h>
+#include <decaf/lang/Long.h>
+#include <decaf/util/Date.h>
+#include <activemq/core/ActiveMQConnectionFactory.h>
+#include <activemq/util/Config.h>
+#include <activemq/library/ActiveMQCPP.h>
+#include <activemq/cmsutil/CmsTemplate.h>
+#include <activemq/cmsutil/MessageCreator.h>
+#include <cms/Connection.h>
+#include <cms/Session.h>
+#include <cms/TextMessage.h>
+#include <cms/BytesMessage.h>
+#include <cms/MapMessage.h>
+#include <cms/ExceptionListener.h>
+#include <cms/MessageListener.h>
+#include <stdlib.h>
+#include <stdio.h>
+#include <iostream>
+#include <memory>
+
+using namespace activemq;
+using namespace activemq::core;
+using namespace activemq::cmsutil;
+using namespace decaf::lang;
+using namespace decaf::util;
+using namespace decaf::util::concurrent;
+using namespace cms;
+using namespace std;
+
+////////////////////////////////////////////////////////////////////////////////
+namespace {
+
+    class Receiver: Runnable {
+    private:
+
+        string name;
+        CountDownLatch ready;
+        bool isClosing;
+        CmsTemplate* cmsTemplate;
+        Thread* asyncReceiverThread;
+
+    public:
+
+        Receiver( string name, CmsTemplate* cmsTemplate ) :
+            name(name), ready( 1 ), isClosing( false ), cmsTemplate( cmsTemplate ) {
+
+            asyncReceiverThread = new Thread( this, "AsyncReceiver" );
+            asyncReceiverThread->start();
+
+            waitUntilReady();
+        }
+
+        virtual ~Receiver() {
+
+            isClosing = true;
+
+            //wait for the m_asyncReceiverThread to terminate
+            if( asyncReceiverThread ) {
+                asyncReceiverThread->join();
+                delete asyncReceiverThread;
+            }
+
+            delete cmsTemplate;
+        }
+
+        virtual void run() {
+
+            ready.countDown();
+            while( !isClosing ) {
+                receiveMessage();
+            }
+        }
+
+    private:
+
+        /**
+         * to reveive message from a destination synchronously
+         * Note: If parameter retryOnError is true, then this function will retry on errors
+         *       as long as the receiver's timeout has not been reached. Otherwise, this function will not retry on errors.
+         */
+        void receiveMessage() {
+
+            try {
+
+                Message* cmsMessage = cmsTemplate->receive();
+                if( cmsMessage ) {
+                    printf( "%s received a message\n", name.c_str() );
+                    delete cmsMessage;
+                }
+
+            } catch( cms::CMSException& ex ) {
+                std::cout << "Caught CMSException: ";
+                std::cout << ex.getMessage().c_str() << endl;
+                Thread::sleep( 10000 );
+            }
+        }
+
+        void waitUntilReady() {
+            ready.await();
+        }
+
+    };
+}
+
+////////////////////////////////////////////////////////////////////////////////
+int main( int argc AMQCPP_UNUSED, char* argv[] AMQCPP_UNUSED ) {
+
+    std::cout << "=====================================================\n";
+    std::cout << "Starting the MessageListener application..." << std::endl;
+
+    //initialize ActiveMQCPP library
+    activemq::library::ActiveMQCPP::initializeLibrary();
+
+    ActiveMQConnectionFactory* connectionFactory =
+        new ActiveMQConnectionFactory( "failover://(tcp://127.0.0.1:61616)" );
+
+    CmsTemplate* cmsTemplate = new CmsTemplate( connectionFactory );
+    cmsTemplate->setDefaultDestinationName( "CMSTemplateExamples" );
+    cmsTemplate->setPubSubDomain( true );
+    cmsTemplate->setReceiveTimeout( 2000 );
+    Receiver* receiver = new Receiver( "MessageListener", cmsTemplate );
+
+    std::cout << "Click any char to terminate the application..." << std::endl;
+    getchar();
+
+    //shut down ActiveMQCPP library
+    delete connectionFactory;
+    delete receiver;
+
+    activemq::library::ActiveMQCPP::shutdownLibrary();
+
+}

Propchange: activemq/activemq-cpp/trunk/activemq-cpp/src/examples/cmstemplate/CMSTemplateReceiver.cpp
------------------------------------------------------------------------------
    svn:eol-style = native

Added: activemq/activemq-cpp/trunk/activemq-cpp/src/examples/cmstemplate/CMSTemplateSender.cpp
URL: http://svn.apache.org/viewvc/activemq/activemq-cpp/trunk/activemq-cpp/src/examples/cmstemplate/CMSTemplateSender.cpp?rev=999128&view=auto
==============================================================================
--- activemq/activemq-cpp/trunk/activemq-cpp/src/examples/cmstemplate/CMSTemplateSender.cpp (added)
+++ activemq/activemq-cpp/trunk/activemq-cpp/src/examples/cmstemplate/CMSTemplateSender.cpp Mon Sep 20 21:43:44 2010
@@ -0,0 +1,119 @@
+/*
+ * 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 <decaf/lang/Thread.h>
+#include <decaf/lang/Runnable.h>
+#include <decaf/util/concurrent/CountDownLatch.h>
+#include <decaf/lang/Long.h>
+#include <decaf/util/Date.h>
+#include <activemq/core/ActiveMQConnectionFactory.h>
+#include <activemq/util/Config.h>
+#include <activemq/library/ActiveMQCPP.h>
+#include <activemq/cmsutil/CmsTemplate.h>
+#include <activemq/cmsutil/MessageCreator.h>
+#include <cms/Connection.h>
+#include <cms/Session.h>
+#include <cms/TextMessage.h>
+#include <cms/BytesMessage.h>
+#include <cms/MapMessage.h>
+#include <cms/ExceptionListener.h>
+#include <cms/MessageListener.h>
+#include <stdlib.h>
+#include <stdio.h>
+#include <iostream>
+#include <memory>
+
+using namespace activemq;
+using namespace activemq::core;
+using namespace activemq::cmsutil;
+using namespace decaf;
+using namespace decaf::lang;
+using namespace decaf::util;
+using namespace decaf::util::concurrent;
+using namespace cms;
+using namespace std;
+
+////////////////////////////////////////////////////////////////////////////////
+namespace{
+
+    class TextMessageCreator: public activemq::cmsutil::MessageCreator {
+    public:
+
+        TextMessageCreator() : MessageCreator() {}
+
+        virtual ~TextMessageCreator() throw() {}
+
+        virtual cms::Message* createMessage( cms::Session* session ) {
+
+            cms::Message* message = NULL;
+
+            if( session != NULL ) {
+                message = session->createTextMessage("test text message");
+            }
+
+            return message;
+        }
+    };
+}
+
+////////////////////////////////////////////////////////////////////////////////
+int main(int argc AMQCPP_UNUSED, char* argv[] AMQCPP_UNUSED) {
+
+    std::cout << "=====================================================\n";
+    std::cout << "Starting the MessageSender application..." << std::endl;
+
+    // initialize ActiveMQCPP library
+    activemq::library::ActiveMQCPP::initializeLibrary();
+
+    ActiveMQConnectionFactory* connectionFactory =
+        new ActiveMQConnectionFactory( "failover://(tcp://127.0.0.1:61616)" );
+
+    CmsTemplate* cmsTemplate = new CmsTemplate( connectionFactory );
+    cmsTemplate->setDefaultDestinationName( "CMSTemplateExamples" );
+    cmsTemplate->setPubSubDomain( true );
+
+    TextMessageCreator* textMessageCreator = new TextMessageCreator();
+
+    for( int i = 0; i < 100; i++ ) {
+
+        std::cout << "Iteration " << i << ". Sending 3 messages..." << std::endl;
+
+        for( int j = 0; j < 3; j++ ) {
+
+            try {
+                cmsTemplate->send( textMessageCreator );
+                std::cout << "Sent a message successfully." << endl;
+            } catch( cms::CMSException& ex ) {
+                std::cout << "Caught CMSException:  ";
+                std::cout << ex.getMessage().c_str() << endl;
+            }
+        }
+
+        std::cout << "Sleeping 15000 milliseconds..." << std::endl;
+        Thread::sleep( 15000 );
+    }
+
+    std::cout << "Shutting down MessageSender..." << std::endl;
+
+    //shut down ActiveMQCPP library
+    delete textMessageCreator;
+    delete cmsTemplate;
+    delete connectionFactory;
+
+    // terminates the ActiveMQCPP library
+    activemq::library::ActiveMQCPP::shutdownLibrary();
+}

Propchange: activemq/activemq-cpp/trunk/activemq-cpp/src/examples/cmstemplate/CMSTemplateSender.cpp
------------------------------------------------------------------------------
    svn:eol-style = native