You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@activemq.apache.org by nm...@apache.org on 2008/01/08 04:20:56 UTC

svn commit: r609859 - in /activemq/activemq-cpp/trunk/src/main: Makefile.am activemq/cmsutil/PooledSession.cpp activemq/cmsutil/PooledSession.h activemq/cmsutil/SessionPool.cpp activemq/cmsutil/SessionPool.h

Author: nmittler
Date: Mon Jan  7 19:20:55 2008
New Revision: 609859

URL: http://svn.apache.org/viewvc?rev=609859&view=rev
Log:
AMQCPP-152 - Adding classes for support of CmsTemplate

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

Modified: activemq/activemq-cpp/trunk/src/main/Makefile.am
URL: http://svn.apache.org/viewvc/activemq/activemq-cpp/trunk/src/main/Makefile.am?rev=609859&r1=609858&r2=609859&view=diff
==============================================================================
--- activemq/activemq-cpp/trunk/src/main/Makefile.am (original)
+++ activemq/activemq-cpp/trunk/src/main/Makefile.am Mon Jan  7 19:20:55 2008
@@ -26,6 +26,8 @@
     activemq/cmsutil/ResourceLifecycleManager.cpp \
     activemq/cmsutil/CmsAccessor.cpp \
     activemq/cmsutil/CmsDestinationAccessor.cpp \
+    activemq/cmsutil/SessionPool.cpp \
+    activemq/cmsutil/PooledSession.cpp \
     activemq/core/ActiveMQConsumer.cpp \
     activemq/core/ActiveMQConnection.cpp \
     activemq/core/ActiveMQSession.cpp \
@@ -96,6 +98,8 @@
     activemq/cmsutil/DestinationResolver.h \
     activemq/cmsutil/DynamicDestinationResolver.h \
     activemq/cmsutil/ResourceLifecycleManager.h \
