activemq-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From nmitt...@apache.org
Subject svn commit: r618105 - in /activemq/activemq-cpp/trunk/src/main/activemq/cmsutil: CmsTemplate.cpp PooledSession.cpp PooledSession.h SessionPool.h
Date Sun, 03 Feb 2008 22:18:13 GMT
Author: nmittler
Date: Sun Feb  3 14:18:12 2008
New Revision: 618105

URL: http://svn.apache.org/viewvc?rev=618105&view=rev
Log:
adding caching producers to CmsTemplate

Added:
    activemq/activemq-cpp/trunk/src/main/activemq/cmsutil/PooledSession.cpp
Modified:
    activemq/activemq-cpp/trunk/src/main/activemq/cmsutil/CmsTemplate.cpp
    activemq/activemq-cpp/trunk/src/main/activemq/cmsutil/PooledSession.h
    activemq/activemq-cpp/trunk/src/main/activemq/cmsutil/SessionPool.h

Modified: activemq/activemq-cpp/trunk/src/main/activemq/cmsutil/CmsTemplate.cpp
URL: http://svn.apache.org/viewvc/activemq/activemq-cpp/trunk/src/main/activemq/cmsutil/CmsTemplate.cpp?rev=618105&r1=618104&r2=618105&view=diff
==============================================================================
--- activemq/activemq-cpp/trunk/src/main/activemq/cmsutil/CmsTemplate.cpp (original)
+++ activemq/activemq-cpp/trunk/src/main/activemq/cmsutil/CmsTemplate.cpp Sun Feb  3 14:18:12
2008
@@ -205,13 +205,19 @@
             dest = resolveDefaultDestination(session);
         }
         
-        cms::MessageProducer* producer = session->createProducer(dest);
-        if (!isMessageIdEnabled()) {
-            producer->setDisableMessageID(true);
-        }
-        if (!isMessageTimestampEnabled()) {
-            producer->setDisableMessageTimeStamp(true);
+        cms::MessageProducer* producer = NULL;
+        
+        // Try to use a cached producer - requires that we're using a
+        // PooledSession
+        PooledSession* pooledSession = dynamic_cast<PooledSession*>(session);
+        if( pooledSession != NULL ) {
+            producer = pooledSession->createCachedProducer(dest);
+        } else {
+            producer = session->createProducer(dest);
         }
+        
+        producer->setDisableMessageID(!isMessageIdEnabled());
+        producer->setDisableMessageTimeStamp(!isMessageTimestampEnabled());
 
         return producer;
     }

Added: activemq/activemq-cpp/trunk/src/main/activemq/cmsutil/PooledSession.cpp
URL: http://svn.apache.org/viewvc/activemq/activemq-cpp/trunk/src/main/activemq/cmsutil/PooledSession.cpp?rev=618105&view=auto
==============================================================================
--- activemq/activemq-cpp/trunk/src/main/activemq/cmsutil/PooledSession.cpp (added)
+++ activemq/activemq-cpp/trunk/src/main/activemq/cmsutil/PooledSession.cpp Sun Feb  3 14:18:12
2008
@@ -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.
+ */
+
+#include "PooledSession.h"
+#include "SessionPool.h"
+#include "ResourceLifecycleManager.h"
+#include <activemq/exceptions/ActiveMQException.h>
+#include <activemq/exceptions/ExceptionDefines.h>
+
+using namespace activemq::cmsutil;
+using namespace activemq::exceptions;
+
+////////////////////////////////////////////////////////////////////////////////
+PooledSession::PooledSession(SessionPool* pool, cms::Session* session) {
+    this->session = session;
+    this->pool = pool;
+}
+
+////////////////////////////////////////////////////////////////////////////////
+PooledSession::~PooledSession(){
+}
+
+////////////////////////////////////////////////////////////////////////////////
+void PooledSession::close() throw( cms::CMSException ) {
+    
+    if( pool != NULL ) {
+        pool->returnSession(this);
+    }
+}
+
+////////////////////////////////////////////////////////////////////////////////
+cms::MessageProducer* PooledSession::createCachedProducer( 
+        const cms::Destination* destination )
+    throw ( cms::CMSException ) {
+    
+    try {
+        
+        std::string destName = getUniqueDestName(destination);
+        
+        // Check the cache - add it if necessary.
+        cms::MessageProducer* p = producerCache.getValue(destName);
+        if( p == NULL ) {
+            
+            // No producer exists for this destination - create it.
+            p = session->createProducer(destination);           
+            
+            // Add it to the cache.
+            producerCache.setValue(destName, p);
+            
+            // Add the producer to the resource lifecycle manager.
+            pool->getResourceLifecycleManager()->addMessageProducer(p);
+        }
+        
+        return p;
+    }
+    AMQ_CATCH_RETHROW( ActiveMQException )
+    AMQ_CATCHALL_THROW( ActiveMQException )
+}
+
+////////////////////////////////////////////////////////////////////////////////
+std::string PooledSession::getUniqueDestName( const cms::Destination* dest ) {
+    
+    std::string destName;
+    const cms::Queue* queue = dynamic_cast<const cms::Queue*>(dest);
+    if( queue != NULL ) {
+        destName = "q:" + queue->getQueueName();
+    } else {
+        const cms::Topic* topic = dynamic_cast<const cms::Topic*>(dest);
+        if( topic != NULL ) {
+            destName = "t:" + topic->getTopicName();
+        }
+    }
+    
+    return destName;
+}

