activemq-cpp-3.3.0

src/main/activemq/core/ActiveMQSession.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 #ifndef _ACTIVEMQ_CORE_ACTIVEMQSESSION_H_
00018 #define _ACTIVEMQ_CORE_ACTIVEMQSESSION_H_
00019 
00020 #include <cms/Session.h>
00021 #include <cms/ExceptionListener.h>
00022 
00023 #include <activemq/util/Config.h>
00024 #include <activemq/util/Usage.h>
00025 #include <activemq/exceptions/ActiveMQException.h>
00026 #include <activemq/core/ActiveMQTransactionContext.h>
00027 #include <activemq/commands/ActiveMQTempDestination.h>
00028 #include <activemq/commands/Response.h>
00029 #include <activemq/commands/SessionInfo.h>
00030 #include <activemq/commands/ConsumerInfo.h>
00031 #include <activemq/commands/ConsumerId.h>
00032 #include <activemq/commands/ProducerId.h>
00033 #include <activemq/commands/TransactionId.h>
00034 #include <activemq/core/Dispatcher.h>
00035 #include <activemq/core/MessageDispatchChannel.h>
00036 #include <activemq/util/LongSequenceGenerator.h>
00037 #include <activemq/threads/Scheduler.h>
00038 
00039 #include <decaf/lang/Pointer.h>
00040 #include <decaf/util/StlMap.h>
00041 #include <decaf/util/Properties.h>
00042 #include <decaf/util/concurrent/atomic/AtomicBoolean.h>
00043 #include <decaf/util/concurrent/CopyOnWriteArrayList.h>
00044 
00045 #include <string>
00046 #include <memory>
00047 
00048 namespace activemq{
00049 namespace core{
00050 
00051     using decaf::lang::Pointer;
00052     using decaf::util::concurrent::atomic::AtomicBoolean;
00053 
00054     class SessionConfig;
00055     class ActiveMQConnection;
00056     class ActiveMQConsumer;
00057     class ActiveMQMessage;
00058     class ActiveMQProducer;
00059     class ActiveMQConsumer;
00060     class ActiveMQSessionExecutor;
00061 
00062     class AMQCPP_API ActiveMQSession : public virtual cms::Session, public Dispatcher {
00063     private:
00064 
00065         typedef decaf::util::StlMap< Pointer<commands::ConsumerId>,
00066                                      ActiveMQConsumer*,
00067                                      commands::ConsumerId::COMPARATOR> ConsumersMap;
00068 
00069         friend class ActiveMQSessionExecutor;
00070 
00071     protected:
00072 
00073         SessionConfig* config;
00074 
00078         Pointer<commands::SessionInfo> sessionInfo;
00079 
00083         Pointer<ActiveMQTransactionContext> transaction;
00084 
00088         ActiveMQConnection* connection;
00089 
00093         ConsumersMap consumers;
00094 
00099         AtomicBoolean closed;
00100 
00104         std::auto_ptr<ActiveMQSessionExecutor> executor;
00105 
00109         cms::Session::AcknowledgeMode ackMode;
00110 
00114         util::LongSequenceGenerator producerIds;
00115 
00119         util::LongSequenceGenerator producerSequenceIds;
00120 
00124         util::LongSequenceGenerator consumerIds;
00125 
00129         long long lastDeliveredSequenceId;
00130 
00131     private:
00132 
00133         ActiveMQSession( const ActiveMQSession& );
00134         ActiveMQSession& operator= ( const ActiveMQSession& );
00135 
00136     public:
00137 
00138         ActiveMQSession( ActiveMQConnection* connection,
00139                          const Pointer<commands::SessionId>& id,
00140                          cms::Session::AcknowledgeMode ackMode,
00141                          const decaf::util::Properties& properties );
00142 
00143         virtual ~ActiveMQSession() throw();
00144 
00149         virtual void redispatch( MessageDispatchChannel& unconsumedMessages );
00150 
00154         void start();
00155 
00159         void stop();
00160 
00165         bool isStarted() const;
00166 
00167         virtual bool isAutoAcknowledge() const {
00168             return this->ackMode == cms::Session::AUTO_ACKNOWLEDGE;
00169         }
00170 
00171         virtual bool isDupsOkAcknowledge() const {
00172             return this->ackMode == cms::Session::DUPS_OK_ACKNOWLEDGE;
00173         }
00174 
00175         virtual bool isClientAcknowledge() const {
00176             return this->ackMode == cms::Session::CLIENT_ACKNOWLEDGE;
00177         }
00178 
00179         virtual bool isIndividualAcknowledge() const {
00180             return this->ackMode == cms::Session::INDIVIDUAL_ACKNOWLEDGE;
00181         }
00182 
00186         void fire( const exceptions::ActiveMQException& ex );
00187 
00188     public:  // Methods from ActiveMQMessageDispatcher
00189 
00194         virtual void dispatch( const Pointer<MessageDispatch>& message );
00195 
00196     public:   // Implements Methods
00197 
00198         virtual void close();
00199 
00200         virtual void commit();
00201 
00202         virtual void rollback();
00203 
00204         virtual void recover();
00205 
00206         virtual cms::MessageConsumer* createConsumer( const cms::Destination* destination );
00207 
00208         virtual cms::MessageConsumer* createConsumer( const cms::Destination* destination,
00209                                                       const std::string& selector );
00210 
00211         virtual cms::MessageConsumer* createConsumer( const cms::Destination* destination,
00212                                                       const std::string& selector,
00213                                                       bool noLocal );
00214 
00215         virtual cms::MessageConsumer* createDurableConsumer( const cms::Topic* destination,
00216                                                              const std::string& name,
00217                                                              const std::string& selector,
00218                                                              bool noLocal = false );
00219 
00220         virtual cms::MessageProducer* createProducer( const cms::Destination* destination );
00221 
00222         virtual cms::QueueBrowser* createBrowser( const cms::Queue* queue );
00223 
00224         virtual cms::QueueBrowser* createBrowser( const cms::Queue* queue, const std::string& selector );
00225 
00226         virtual cms::Queue* createQueue( const std::string& queueName );
00227 
00228         virtual cms::Topic* createTopic( const std::string& topicName );
00229 
00230         virtual cms::TemporaryQueue* createTemporaryQueue();
00231 
00232         virtual cms::TemporaryTopic* createTemporaryTopic();
00233 
00234         virtual cms::Message* createMessage();
00235 
00236         virtual cms::BytesMessage* createBytesMessage();
00237 
00238         virtual cms::BytesMessage* createBytesMessage( const unsigned char* bytes, int bytesSize );
00239 
00240         virtual cms::StreamMessage* createStreamMessage();
00241 
00242         virtual cms::TextMessage* createTextMessage();
00243 
00244         virtual cms::TextMessage* createTextMessage( const std::string& text );
00245 
00246         virtual cms::MapMessage* createMapMessage();
00247 
00248         virtual cms::Session::AcknowledgeMode getAcknowledgeMode() const;
00249 
00250         virtual bool isTransacted() const;
00251 
00252         virtual void unsubscribe( const std::string& name );
00253 
00254    public:   // ActiveMQSession specific Methods
00255 
00273         void send( cms::Message* message, ActiveMQProducer* producer, util::Usage* usage );
00274 
00282         cms::ExceptionListener* getExceptionListener();
00283 
00289         const commands::SessionInfo& getSessionInfo() const {
00290             this->checkClosed();
00291             return *( this->sessionInfo );
00292         }
00293 
00299         const commands::SessionId& getSessionId() const {
00300             this->checkClosed();
00301             return *( this->sessionInfo->getSessionId() );
00302         }
00303 
00307         ActiveMQConnection* getConnection() const {
00308             return this->connection;
00309         }
00310 
00314         Pointer<threads::Scheduler> getScheduler() const;
00315 
00321         long long getLastDeliveredSequenceId() const {
00322             return this->lastDeliveredSequenceId;
00323         }
00324 
00331         void setLastDeliveredSequenceId( long long value ) {
00332             this->lastDeliveredSequenceId = value;
00333         }
00334 
00344         void oneway( Pointer<commands::Command> command );
00345 
00360         Pointer<commands::Response> syncRequest( Pointer<commands::Command> command, unsigned int timeout = 0 );
00361 
00372         void addConsumer( ActiveMQConsumer* consumer );
00373 
00383         void removeConsumer( const Pointer<commands::ConsumerId>& consumerId );
00384 
00395         void addProducer( ActiveMQProducer* producer );
00396 
00406         void removeProducer( ActiveMQProducer* producer );
00407 
00415         virtual void doStartTransaction();
00416 
00422         Pointer<ActiveMQTransactionContext> getTransactionContext() {
00423             return this->transaction;
00424         }
00425 
00430         void acknowledge();
00431 
00436         void deliverAcks();
00437 
00442         void clearMessagesInProgress();
00443 
00447         void wakeup();
00448 
00453         Pointer<commands::ConsumerId> getNextConsumerId();
00454 
00459         Pointer<commands::ProducerId> getNextProducerId();
00460 
00467         void doClose();
00468 
00475         void dispose();
00476 
00477    private:
00478 
00483        long long getNextProducerSequenceId() {
00484            return this->producerSequenceIds.getNextSequenceId();
00485        }
00486 
00487        // Checks for the closed state and throws if so.
00488        void checkClosed() const;
00489 
00490        // Send the Destination Creation Request to the Broker, alerting it
00491        // that we've created a new Temporary Destination.
00492        // @param tempDestination - The new Temporary Destination
00493        void createTemporaryDestination( commands::ActiveMQTempDestination* tempDestination );
00494 
00495        // Send the Destination Destruction Request to the Broker, alerting
00496        // it that we've removed an existing Temporary Destination.
00497        // @param tempDestination - The Temporary Destination to remove
00498        void destroyTemporaryDestination( commands::ActiveMQTempDestination* tempDestination );
00499 
00500        // Creates a new Temporary Destination name using the connection id
00501        // and a rolling count.
00502        // @returns a unique Temporary Destination name
00503        std::string createTemporaryDestinationName();
00504 
00505     };
00506 
00507 }}
00508 
00509 #endif /*_ACTIVEMQ_CORE_ACTIVEMQSESSION_H_*/