activemq-cpp-3.6.0
ActiveMQConsumerKernel.h
Go to the documentation of this file.
1 /*
2  * Licensed to the Apache Software Foundation (ASF) under one or more
3  * contributor license agreements. See the NOTICE file distributed with
4  * this work for additional information regarding copyright ownership.
5  * The ASF licenses this file to You under the Apache License, Version 2.0
6  * (the "License"); you may not use this file except in compliance with
7  * the License. You may obtain a copy of the License at
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  */
17 #ifndef _ACTIVEMQ_CORE_KERNELS_ACTIVEMQCONSUMERKERNEL_H_
18 #define _ACTIVEMQ_CORE_KERNELS_ACTIVEMQCONSUMERKERNEL_H_
19 
20 #include <cms/MessageConsumer.h>
21 #include <cms/MessageListener.h>
23 #include <cms/Message.h>
24 #include <cms/CMSException.h>
25 
26 #include <activemq/util/Config.h>
34 
36 #include <decaf/lang/Pointer.h>
38 
39 namespace activemq {
40 namespace core {
41 namespace kernels {
42 
45 
46  class ActiveMQSessionKernel;
47  class ActiveMQConsumerKernelConfig;
48 
50  private:
51 
55  ActiveMQConsumerKernelConfig* internal;
56 
60  ActiveMQSessionKernel* session;
61 
66 
67  private:
68 
71 
72  public:
73 
76  const Pointer<commands::ActiveMQDestination>& destination,
77  const std::string& name,
78  const std::string& selector,
79  int prefetch,
80  int maxPendingMessageCount,
81  bool noLocal,
82  bool browser,
83  bool dispatchAsync,
84  cms::MessageListener* listener);
85 
86  virtual ~ActiveMQConsumerKernel();
87 
88  public: // Interface Implementation
89 
90  virtual void start();
91 
92  virtual void stop();
93 
94  virtual void close();
95 
96  virtual cms::Message* receive();
97 
98  virtual cms::Message* receive(int millisecs);
99 
100  virtual cms::Message* receiveNoWait();
101 
102  virtual void setMessageListener(cms::MessageListener* listener);
103 
104  virtual cms::MessageListener* getMessageListener() const;
105 
106  virtual void setMessageAvailableListener(cms::MessageAvailableListener* listener);
107 
108  virtual cms::MessageAvailableListener* getMessageAvailableListener() const;
109 
110  virtual std::string getMessageSelector() const;
111 
112  virtual void acknowledge(const Pointer<commands::MessageDispatch>& dispatch);
113 
114  virtual void setMessageTransformer(cms::MessageTransformer* transformer);
115 
116  virtual cms::MessageTransformer* getMessageTransformer() const;
117 
118  public: // Dispatcher Methods
119 
120  virtual void dispatch( const Pointer<MessageDispatch>& message );
121 
122  public: // ActiveMQConsumerKernel Methods
123 
129  void acknowledge();
130 
136  void commit();
137 
143  void rollback();
144 
150  void doClose();
151 
157  void dispose();
158 
163  const Pointer<commands::ConsumerInfo>& getConsumerInfo() const;
164 
169  const Pointer<commands::ConsumerId>& getConsumerId() const;
170 
174  bool isClosed() const;
175 
180  bool isSynchronizationRegistered() const ;
181 
186  void setSynchronizationRegistered(bool value);
187 
193  bool iterate();
194 
200  void deliverAcks();
201 
205  void clearMessagesInProgress();
206 
211  void inProgressClearRequired();
212 
218  long long getLastDeliveredSequenceId() const;
219 
226  void setLastDeliveredSequenceId(long long value);
227 
231  int getMessageAvailableCount() const;
232 
242  void setRedeliveryPolicy(RedeliveryPolicy* policy);
243 
250  RedeliveryPolicy* getRedeliveryPolicy() const;
251 
258  void setFailureError(decaf::lang::Exception* error);
259 
266  decaf::lang::Exception* getFailureError() const;
267 
272  void setPrefetchSize(int prefetchSize);
273 
279  bool isInUse(Pointer<commands::ActiveMQDestination> destination) const;
280 
281  protected:
282 
298  Pointer<MessageDispatch> dequeue(long long timeout);
299 
304  void beforeMessageIsConsumed(const Pointer<commands::MessageDispatch>& dispatch);
305 
311  void afterMessageIsConsumed(const Pointer<commands::MessageDispatch>& dispatch, bool messageExpired);
312 
313  private:
314 
315  // Creates a deliverable cms::Message from a received MessageDispatch, transforming if needed
316  // and configuring appropriate ack handlers.
318 
319  // Using options from the Destination URI override any settings that are
320  // defined for this consumer.
321  void applyDestinationOptions(const Pointer<commands::ConsumerInfo>& info);
322 
323  // If supported sends a message pull request to the service provider asking
324  // for the delivery of a new message. This is used in the case where the
325  // service provider has been configured with a zero prefetch or is only
326  // capable of delivering messages on a pull basis. No request is made if
327  // there are already messages in the unconsumed queue since there's no need
328  // for a server round-trip in that instance.
329  void sendPullRequest(long long timeout);
330 
331  // Checks for the closed state and throws if so.
332  void checkClosed() const;
333 
334  // Sends an ack as needed in order to keep them coming in if the current
335  // ack mode allows the consumer to receive up to the prefetch limit before
336  // an real ack is sent.
337  void ackLater(const Pointer<commands::MessageDispatch>& message, int ackType);
338 
339  // Create an Ack Message that acks all messages that have been delivered so far.
340  Pointer<commands::MessageAck> makeAckForAllDeliveredMessages(int type);
341 
342  // Should Acks be sent on each dispatched message
343  bool isAutoAcknowledgeEach() const;
344 
345  // Can Acks be batched for less network overhead.
346  bool isAutoAcknowledgeBatch() const;
347 
348  };
349 
350 }}}
351 
352 #endif /* _ACTIVEMQ_CORE_KERNELS_ACTIVEMQCONSUMERKERNEL_H_ */