+    activemq/cmsutil/SessionPool.h \
+    activemq/cmsutil/PooledSession.h \
     activemq/support/LibraryInit.h \
     activemq/support/InitDirector.h \
     activemq/io/LoggingInputStream.h \

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=609859&view=auto
==============================================================================
--- activemq/activemq-cpp/trunk/src/main/activemq/cmsutil/PooledSession.cpp (added)
+++ activemq/activemq-cpp/trunk/src/main/activemq/cmsutil/PooledSession.cpp Mon Jan  7 19:20:55 2008
@@ -0,0 +1,40 @@
+/*
+ * 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"
+
+using namespace activemq::cmsutil;
+
+////////////////////////////////////////////////////////////////////////////////
+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);
+    }
+}
+

Added: 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=609859&view=auto
==============================================================================
--- activemq/activemq-cpp/trunk/src/main/activemq/cmsutil/PooledSession.h (added)
+++ activemq/activemq-cpp/trunk/src/main/activemq/cmsutil/PooledSession.h Mon Jan  7 19:20:55 2008
@@ -0,0 +1,184 @@
+/*
+ * 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.
+ */
+
+#ifndef ACTIVEMQ_CMSUTIL_POOLEDSESSION_H_
+#define ACTIVEMQ_CMSUTIL_POOLEDSESSION_H_
+
+#include <cms/Session.h>
+
+namespace activemq {
+namespace cmsutil {
+
+    // Forward declarations.
+    class SessionPool;
+
+    /**
+     * A pooled session object that wraps around a delegate session.  Calls
+     * to close this session only result in giving the session back to the pool.
+     */
+    class PooledSession : public cms::Session {
+    private:
+        
+        SessionPool* pool;
+        
+        cms::Session* session;
+        
+    public:
+        
+    	PooledSession( SessionPool* pool, cms::Session* session );
+    	
+    	/**
+    	 * Does nothing
+    	 */
+    	virtual ~PooledSession();
+
+    	/**
+         * Returns a non-constant reference to the internal session object.
+         * 
+         * @return the session object.
+         */
+    	virtual cms::Session* getSession() {
+    	    return session;
+    	}
+    	
+    	/**
+    	 * Returns a constant reference to the internal session object.
+    	 * 
+    	 * @return the session object.
+    	 */
+    	virtual const cms::Session* getSession() const {
+            return session;
+        }
+    	
+    	/**
+    	 * Returns this session back to the pool, but does not close 
+    	 * or destroy the internal session object.
+    	 */
+        virtual void close() throw( cms::CMSException );
+        
+        virtual void commit() throw ( cms::CMSException ) {
+            session->commit();
+        }
+        
+        virtual void rollback() throw ( cms::CMSException ) {
+            session->rollback();
+        }
+        
+        virtual cms::MessageConsumer* createConsumer(
+            const cms::Destination* destination )
+                throw ( cms::CMSException ) {
+            return session->createConsumer(destination);
+        }
+        
+        virtual cms::MessageConsumer* createConsumer( 
+            const cms::Destination* destination,
+            const std::string& selector )
+                throw ( cms::CMSException ) {
+            return session->createConsumer(destination, selector);
+        }
+        
+        virtual cms::MessageConsumer* createConsumer( 
+            const cms::Destination* destination,
+            const std::string& selector,
+            bool noLocal )
+                throw ( cms::CMSException ) {
+            return session->createConsumer(destination, selector, noLocal);
+        }
+        
+        virtual cms::MessageConsumer* createDurableConsumer(
+            const cms::Topic* destination,
+            const std::string& name,
+            const std::string& selector,
+            bool noLocal = false )
+                throw ( cms::CMSException ) {
+            return session->createDurableConsumer(destination, name, selector, noLocal);
+        }
+        
+        virtual cms::MessageProducer* createProducer( const cms::Destination* destination )
+            throw ( cms::CMSException ) {
+            return session->createProducer(destination);
+        }
+        
+        virtual cms::Queue* createQueue( const std::string& queueName )
+            throw ( cms::CMSException ) {
+            return session->createQueue(queueName);
+        }
+        
+        virtual cms::Topic* createTopic( const std::string& topicName )
+            throw ( cms::CMSException ) {
+            return session->createTopic(topicName);
+        }
+
+        virtual cms::TemporaryQueue* createTemporaryQueue()
+            throw ( cms::CMSException ) {
+            return session->createTemporaryQueue();
+        }
+
+        virtual cms::TemporaryTopic* createTemporaryTopic()
+            throw ( cms::CMSException ) {
+            return session->createTemporaryTopic();
+        }
+
+        virtual cms::Message* createMessage() 
+            throw ( cms::CMSException ) {            
+            return session->createMessage();
+        }
+
+        virtual cms::BytesMessage* createBytesMessage() 
+            throw ( cms::CMSException) {            
+            return session->createBytesMessage();
+        }
+        
+        virtual cms::BytesMessage* createBytesMessage(
+            const unsigned char* bytes,
+            std::size_t bytesSize ) 
+                throw ( cms::CMSException) {            
+            return session->createBytesMessage(bytes, bytesSize);
+        }
+        
+        virtual cms::TextMessage* createTextMessage() 
+            throw ( cms::CMSException ) {
+            return session->createTextMessage();
+        }
+
+        virtual cms::TextMessage* createTextMessage( const std::string& text ) 
+            throw ( cms::CMSException ) {
+            return session->createTextMessage(text);
+        }
+        
+        virtual cms::MapMessage* createMapMessage() 
+            throw ( cms::CMSException ) {
+            return session->createMapMessage();
+        }
+        
+        virtual cms::Session::AcknowledgeMode getAcknowledgeMode() const {
+            return session->getAcknowledgeMode();
+        }
+        
+        virtual bool isTransacted() const {
+            return session->isTransacted();
+        }
+
+        virtual void unsubscribe( const std::string& name ) 
+            throw ( cms::CMSException ) {
+            session->unsubscribe(name);
+        }
+    };
+
+}}
+
+#endif /*ACTIVEMQ_CMSUTIL_POOLEDSESSION_H_*/

