activemq-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From nmitt...@apache.org
Subject svn commit: r611694 - in /activemq/activemq-cpp/trunk/src/main/activemq/cmsutil: CmsDestinationAccessor.cpp CmsTemplate.cpp CmsTemplate.h MessageCreator.h ProducerCallback.h SessionCallback.h
Date Mon, 14 Jan 2008 01:34:14 GMT
Author: nmittler
Date: Sun Jan 13 17:34:12 2008
New Revision: 611694

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

Modified:
    activemq/activemq-cpp/trunk/src/main/activemq/cmsutil/CmsDestinationAccessor.cpp
    activemq/activemq-cpp/trunk/src/main/activemq/cmsutil/CmsTemplate.cpp
    activemq/activemq-cpp/trunk/src/main/activemq/cmsutil/CmsTemplate.h
    activemq/activemq-cpp/trunk/src/main/activemq/cmsutil/MessageCreator.h
    activemq/activemq-cpp/trunk/src/main/activemq/cmsutil/ProducerCallback.h
    activemq/activemq-cpp/trunk/src/main/activemq/cmsutil/SessionCallback.h

Modified: activemq/activemq-cpp/trunk/src/main/activemq/cmsutil/CmsDestinationAccessor.cpp
URL: http://svn.apache.org/viewvc/activemq/activemq-cpp/trunk/src/main/activemq/cmsutil/CmsDestinationAccessor.cpp?rev=611694&r1=611693&r2=611694&view=diff
==============================================================================
--- activemq/activemq-cpp/trunk/src/main/activemq/cmsutil/CmsDestinationAccessor.cpp (original)
+++ activemq/activemq-cpp/trunk/src/main/activemq/cmsutil/CmsDestinationAccessor.cpp Sun Jan
13 17:34:12 2008
@@ -56,7 +56,7 @@
     
     checkDestinationResolver();
     
-    getDestinationResolver()->resolveDestinationName(session, 
+    return getDestinationResolver()->resolveDestinationName(session, 
             destName, 
             isPubSubDomain());
 }

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=611694&r1=611693&r2=611694&view=diff
==============================================================================
--- activemq/activemq-cpp/trunk/src/main/activemq/cmsutil/CmsTemplate.cpp (original)
+++ activemq/activemq-cpp/trunk/src/main/activemq/cmsutil/CmsTemplate.cpp Sun Jan 13 17:34:12
2008
@@ -16,8 +16,13 @@
  */
 
 #include "CmsTemplate.h"
+#include <activemq/exceptions/ActiveMQException.h>
+#include <activemq/exceptions/ExceptionDefines.h>
+#include "ProducerCallback.h"
+#include "MessageCreator.h"
 
 using namespace activemq::cmsutil;
+using namespace activemq::exceptions;
 using namespace decaf::lang::exceptions;
 
 ////////////////////////////////////////////////////////////////////////////////
