activemq-cpp-3.3.0

src/main/activemq/core/ActiveMQConsumer.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_ACTIVEMQCONSUMER_H_
00018 #define _ACTIVEMQ_CORE_ACTIVEMQCONSUMER_H_
00019 
00020 #include <cms/MessageConsumer.h>
00021 #include <cms/MessageListener.h>
00022 #include <cms/Message.h>
00023 #include <cms/CMSException.h>
00024 
00025 #include <activemq/util/Config.h>
00026 #include <activemq/exceptions/ActiveMQException.h>
00027 #include <activemq/commands/ConsumerInfo.h>
00028 #include <activemq/commands/MessageAck.h>
00029 #include <activemq/commands/MessageDispatch.h>
00030 #include <activemq/core/Dispatcher.h>
00031 #include <activemq/core/RedeliveryPolicy.h>
00032 #include <activemq/core/MessageDispatchChannel.h>
00033 
00034 #include <decaf/util/concurrent/atomic/AtomicBoolean.h>
00035 #include <decaf/lang/Pointer.h>
00036 #include <decaf/util/concurrent/Mutex.h>
00037 
00038 namespace activemq{
00039 namespace core{
00040 
00041     using decaf::lang::Pointer;
00042     using decaf::util::concurrent::atomic::AtomicBoolean;
00043 
00044     class ActiveMQSession;
00045     class ActiveMQConsumerMembers;
00046 
00047     class AMQCPP_API ActiveMQConsumer : public cms::MessageConsumer,
00048                                         public Dispatcher
00049     {
00050     private:
00051 
00055         ActiveMQConsumerMembers* internal;
00056 
00060         ActiveMQSession* session;
00061 
00065         Pointer<commands::ConsumerInfo> consumerInfo;
00066 
00067     private:
00068 
00069         ActiveMQConsumer( const ActiveMQConsumer& );
00070         ActiveMQConsumer& operator= ( const ActiveMQConsumer& );
00071 
00072     public:
00073 
00077         ActiveMQConsumer( ActiveMQSession* session,
00078                           const Pointer<commands::ConsumerId>& id,
00079                           const Pointer<commands::ActiveMQDestination>& destination,
00080                           const std::string& name,
00081                           const std::string& selector,
00082                           int prefetch,
00083                           int maxPendingMessageCount,
00084                           bool noLocal,
00085                           bool browser,
00086                           bool dispatchAsync,
00087                           cms::MessageListener* listener );
00088 
00089         virtual ~ActiveMQConsumer() throw();
00090 
00091     public:  // Interface Implementation
00092 
00093         virtual void start();
00094 
00095         virtual void stop();
00096 
00097         virtual void close();
00098 
00099         virtual cms::Message* receive();
00100 
00101         virtual cms::Message* receive( int millisecs );
00102 
00103         virtual cms::Message* receiveNoWait();
00104 
00105         virtual void setMessageListener( cms::MessageListener* listener );
00106 
00107         virtual cms::MessageListener* getMessageListener() const;
00108 
00109         virtual std::string getMessageSelector() const;
00110 
00111         virtual void acknowledge( const Pointer<commands::MessageDispatch>& dispatch );
00112 
00113     public:  // Dispatcher Methods
00114 
00115         virtual void dispatch( const Pointer<MessageDispatch>& message );
00116 
00117     public:  // ActiveMQConsumer Methods
00118 
00124         void acknowledge();
00125 
00131         void commit();
00132 
00138         void rollback();
00139 
00145         void doClose();
00146 
00152         void dispose();
00153 
00158         const Pointer<commands::ConsumerInfo>& getConsumerInfo() const;
00159 
00164         const Pointer<commands::ConsumerId>& getConsumerId() const;
00165 
00169         bool isClosed() const;
00170 
00175         bool isSynchronizationRegistered() const ;
00176 
00181         void setSynchronizationRegistered( bool value );
00182 
00188         bool iterate();
00189 
00195         void deliverAcks();
00196 
00200         void clearMessagesInProgress();
00201 
00206         void inProgressClearRequired();
00207 
00213         long long getLastDeliveredSequenceId() const;
00214 
00221         void setLastDeliveredSequenceId( long long value );
00222 
00226         int getMessageAvailableCount() const;
00227 
00237         void setRedeliveryPolicy( RedeliveryPolicy* policy );
00238 
00245         RedeliveryPolicy* getRedeliveryPolicy() const;
00246 
00253         void setFailureError( decaf::lang::Exception* error );
00254 
00261         decaf::lang::Exception* getFailureError() const;
00262 
00263     protected:
00264 
00280         Pointer<MessageDispatch> dequeue( long long timeout );
00281 
00286         void beforeMessageIsConsumed(
00287             const Pointer<commands::MessageDispatch>& dispatch );
00288 
00294         void afterMessageIsConsumed(
00295             const Pointer<commands::MessageDispatch>& dispatch, bool messageExpired );
00296 
00297     private:
00298 
00299         // Using options from the Destination URI override any settings that are
00300         // defined for this consumer.
00301         void applyDestinationOptions( const Pointer<commands::ConsumerInfo>& info );
00302 
00303         // If supported sends a message pull request to the service provider asking
00304         // for the delivery of a new message.  This is used in the case where the
00305         // service provider has been configured with a zero prefetch or is only
00306         // capable of delivering messages on a pull basis.  No request is made if
00307         // there are already messages in the unconsumed queue since there's no need
00308         // for a server round-trip in that instance.
00309         void sendPullRequest( long long timeout );
00310 
00311         // Checks for the closed state and throws if so.
00312         void checkClosed() const;
00313 
00314         // Sends an ack as needed in order to keep them coming in if the current
00315         // ack mode allows the consumer to receive up to the prefetch limit before
00316         // an real ack is sent.
00317         void ackLater( const Pointer<commands::MessageDispatch>& message, int ackType );
00318 
00319         // Create an Ack Message that acks all messages that have been delivered so far.
00320         Pointer<commands::MessageAck> makeAckForAllDeliveredMessages( int type );
00321 
00322         // Should Acks be sent on each dispatched message
00323         bool isAutoAcknowledgeEach() const;
00324 
00325         // Can Acks be batched for less network overhead.
00326         bool isAutoAcknowledgeBatch() const;
00327 
00328     };
00329 
00330 }}
00331 
00332 #endif /*_ACTIVEMQ_CORE_ACTIVEMQCONSUMER_H_*/