Added: activemq/activemq-cpp/trunk/src/main/activemq/cmsutil/SessionPool.cpp
URL: http://svn.apache.org/viewvc/activemq/activemq-cpp/trunk/src/main/activemq/cmsutil/SessionPool.cpp?rev=609859&view=auto
==============================================================================
--- activemq/activemq-cpp/trunk/src/main/activemq/cmsutil/SessionPool.cpp (added)
+++ activemq/activemq-cpp/trunk/src/main/activemq/cmsutil/SessionPool.cpp Mon Jan  7 19:20:55 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 "SessionPool.h"
+#include "ResourceLifecycleManager.h"
+
+using namespace activemq::cmsutil;
+using namespace std;
+
+////////////////////////////////////////////////////////////////////////////////
+SessionPool::SessionPool( cms::Connection* connection, 
+    cms::Session::AcknowledgeMode ackMode,
+    ResourceLifecycleManager* resourceLifecycleManager) {
+    
+    this->connection = connection;
+    this->acknowledgeMode = ackMode;
+    this->resourceLifecycleManager = resourceLifecycleManager;
+}
+
+////////////////////////////////////////////////////////////////////////////////
+SessionPool::~SessionPool() {
+    
+    // Destroy all of the pooled session objects.
+    list<PooledSession*>::iterator iter = sessions.begin();
+    for( ; iter != sessions.end(); ++iter ) {
+        delete *iter;
+    }
+    sessions.clear();
+}
+
+////////////////////////////////////////////////////////////////////////////////
+PooledSession* SessionPool::takeSession() throw (cms::CMSException){
+    
+    synchronized(&mutex) {
+        
+        PooledSession* pooledSession = NULL;
+        
+        // If there are no sessions available, create a new one and return it.
+        if( available.size() == 0 ) {
+            
+            // No sessions were available - create a new one.
+            cms::Session* session = connection->createSession(acknowledgeMode);
+            
+            // Give this resource to the lifecycle manager to manage. The pool
+            // will not be in charge of destroying this resource.
+            resourceLifecycleManager->addSession(session);
+            
+            // Now wrap the session with a pooled session.
+            PooledSession* pooledSession = new PooledSession(this, session);
+            
+            // Add to the sessions list.
+            sessions.push_back(pooledSession);
+            
+        } else {
+        
+            // There are sessions available - use the one at the head of the
+            // list, and remove it from the available list.
+            pooledSession = available.front();
+            available.pop_front();
+        }
+        
+        // Return the session.
+        return pooledSession;
+    }
+}
+
+////////////////////////////////////////////////////////////////////////////////
+void SessionPool::returnSession( PooledSession* session ) {
+    
+    synchronized(&mutex) {        
+        
+        // Add to the available list.
+        available.push_back(session);
+    }
+}

Added: 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=609859&view=auto
==============================================================================
--- activemq/activemq-cpp/trunk/src/main/activemq/cmsutil/SessionPool.h (added)
+++ activemq/activemq-cpp/trunk/src/main/activemq/cmsutil/SessionPool.h Mon Jan  7 19:20:55 2008
@@ -0,0 +1,96 @@
+/*
+ * 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.
+ */
+
+#ifndef ACTIVEMQ_CMSUTIL_SESSIONPOOL_H_
+#define ACTIVEMQ_CMSUTIL_SESSIONPOOL_H_
+
+#include <activemq/cmsutil/PooledSession.h>
+#include <decaf/util/concurrent/Mutex.h>
+#include <cms/Connection.h>
+#include <list>
+
+namespace activemq {
+namespace cmsutil {
+
+    // Forward declarations.
+    class ResourceLifecycleManager;
+    
+    /**
+     * A pool of CMS sessions from the same connection and with the same
+     * acknowledge mode.  Internal session resources are managed through a
+     * provided <code>ResourceLifecycleManager</code>, not by this pool.  This
+     * class is thread-safe.
+     */
+    class SessionPool
+    {
+    private:
+        
+        cms::Connection* connection;
+        
+        ResourceLifecycleManager* resourceLifecycleManager;
+        
+        decaf::util::concurrent::Mutex mutex;
+        
+        std::list<PooledSession*> available;
+        
+        std::list<PooledSession*> sessions;
+        
+        cms::Session::AcknowledgeMode acknowledgeMode;
+        
+    public:
+        
+        /**
+         * Constructs a session pool.
+         * @param connection
+         *          the connection to be used for creating all sessions.
+         * @param ackMode
+         *          the acknowledge mode to be used for all sessions
+         * @param resourceLifecycleManager
+         *          the object responsible for managing the lifecycle of
+         *          any allocated cms::Session resources.
+         */
+    	SessionPool(cms::Connection* connection, 
+    	        cms::Session::AcknowledgeMode ackMode,
+    	        ResourceLifecycleManager* resourceLifecycleManager );
+    	
+    	/**
+    	 * Destroys the pooled session objects, but not the underlying session
+    	 * resources.  That is the job of the ResourceLifecycleManager.
+    	 */
+    	virtual ~SessionPool();
+    	
+    	/**
+    	 * Takes a session from the pool, creating one if necessary.
+    	 * 
+    	 * @return the pooled session object
+    	 * 
+    	 * @throws cms::CMSException if an error occurred
+    	 */
+    	virtual PooledSession* takeSession() throw (cms::CMSException);
+    	
+    	/**
+    	 * Returns a session to the pool.
+    	 * @param session
+    	 *         the session to be returned.
+    	 */
+    	virtual void returnSession(PooledSession* session);
+    	
+    };
+
+}}
+
+#endif /*ACTIVEMQ_CMSUTIL_SESSIONPOOL_H_*/