@@ -84,16 +89,22 @@
 ////////////////////////////////////////////////////////////////////////////////
 void CmsTemplate::init() throw (cms::CMSException, IllegalStateException) {
 
-    // Invoke the base class.
-    CmsDestinationAccessor::init();
-
-    // Make sure we have a valid default destination.
-    checkDefaultDestination();
+    try {
+        
+        // Invoke the base class.
+        CmsDestinationAccessor::init();
+    
+        // Make sure we have a valid default destination.
+        checkDefaultDestination();
+    }
+    AMQ_CATCH_RETHROW( ActiveMQException )
+    AMQ_CATCH_RETHROW( IllegalStateException )
+    AMQ_CATCHALL_THROW( ActiveMQException )
 }
 
 ////////////////////////////////////////////////////////////////////////////////
 void CmsTemplate::checkDefaultDestination() throw (IllegalStateException) {
-    if (this->defaultDestination == NULL) {
+    if (this->defaultDestination == NULL && this->defaultDestinationName.size()==0)
{
         throw IllegalStateException(
                 __FILE__, __LINE__,
                 "No defaultDestination or defaultDestinationName specified. Check configuration
of CmsTemplate.");
@@ -101,6 +112,32 @@
 }
 
 ////////////////////////////////////////////////////////////////////////////////
+cms::Destination* CmsTemplate::resolveDefaultDestination(cms::Session* session)
+throw (cms::CMSException, IllegalStateException) {
+    
+    try {
+        
+        // Make sure we have a default - otherwise throw.
+        checkDefaultDestination();
+        
+        // First, check the destination object.
+        cms::Destination* dest = getDefaultDestination();
+        
+        // If no default object was provided, the name was provided.  Resolve
+        // the name and then set the destination object so we don't have to
+        // do this next time.
+        if( dest == NULL ) {
+            dest = resolveDestinationName(session, getDefaultDestinationName());
+            setDefaultDestination(dest);
+        }
+        
+        return dest;
+    }
+    AMQ_CATCH_RETHROW( ActiveMQException )
+    AMQ_CATCH_RETHROW( IllegalStateException )
+}
+
+////////////////////////////////////////////////////////////////////////////////
 cms::Connection* CmsTemplate::getConnection() 
 throw (cms::CMSException) {
 
@@ -122,7 +159,8 @@
         
         return connection;
     }
-    AMQ_CATCH_RETHROW( cms::CMSException )
+    AMQ_CATCH_RETHROW( ActiveMQException )
+    AMQ_CATCHALL_THROW( ActiveMQException )
 }
 
 ////////////////////////////////////////////////////////////////////////////////
@@ -132,9 +170,10 @@
     try {
         
         // Take a session from the pool.
-        return sessionPools[getSessionAcknowledgeMode()].takeSession();        
+        return sessionPools[getSessionAcknowledgeMode()]->takeSession();        
     }
-    AMQ_CATCH_RETHROW( cms::CMSException )
+    AMQ_CATCH_RETHROW( ActiveMQException )
+    AMQ_CATCHALL_THROW( ActiveMQException )
 }
 
 ////////////////////////////////////////////////////////////////////////////////
@@ -151,17 +190,19 @@
         session->close();
         session = NULL;
     }
-    AMQ_CATCH_RETHROW( cms::CMSException )
+    AMQ_CATCH_RETHROW( ActiveMQException )
+    AMQ_CATCHALL_THROW( ActiveMQException )
 }
 
 ////////////////////////////////////////////////////////////////////////////////
-cms::Producer* CmsTemplate::createProducer(cms::Session* session,
+cms::MessageProducer* CmsTemplate::createProducer(cms::Session* session,
         cms::Destination* dest) throw (cms::CMSException) {
 
     try {
 
-        if( dest == NULL ) {
-            dest = getDefaultDestination();
+        // If no destination was provided, resolve the default.
+        if( dest == NULL ) {            
+            dest = resolveDefaultDestination(session);
         }
         
         cms::MessageProducer* producer = session->createProducer(dest);
@@ -169,12 +210,14 @@
             producer->setDisableMessageID(true);
         }
         if (!isMessageTimestampEnabled()) {
-            producer->setDisableMessageTimestamp(true);
+            producer->setDisableMessageTimeStamp(true);
         }
 
         return producer;
     }
-    AMQ_CATCH_RETHROW( cms::CMSException )
+    AMQ_CATCH_RETHROW( ActiveMQException )
+    AMQ_CATCH_EXCEPTION_CONVERT( IllegalStateException, ActiveMQException )
+    AMQ_CATCHALL_THROW( ActiveMQException )
 }
 
 ////////////////////////////////////////////////////////////////////////////////
@@ -190,7 +233,7 @@
         // Close the producer, then destroy it.
         producer->close();                
     }
-    AMQ_CATCH_NO_RETHROW( cms::CMSException )
+    AMQ_CATCH_NOTHROW( cms::CMSException )
     
     delete producer;
     producer = NULL;
@@ -208,7 +251,7 @@
         
         return consumer;
     }
-    AMQ_CATCH_RETHROW( cms::CMSException )
+    AMQ_CATCH_RETHROW( ActiveMQException )
 }
 
 ////////////////////////////////////////////////////////////////////////////////
@@ -224,7 +267,7 @@
         // Close the consumer, then destroy it.
         consumer->close();                
     }