Modified: activemq/activemq-cpp/trunk/src/main/activemq/cmsutil/PooledSession.h
URL: http://svn.apache.org/viewvc/activemq/activemq-cpp/trunk/src/main/activemq/cmsutil/PooledSession.h?rev=618105&r1=618104&r2=618105&view=diff
==============================================================================
--- activemq/activemq-cpp/trunk/src/main/activemq/cmsutil/PooledSession.h (original)
+++ activemq/activemq-cpp/trunk/src/main/activemq/cmsutil/PooledSession.h Sun Feb  3 14:18:12
2008
@@ -19,6 +19,7 @@
 #define ACTIVEMQ_CMSUTIL_POOLEDSESSION_H_
 
 #include <cms/Session.h>
+#include <decaf/util/Map.h>
 
 namespace activemq {
 namespace cmsutil {
@@ -37,6 +38,8 @@
         
         cms::Session* session;
         
+        decaf::util::Map<std::string, cms::MessageProducer*> producerCache;
+        
     public:
         
     	PooledSession( SessionPool* pool, cms::Session* session );
@@ -113,6 +116,19 @@
             return session->createProducer(destination);
         }
         
+        /**
+         * First checks the internal producer cache and creates one if none exist 
+         * for the given destination.  If created, the producer is added to the 
+         * pool's lifecycle manager.
+         * 
+         * @param destination
+         *          the destination to send on
+         * @return the producer resource
+         * @throws cms::CMSException if something goes wrong.
+         */
+        virtual cms::MessageProducer* createCachedProducer( const cms::Destination* destination
)
+            throw ( cms::CMSException );
+        
         virtual cms::Queue* createQueue( const std::string& queueName )
             throw ( cms::CMSException ) {
             return session->createQueue(queueName);
@@ -177,6 +193,10 @@
             throw ( cms::CMSException ) {
             session->unsubscribe(name);
         }
+        
+    private:
+        
+        std::string getUniqueDestName( const cms::Destination* dest );
     };
 
 }}

Modified: activemq/activemq-cpp/trunk/src/main/activemq/cmsutil/SessionPool.h
URL: http://svn.apache.org/viewvc/activemq/activemq-cpp/trunk/src/main/activemq/cmsutil/SessionPool.h?rev=618105&r1=618104&r2=618105&view=diff
==============================================================================
--- activemq/activemq-cpp/trunk/src/main/activemq/cmsutil/SessionPool.h (original)
+++ activemq/activemq-cpp/trunk/src/main/activemq/cmsutil/SessionPool.h Sun Feb  3 14:18:12
2008
@@ -89,6 +89,10 @@
     	 */
     	virtual void returnSession(PooledSession* session);
     	
+    	ResourceLifecycleManager* getResourceLifecycleManager() {
+    	    return resourceLifecycleManager;
+    	}
+    	
     };
 
 }}



Mime
View raw message