activemq-cpp-3.3.0

src/main/activemq/cmsutil/CmsTemplate.h

Go to the documentation of this file.
00001 /*
00002  * Licensed to the Apache Software Foundation (ASF) under one or more
00003  * contributor license agreements.  See the NOTICE file distributed with
00004  * this work for additional information regarding copyright ownership.
00005  * The ASF licenses this file to You under the Apache License, Version 2.0
00006  * (the "License"); you may not use this file except in compliance with
00007  * the License.  You may obtain a copy of the License at
00008  *
00009  *     http://www.apache.org/licenses/LICENSE-2.0
00010  *
00011  * Unless required by applicable law or agreed to in writing, software
00012  * distributed under the License is distributed on an "AS IS" BASIS,
00013  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
00014  * See the License for the specific language governing permissions and
00015  * limitations under the License.
00016  */
00017 
00018 #ifndef ACTIVEMQ_CMSUTIL_CMSTEMPLATE_H_
00019 #define ACTIVEMQ_CMSUTIL_CMSTEMPLATE_H_
00020 
00021 #include <activemq/util/Config.h>
00022 #include <activemq/cmsutil/CmsDestinationAccessor.h>
00023 #include <activemq/cmsutil/SessionCallback.h>
00024 #include <activemq/cmsutil/ProducerCallback.h>
00025 #include <activemq/cmsutil/SessionPool.h>
00026 #include <cms/ConnectionFactory.h>
00027 #include <cms/DeliveryMode.h>
00028 #include <string>
00029 
00030 namespace activemq {
00031 namespace cmsutil {
00032 
00033     // Forward declarations.
00034     class MessageCreator;
00035 
00060     class AMQCPP_API CmsTemplate : public CmsDestinationAccessor {
00061     public:
00062 
00067         static const long long RECEIVE_TIMEOUT_NO_WAIT;
00068 
00072         static const long long RECEIVE_TIMEOUT_INDEFINITE_WAIT;
00073 
00077         static const int DEFAULT_PRIORITY;
00078 
00082         static const long long DEFAULT_TIME_TO_LIVE;
00083 
00084     public:
00085 
00089         class ProducerExecutor;
00090         friend class ProducerExecutor;
00091         class ProducerExecutor : public SessionCallback {
00092         protected:
00093 
00094             ProducerCallback* action;
00095             CmsTemplate* parent;
00096             cms::Destination* destination;
00097 
00098         private:
00099 
00100             ProducerExecutor( const ProducerExecutor& );
00101             ProducerExecutor& operator= ( const ProducerExecutor& );
00102 
00103         public:
00104 
00105             ProducerExecutor( ProducerCallback* action,
00106                               CmsTemplate* parent,
00107                               cms::Destination* destination )
00108             : SessionCallback(), action( action ), parent( parent ), destination( destination ) {
00109             }
00110 
00111             virtual ~ProducerExecutor() throw() {}
00112 
00113             virtual void doInCms( cms::Session* session );
00114 
00115             virtual cms::Destination* getDestination( cms::Session* session AMQCPP_UNUSED ) {
00116                 return destination;
00117             }
00118         };
00119 
00123         class ResolveProducerExecutor;
00124         friend class ResolveProducerExecutor;
00125         class ResolveProducerExecutor : public ProducerExecutor {
00126         private:
00127 
00128             std::string destinationName;
00129 
00130         private:
00131 
00132             ResolveProducerExecutor( const ResolveProducerExecutor& );
00133             ResolveProducerExecutor& operator= ( const ResolveProducerExecutor& );
00134 
00135         public:
00136 
00137             ResolveProducerExecutor( ProducerCallback* action,
00138                                      CmsTemplate* parent,
00139                                      const std::string& destinationName )
00140             : ProducerExecutor( action, parent, NULL ), destinationName( destinationName ) {
00141             }
00142 
00143             virtual ~ResolveProducerExecutor() throw() {}
00144 
00145             virtual cms::Destination* getDestination( cms::Session* session );
00146         };
00147 
00151         class SendExecutor;
00152         friend class SendExecutor;
00153         class SendExecutor : public ProducerCallback {
00154         private:
00155 
00156             MessageCreator* messageCreator;
00157             CmsTemplate* parent;
00158 
00159         private:
00160 
00161             SendExecutor( const SendExecutor& );
00162             SendExecutor& operator= ( const SendExecutor& );
00163 
00164         public:
00165 
00166             SendExecutor( MessageCreator* messageCreator,
00167                           CmsTemplate* parent )
00168             : ProducerCallback(), messageCreator( messageCreator ), parent( parent ) {
00169             }
00170 
00171             virtual ~SendExecutor() throw() {}
00172 
00173             virtual void doInCms( cms::Session* session,
00174                                   cms::MessageProducer* producer ) {
00175                 parent->doSend( session, producer, messageCreator );
00176             }
00177         };
00178 
00182         class ReceiveExecutor;
00183         friend class ReceiveExecutor;
00184         class ReceiveExecutor : public SessionCallback {
00185         protected:
00186 
00187             cms::Destination* destination;
00188             std::string selector;
00189             bool noLocal;
00190             cms::Message* message;
00191             CmsTemplate* parent;
00192 
00193         private:
00194 
00195             ReceiveExecutor( const ReceiveExecutor& );
00196             ReceiveExecutor& operator= ( const ReceiveExecutor& );
00197 
00198         public:
00199 
00200             ReceiveExecutor( CmsTemplate* parent,
00201                              cms::Destination* destination,
00202                              const std::string& selector,
00203                              bool noLocal )
00204             : SessionCallback(),
00205               destination( destination ),
00206               selector( selector ),
00207               noLocal( noLocal ),
00208               message( NULL ),
00209               parent( parent ) {
00210             }
00211 
00212             virtual ~ReceiveExecutor() throw() {}
00213 
00214             virtual void doInCms( cms::Session* session );
00215 
00216             virtual cms::Destination* getDestination( cms::Session* session AMQCPP_UNUSED ) {
00217                 return destination;
00218             }
00219 
00220             cms::Message* getMessage() {
00221                 return message;
00222             }
00223         };
00224 
00228         class ResolveReceiveExecutor;
00229         friend class ResolveReceiveExecutor;
00230         class ResolveReceiveExecutor : public ReceiveExecutor {
00231         private:
00232 
00233             std::string destinationName;
00234 
00235         private:
00236 
00237             ResolveReceiveExecutor( const ResolveReceiveExecutor& );
00238             ResolveReceiveExecutor& operator= ( const ResolveReceiveExecutor& );
00239 
00240         public:
00241 
00242             ResolveReceiveExecutor( CmsTemplate* parent,
00243                                     const std::string& selector,
00244                                     bool noLocal,
00245                                     const std::string& destinationName )
00246             : ReceiveExecutor( parent, NULL, selector, noLocal ), destinationName( destinationName ) {
00247 
00248             }
00249 
00250             virtual ~ResolveReceiveExecutor() throw() {}
00251 
00252             virtual cms::Destination* getDestination( cms::Session* session );
00253         };
00254 
00255     private:
00256 
00257         static const int NUM_SESSION_POOLS = (int)cms::Session::SESSION_TRANSACTED + 1;
00258 
00259         cms::Connection* connection;
00260 
00261         SessionPool* sessionPools[NUM_SESSION_POOLS];
00262 
00263         cms::Destination* defaultDestination;
00264 
00265         std::string defaultDestinationName;
00266 
00267         bool messageIdEnabled;
00268 
00269         bool messageTimestampEnabled;
00270 
00271         bool noLocal;
00272 
00273         long long receiveTimeout;
00274 
00275         bool explicitQosEnabled;
00276 
00277         int deliveryMode;
00278 
00279         int priority;
00280 
00281         long long timeToLive;
00282 
00283         bool initialized;
00284 
00285     private:
00286 
00287         CmsTemplate( const CmsTemplate& );
00288         CmsTemplate& operator= ( const CmsTemplate& );
00289 
00290     public:
00291 
00292         CmsTemplate();
00293         CmsTemplate( cms::ConnectionFactory* connectionFactory );
00294 
00295         virtual ~CmsTemplate() throw();
00296 
00306         virtual void setDefaultDestination( cms::Destination* defaultDestination ) {
00307             this->defaultDestination = defaultDestination;
00308         }
00309 
00314         virtual const cms::Destination* getDefaultDestination() const {
00315             return this->defaultDestination;
00316         }
00317 
00322         virtual cms::Destination* getDefaultDestination() {
00323             return this->defaultDestination;
00324         }
00325 
00335         virtual void setDefaultDestinationName( const std::string& defaultDestinationName ) {
00336             if( defaultDestinationName != this->defaultDestinationName ) {
00337                 this->defaultDestination = NULL;
00338                 this->defaultDestinationName = defaultDestinationName;
00339             }
00340         }
00341 
00349         virtual const std::string getDefaultDestinationName() const {
00350             return this->defaultDestinationName;
00351         }
00352 
00360         virtual void setPubSubDomain( bool pubSubDomain ) {
00361             if( pubSubDomain != isPubSubDomain() ) {
00362                 this->defaultDestination = NULL;
00363                 CmsDestinationAccessor::setPubSubDomain( pubSubDomain );
00364             }
00365         }
00366 
00367         virtual void setMessageIdEnabled( bool messageIdEnabled ) {
00368             this->messageIdEnabled = messageIdEnabled;
00369         }
00370 
00371         virtual bool isMessageIdEnabled() const {
00372             return this->messageIdEnabled;
00373         }
00374 
00375         virtual void setMessageTimestampEnabled( bool messageTimestampEnabled ) {
00376             this->messageTimestampEnabled = messageTimestampEnabled;
00377         }
00378 
00379         virtual bool isMessageTimestampEnabled() const {
00380             return this->messageTimestampEnabled;
00381         }
00382 
00383         virtual void setNoLocal( bool noLocal ) {
00384             this->noLocal = noLocal;
00385         }
00386 
00387         virtual bool isNoLocal() const {
00388             return this->noLocal;
00389         }
00390 
00391         virtual void setReceiveTimeout( long long receiveTimeout ) {
00392             this->receiveTimeout = receiveTimeout;
00393         }
00394 
00395         virtual long long getReceiveTimeout() const {
00396             return this->receiveTimeout;
00397         }
00398 
00407         virtual void setExplicitQosEnabled( bool explicitQosEnabled ) {
00408             this->explicitQosEnabled = explicitQosEnabled;
00409         }
00410 
00423         virtual bool isExplicitQosEnabled() const {
00424             return this->explicitQosEnabled;
00425         }
00426 
00435         virtual void setDeliveryPersistent( bool deliveryPersistent ) {
00436             this->deliveryMode = (deliveryPersistent ? 0 : 1 );
00437         }
00438 
00447         virtual void setDeliveryMode( int deliveryMode ) {
00448             this->deliveryMode = deliveryMode;
00449         }
00450 
00454         virtual int getDeliveryMode() const {
00455             return this->deliveryMode;
00456         }
00457 
00465         virtual void setPriority( int priority ) {
00466             this->priority = priority;
00467         }
00468 
00472         virtual int getPriority() const {
00473             return this->priority;
00474         }
00475 
00484         virtual void setTimeToLive( long long timeToLive ) {
00485             this->timeToLive = timeToLive;
00486         }
00487 
00491         virtual long long getTimeToLive() const {
00492             return this->timeToLive;
00493         }
00494 
00501         virtual void execute( SessionCallback* action );
00502 
00511         virtual void execute( ProducerCallback* action );
00512 
00523         virtual void execute( cms::Destination* dest, ProducerCallback* action );
00524 
00537         virtual void execute( const std::string& destinationName,
00538                               ProducerCallback* action );
00539 
00547         virtual void send( MessageCreator* messageCreator );
00548 
00558         virtual void send( cms::Destination* dest, MessageCreator* messageCreator );
00559 
00569         virtual void send( const std::string& destinationName,
00570                            MessageCreator* messageCreator );
00571 
00577         virtual cms::Message* receive();
00578 
00586         virtual cms::Message* receive( cms::Destination* destination );
00587 
00596         virtual cms::Message* receive( const std::string& destinationName );
00597 
00607         virtual cms::Message* receiveSelected( const std::string& selector );
00608 
00620         virtual cms::Message* receiveSelected( cms::Destination* destination,
00621                                                const std::string& selector );
00622 
00635         virtual cms::Message* receiveSelected( const std::string& destinationName,
00636                                                const std::string& selector );
00637 
00638     protected:
00639 
00640         void init();
00641 
00642         void destroy();
00643 
00644     private:
00645 
00649         void initDefaults();
00650 
00654         void createSessionPools();
00655 
00659         void destroySessionPools();
00660 
00667         void checkDefaultDestination();
00668 
00675         cms::Connection* getConnection();
00676 
00683         PooledSession* takeSession();
00684 
00691         void returnSession( PooledSession*& session );
00692 
00704         cms::MessageProducer* createProducer( cms::Session* session, cms::Destination* dest );
00705 
00712         void destroyProducer( cms::MessageProducer*& producer );
00713 
00725         cms::MessageConsumer* createConsumer( cms::Session* session,
00726                                               cms::Destination* dest,
00727                                               const std::string& selector,
00728                                               bool noLocal );
00729 
00736         void destroyConsumer( cms::MessageConsumer*& consumer );
00737 
00743         void destroyMessage( cms::Message*& message );
00744 
00755         void doSend( cms::Session* session,
00756                      cms::MessageProducer* producer,
00757                      MessageCreator* messageCreator );
00758 
00766         cms::Message* doReceive( cms::MessageConsumer* consumer );
00767 
00775         cms::Destination* resolveDefaultDestination( cms::Session* session );
00776 
00777     };
00778 
00779 }}
00780 
00781 #endif /*ACTIVEMQ_CMSUTIL_CMSTEMPLATE_H_*/