-    AMQ_CATCH_NO_RETHROW( cms::CMSException )
+    AMQ_CATCH_NOTHROW( cms::CMSException )
     
     delete consumer;
     consumer = NULL;
@@ -262,7 +305,7 @@
         // Return the session to the pool.
         returnSession(pooledSession);
         
-    } catch( cms::CMSException& e ) {
+    } catch( ActiveMQException& e ) {
         
         e.setMark(__FILE__, __LINE__);
         
@@ -279,19 +322,20 @@
     try {
         
         // Create the callback.
-        ProducerSessionCallback cb(action);
+        ProducerSessionCallback cb(action, this);
         
         // Execute the action in a session.
         execute(&cb);
     }
-    AMQ_CATCH_RETHROW( cms::CMSException )
+    AMQ_CATCH_RETHROW( ActiveMQException )
+    AMQ_CATCHALL_THROW( ActiveMQException )
 }
 
 ////////////////////////////////////////////////////////////////////////////////
 void CmsTemplate::ProducerSessionCallback::doInCms( cms::Session* session ) 
 throw (cms::CMSException) {
         
-    MessageProducer* producer = NULL;
+    cms::MessageProducer* producer = NULL;
     
     try {
     
@@ -300,22 +344,71 @@
         }
         
         // Create the producer.
-        producer = createProducer(session, null);
+        producer = parent->createProducer(session, NULL);
         
         // Execute the action.
         action->doInCms(session, producer);
         
         // Destroy the producer.
-        destroyProducer(producer);
+        parent->destroyProducer(producer);
         
-    } catch( cms::CMSException& e) {
+    } catch( ActiveMQException& e) {
+        
+        e.setMark(__FILE__, __LINE__);
         
         // Destroy the producer.
-        destroyProducer(producer);
+        parent->destroyProducer(producer);
+        
+        throw e;
+    }
+}
+
+////////////////////////////////////////////////////////////////////////////////
+void CmsTemplate::send(MessageCreator* messageCreator) 
+throw (cms::CMSException, IllegalStateException)  {
+    
+    try {
+        
+        checkDefaultDestination();
+        if (getDefaultDestination() != NULL) {
+            send(getDefaultDestination(), messageCreator);
+        }
+        else {
+            send(getDefaultDestinationName(), messageCreator);
+        }
     }
+    AMQ_CATCH_RETHROW( ActiveMQException )
+    AMQ_CATCH_RETHROW( IllegalStateException )
+    AMQ_CATCHALL_THROW( ActiveMQException )
 }
 
 ////////////////////////////////////////////////////////////////////////////////
+void CmsTemplate::send(cms::Destination* dest, 
+                MessageCreator* messageCreator) 
+throw (cms::CMSException, IllegalStateException) {
+    
+    try {
+        Sender sender(dest, messageCreator, this);
+        execute(&sender);
+    }
+    AMQ_CATCH_RETHROW( ActiveMQException )
+    AMQ_CATCHALL_THROW( ActiveMQException )
+}
+       
+////////////////////////////////////////////////////////////////////////////////
+void CmsTemplate::send(const std::string& destinationName, 
+                MessageCreator* messageCreator) 
+throw (cms::CMSException, IllegalStateException) {
+    
+    try {
+        ResolveSender sender(destinationName, messageCreator, this);
+        execute(&sender);
+    }
+    AMQ_CATCH_RETHROW( ActiveMQException )
+    AMQ_CATCHALL_THROW( ActiveMQException )
+}
+        
+////////////////////////////////////////////////////////////////////////////////
 void CmsTemplate::doSend(cms::Session* session, cms::Destination* dest, 
         MessageCreator* messageCreator) throw (cms::CMSException) {
     
@@ -345,11 +438,28 @@
         destroyProducer(producer);
         destroyMessage(message);
         
-    } catch( cms::CMSException& e) {
+    } catch( ActiveMQException& e) {
+        
+        e.setMark(__FILE__, __LINE__ );
         
         // Destroy the resources.
         destroyProducer(producer);
         destroyMessage(message);
+        
+        throw e;
     }
+}
+
+////////////////////////////////////////////////////////////////////////////////
+void CmsTemplate::ResolveSender::doInCms(cms::Session* session) 
+throw (cms::CMSException) {
+    
+    try {
+        cms::Destination* dest = parent->resolveDestinationName(session, destinationName);
+        parent->doSend(session, dest, messageCreator);
+    } 
+    AMQ_CATCH_RETHROW( ActiveMQException )
+    AMQ_CATCH_EXCEPTION_CONVERT( IllegalStateException, ActiveMQException )
+    AMQ_CATCHALL_THROW( ActiveMQException )
 }
 

Modified: activemq/activemq-cpp/trunk/src/main/activemq/cmsutil/CmsTemplate.h
URL: http://svn.apache.org/viewvc/activemq/activemq-cpp/trunk/src/main/activemq/cmsutil/CmsTemplate.h?rev=611694&r1=611693&r2=611694&view=diff
==============================================================================
--- activemq/activemq-cpp/trunk/src/main/activemq/cmsutil/CmsTemplate.h (original)
+++ activemq/activemq-cpp/trunk/src/main/activemq/cmsutil/CmsTemplate.h Sun Jan 13 17:34:12
2008
@@ -20,6 +20,7 @@
 
 #include <activemq/cmsutil/CmsDestinationAccessor.h>
 #include <activemq/cmsutil/SessionCallback.h>
+#include <activemq/cmsutil/SessionPool.h>
 #include <decaf/lang/exceptions/IllegalStateException.h>
 #include <cms/ConnectionFactory.h>
 #include <cms/DeliveryMode.h>
@@ -29,7 +30,8 @@
 namespace cmsutil {
 
     // Forward declarations.
-    class SessionCallback;
+    class ProducerCallback;
+    class MessageCreator;
     
     class CmsTemplate : public CmsDestinationAccessor
     {
@@ -56,32 +58,94 @@
          */
         static const long long DEFAULT_TIME_TO_LIVE = 0;                
     
-    private:
+    public:
         
         /**
          * Session callback that executes a producer callback.
          */
+        class ProducerSessionCallback;
+        friend class ProducerSessionCallback;
         class ProducerSessionCallback : public SessionCallback {
         private:
             
             ProducerCallback* action;
+            CmsTemplate* parent;
             
         public:
             
-            ProducerSessionCallback(PoducerCallback* action){
+            ProducerSessionCallback(ProducerCallback* action,
+                    CmsTemplate* parent){
                 this->action = action;
+                this->parent = parent;
             }
             
             virtual ~ProducerSessionCallback() {}
             
             virtual void doInCms(cms::Session* session) throw (cms::CMSException);
         };
+        
+        /**
+         * Session callback that sends to the given destination.
+         */
+        class Sender;
+        friend class Sender;
+        class Sender : public SessionCallback {
+        private:
+            
+            cms::Destination* dest;
+            MessageCreator* messageCreator;
+            CmsTemplate* parent;
+            
+        public:
+            
+            Sender(cms::Destination* dest, MessageCreator* messageCreator,
+                    CmsTemplate* parent) {
+                this->dest = dest;
+                this->messageCreator = messageCreator;
+                this->parent = parent;
+            }
+            
+            virtual ~Sender() {}
+            
+            virtual void doInCms(cms::Session* session) throw (cms::CMSException) {
+                parent->doSend(session, dest, messageCreator);
+            }
+        };
+        
+        /**
+         * Session callback that sends a message to a named destination.
+         */
+        class ResolveSender;
+        friend class ResolveSender;
+        class ResolveSender : public SessionCallback {
+        private:
+                    
+            std::string destinationName;
+            MessageCreator* messageCreator;
+            CmsTemplate* parent;
+            
+        public:
+            
+            ResolveSender(const std::string& destinationName, 
+                    MessageCreator* messageCreator,
+                    CmsTemplate* parent ) {
+                this->destinationName = destinationName;
+                this->messageCreator = messageCreator;
+                this->parent = parent;
+            }
+            
+            virtual ~ResolveSender() {}
+            
+            virtual void doInCms(cms::Session* session) throw (cms::CMSException);
+        };
+        
+    private:
                 
         static const int NUM_SESSION_POOLS = (int)cms::Session::SESSION_TRANSACTED + 1;
     
         cms::Connection* connection;
         
-        SessionPool*[NUM_SESSION_POOLS] sessionPools;
+        SessionPool* sessionPools[NUM_SESSION_POOLS];
         
         cms::Destination* defaultDestination;
     
@@ -124,6 +188,10 @@
         virtual const cms::Destination* getDefaultDestination() const {
             return this->defaultDestination;
         }
+        
+        virtual cms::Destination* getDefaultDestination() {
+            return this->defaultDestination;
+        }
     
         virtual void setDefaultDestinationName(const std::string& defaultDestinationName)
{
             this->defaultDestinationName = defaultDestinationName;
@@ -202,7 +270,7 @@
          * @see #setDeliveryMode(int)
          */
         virtual void setDeliveryPersistent(bool deliveryPersistent) {
-            this->deliveryMode = (deliveryPersistent ? cms::DeliveryMode.PERSISTENT :
cms::DeliveryMode.NON_PERSISTENT);
+            this->deliveryMode = (deliveryPersistent ? cms::DeliveryMode::PERSISTENT :
cms::DeliveryMode::NON_PERSISTENT);
         }
     
         /**
@@ -214,7 +282,7 @@
          * @see #isExplicitQosEnabled
          */
         virtual void setDeliveryMode(int deliveryMode) {
-            this.deliveryMode = deliveryMode;
+            this->deliveryMode = deliveryMode;
         }
     
         /**
@@ -231,7 +299,7 @@
          * 
          * @see #isExplicitQosEnabled
          */
-        virtual public void setPriority(int priority) {
+        virtual void setPriority(int priority) {
             this->priority = priority;
         }
     
@@ -279,6 +347,48 @@
          */
         virtual void execute(ProducerCallback* action) throw (cms::CMSException);
     
+        /**
+         * Convenience method for sending a message to the default destination.
+         * 
+         * @param messageCreator
+         *          Responsible for creating the message to be sent
+         * @throws cms::CMSException thrown if an error occurs.
+         * @throws decaf::lang::exceptions::IllegalStateException thrown if the
+         *          default destination has not been specified.
+         */
+        virtual void send(MessageCreator* messageCreator) 
+        throw (cms::CMSException, decaf::lang::exceptions::IllegalStateException);
+
+        /**
+         * Convenience method for sending a message to the specified destination.
+         * 
+         * @param dest
+         *          The destination to send to
+         * @param messageCreator
+         *          Responsible for creating the message to be sent
+         * @throws cms::CMSException thrown if an error occurs.
+         * @throws decaf::lang::exceptions::IllegalStateException thrown if the
+         *          default destination has not been specified.
+         */
+        virtual void send(cms::Destination* dest, 
+                MessageCreator* messageCreator)
+        throw (cms::CMSException, decaf::lang::exceptions::IllegalStateException);
+        
+        /**
+         * Convenience method for sending a message to the specified destination.
+         * 
+         * @param destinationName
+         *          The name of the destination to send to.
+         * @param messageCreator
+         *          Responsible for creating the message to be sent
+         * @throws cms::CMSException thrown if an error occurs.
+         * @throws decaf::lang::exceptions::IllegalStateException thrown if the
+         *          default destination has not been specified.
+         */
+        virtual void send(const std::string& destinationName, 
+                MessageCreator* messageCreator)
+        throw (cms::CMSException, decaf::lang::exceptions::IllegalStateException);
+        
     private:
     
         /**
@@ -393,6 +503,18 @@
          */
         void doSend(cms::Session* session, cms::Destination* dest, 
                 MessageCreator* messageCreator) throw (cms::CMSException);
+        
+        /**
+         * Resolves the default destination and returns it.
+         * @param session
+         *          the parent session.
+         * @return the default destination
+         * @throws cms::CMSException if an error occurs
+         * @throws decaf::lang::exceptions::IllegalStateException thrown if
+         *          no default destination was provided.
+         */
+        cms::Destination* resolveDefaultDestination(cms::Session* session)
+        throw (cms::CMSException, decaf::lang::exceptions::IllegalStateException);
     };
 
 }}

Modified: activemq/activemq-cpp/trunk/src/main/activemq/cmsutil/MessageCreator.h
URL: http://svn.apache.org/viewvc/activemq/activemq-cpp/trunk/src/main/activemq/cmsutil/MessageCreator.h?rev=611694&r1=611693&r2=611694&view=diff
==============================================================================
--- activemq/activemq-cpp/trunk/src/main/activemq/cmsutil/MessageCreator.h (original)
+++ activemq/activemq-cpp/trunk/src/main/activemq/cmsutil/MessageCreator.h Sun Jan 13 17:34:12
2008
@@ -18,6 +18,9 @@
 #ifndef ACTIVEMQ_CMSUTIL_MESSAGECREATOR_H
 #define ACTIVEMQ_CMSUTIL_MESSAGECREATOR_H
 
+#include <cms/Session.h>
+#include <cms/Message.h>
+
 namespace activemq {
 namespace cmsutil {
 
@@ -26,8 +29,9 @@
      * <code>CmsTemplate</code>.
      */
     class MessageCreator {
+    public:
         
-        virtual ~MessageCreator();
+        virtual ~MessageCreator(){}
     
         /**
          * Creates a message from the given session.
@@ -39,7 +43,7 @@
         virtual cms::Message* createMessage(cms::Session* session ) 
             throw (cms::CMSException);
     
-    }
+    };
 
 }}
 

Modified: activemq/activemq-cpp/trunk/src/main/activemq/cmsutil/ProducerCallback.h
URL: http://svn.apache.org/viewvc/activemq/activemq-cpp/trunk/src/main/activemq/cmsutil/ProducerCallback.h?rev=611694&r1=611693&r2=611694&view=diff
==============================================================================
--- activemq/activemq-cpp/trunk/src/main/activemq/cmsutil/ProducerCallback.h (original)
+++ activemq/activemq-cpp/trunk/src/main/activemq/cmsutil/ProducerCallback.h Sun Jan 13 17:34:12
2008
@@ -18,15 +18,19 @@
 #ifndef ACTIVEMQ_CMSUTIL_PRODUCERCALLBACK_H
 #define ACTIVEMQ_CMSUTIL_PRODUCERCALLBACK_H
 
+#include <cms/Session.h>
+#include <cms/MessageProducer.h>
+
 namespace activemq {
 namespace cmsutil {
 
     /**
      * Callback for sending a message to a CMS destination.
      */
-    class ProducernCallback {
+    class ProducerCallback {
+    public:
         
-        virtual ~ProducernCallback();
+        virtual ~ProducerCallback(){}
     
         /**
          * Execute an action given a session and producer.
@@ -40,7 +44,7 @@
         virtual void doInCms(cms::Session* session, 
                 cms::MessageProducer* producer) throw (cms::CMSException) = 0;
     
-    }
+    };
 
 }}
 

Modified: activemq/activemq-cpp/trunk/src/main/activemq/cmsutil/SessionCallback.h
URL: http://svn.apache.org/viewvc/activemq/activemq-cpp/trunk/src/main/activemq/cmsutil/SessionCallback.h?rev=611694&r1=611693&r2=611694&view=diff
==============================================================================
--- activemq/activemq-cpp/trunk/src/main/activemq/cmsutil/SessionCallback.h (original)
+++ activemq/activemq-cpp/trunk/src/main/activemq/cmsutil/SessionCallback.h Sun Jan 13 17:34:12
2008
@@ -18,6 +18,8 @@
 #ifndef ACTIVEMQ_CMSUTIL_SESSIONCALLBACK_H
 #define ACTIVEMQ_CMSUTIL_SESSIONCALLBACK_H
 
+#include <cms/Session.h>
+
 namespace activemq {
 namespace cmsutil {
 
@@ -27,7 +29,9 @@
      */
     class SessionCallback {
         
-        virtual ~SessionCallback();
+    public:
+        
+        virtual ~SessionCallback(){}
     
         /**
          * Execute any number of operations against the supplied CMS
@@ -39,7 +43,7 @@
          */
         virtual void doInCms(cms::Session* session) throw (cms::CMSException) = 0;
     
-    }
+    };
 
 }}
 



Mime
View raw message