activemq-cpp-3.6.0
CmsTemplate.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 
18 #ifndef _ACTIVEMQ_CMSUTIL_CMSTEMPLATE_H_
19 #define _ACTIVEMQ_CMSUTIL_CMSTEMPLATE_H_
20 
21 #include <activemq/util/Config.h>
26 #include <cms/ConnectionFactory.h>
27 #include <cms/DeliveryMode.h>
28 #include <string>
29 
30 namespace activemq {
31 namespace cmsutil {
32 
33  // Forward declarations.
34  class MessageCreator;
35 
61  public:
62 
67  static const long long RECEIVE_TIMEOUT_NO_WAIT;
68 
72  static const long long RECEIVE_TIMEOUT_INDEFINITE_WAIT;
73 
77  static const int DEFAULT_PRIORITY;
78 
82  static const long long DEFAULT_TIME_TO_LIVE;
83 
84  public:
85 
90  friend class ProducerExecutor;
92  protected:
93 
97 
98  private:
99 
101  ProducerExecutor& operator=(const ProducerExecutor&);
102 
103  public:
104 
106  CmsTemplate* parent,
107  cms::Destination* destination)
108  : SessionCallback(), action(action), parent(parent), destination(destination) {
109  }
110 
111  virtual ~ProducerExecutor() {}
112 
113  virtual void doInCms(cms::Session* session);
114 
115  virtual cms::Destination* getDestination(cms::Session* session AMQCPP_UNUSED) {
116  return destination;
117  }
118  };
119 
124  friend class ResolveProducerExecutor;
126  private:
127 
128  std::string destinationName;
129 
130  private:
131 
134 
135  public:
136 
138  CmsTemplate* parent,
139  const std::string& destinationName)
140  : ProducerExecutor(action, parent, NULL), destinationName(destinationName) {
141  }
142 
144 
145  virtual cms::Destination* getDestination(cms::Session* session);
146  };
147 
152  friend class SendExecutor;
154  private:
155 
156  MessageCreator* messageCreator;
157  CmsTemplate* parent;
158 
159  private:
160 
161  SendExecutor(const SendExecutor&);
162  SendExecutor& operator=(const SendExecutor&);
163 
164  public:
165 
166  SendExecutor(MessageCreator* messageCreator, CmsTemplate* parent) :
167  ProducerCallback(), messageCreator(messageCreator), parent(parent) {
168  }
169 
170  virtual ~SendExecutor() {
171  }
172 
173  virtual void doInCms(cms::Session* session, cms::MessageProducer* producer) {
174  parent->doSend(session, producer, messageCreator);
175  }
176  };
177 
182  friend class ReceiveExecutor;
184  protected:
185 
187  std::string selector;
188  bool noLocal;
191 
192  private:
193 
195  ReceiveExecutor& operator=(const ReceiveExecutor&);
196 
197  public:
198 
200  const std::string& selector, bool noLocal) :
201  SessionCallback(), destination(destination), selector(selector), noLocal(noLocal), message(NULL), parent(parent) {
202  }
203 
204  virtual ~ReceiveExecutor() {}
205 
206  virtual void doInCms(cms::Session* session);
207 
208  virtual cms::Destination* getDestination(cms::Session* session AMQCPP_UNUSED) {
209  return destination;
210  }
211 
213  return message;
214  }
215  };
216 
221  friend class ResolveReceiveExecutor;
223  private:
224 
225  std::string destinationName;
226 
227  private:
228 
231 
232  public:
233 
234  ResolveReceiveExecutor(CmsTemplate* parent, const std::string& selector,
235  bool noLocal, const std::string& destinationName) :
236  ReceiveExecutor(parent, NULL, selector, noLocal), destinationName(destinationName) {
237  }
238 
240 
241  virtual cms::Destination* getDestination(cms::Session* session);
242  };
243 
244  private:
245 
246  static const int NUM_SESSION_POOLS = (int)cms::Session::SESSION_TRANSACTED + 1;
247 
248  cms::Connection* connection;
249 
250  SessionPool* sessionPools[NUM_SESSION_POOLS];
251 
252  cms::Destination* defaultDestination;
253 
254  std::string defaultDestinationName;
255 
256  bool messageIdEnabled;
257 
258  bool messageTimestampEnabled;
259 
260  bool noLocal;
261 
262  long long receiveTimeout;
263 
264  bool explicitQosEnabled;
265 
266  int deliveryMode;
267 
268  int priority;
269 
270  long long timeToLive;
271 
272  bool initialized;
273 
274  private:
275 
276  CmsTemplate(const CmsTemplate&);
277  CmsTemplate& operator=(const CmsTemplate&);
278 
279  public:
280 
281  CmsTemplate();
282  CmsTemplate(cms::ConnectionFactory* connectionFactory);
283 
284  virtual ~CmsTemplate();
285 
295  virtual void setDefaultDestination(cms::Destination* defaultDestination) {
296  this->defaultDestination = defaultDestination;
297  }
298 
303  virtual const cms::Destination* getDefaultDestination() const {
304  return this->defaultDestination;
305  }
306 
312  return this->defaultDestination;
313  }
314 
324  virtual void setDefaultDestinationName(const std::string& defaultDestinationName) {
325  if (defaultDestinationName != this->defaultDestinationName) {
326  this->defaultDestination = NULL;
327  this->defaultDestinationName = defaultDestinationName;
328  }
329  }
330 
338  virtual const std::string getDefaultDestinationName() const {
339  return this->defaultDestinationName;
340  }
341 
349  virtual void setPubSubDomain(bool pubSubDomain) {
350  if (pubSubDomain != isPubSubDomain()) {
351  this->defaultDestination = NULL;
353  }
354  }
355 
356  virtual void setMessageIdEnabled(bool messageIdEnabled) {
357  this->messageIdEnabled = messageIdEnabled;
358  }
359 
360  virtual bool isMessageIdEnabled() const {
361  return this->messageIdEnabled;
362  }
363 
364  virtual void setMessageTimestampEnabled(bool messageTimestampEnabled) {
365  this->messageTimestampEnabled = messageTimestampEnabled;
366  }
367 
368  virtual bool isMessageTimestampEnabled() const {
369  return this->messageTimestampEnabled;
370  }
371 
372  virtual void setNoLocal(bool noLocal) {
373  this->noLocal = noLocal;
374  }
375 
376  virtual bool isNoLocal() const {
377  return this->noLocal;
378  }
379 
380  virtual void setReceiveTimeout(long long receiveTimeout) {
381  this->receiveTimeout = receiveTimeout;
382  }
383 
384  virtual long long getReceiveTimeout() const {
385  return this->receiveTimeout;
386  }
387 
396  virtual void setExplicitQosEnabled(bool explicitQosEnabled) {
397  this->explicitQosEnabled = explicitQosEnabled;
398  }
399 
412  virtual bool isExplicitQosEnabled() const {
413  return this->explicitQosEnabled;
414  }
415 
424  virtual void setDeliveryPersistent(bool deliveryPersistent) {
425  this->deliveryMode = (deliveryPersistent ? 0 : 1);
426  }
427 
436  virtual void setDeliveryMode(int deliveryMode) {
437  this->deliveryMode = deliveryMode;
438  }
439 
443  virtual int getDeliveryMode() const {
444  return this->deliveryMode;
445  }
446 
454  virtual void setPriority(int priority) {
455  this->priority = priority;
456  }
457 
461  virtual int getPriority() const {
462  return this->priority;
463  }
464 
473  virtual void setTimeToLive(long long timeToLive) {
474  this->timeToLive = timeToLive;
475  }
476 
480  virtual long long getTimeToLive() const {
481  return this->timeToLive;
482  }
483 
490  virtual void execute(SessionCallback* action);
491 
500  virtual void execute(ProducerCallback* action);
501 
512  virtual void execute(cms::Destination* dest, ProducerCallback* action);
513 
526  virtual void execute(const std::string& destinationName, ProducerCallback* action);
527 
535  virtual void send(MessageCreator* messageCreator);
536 
546  virtual void send(cms::Destination* dest, MessageCreator* messageCreator);
547 
557  virtual void send(const std::string& destinationName, MessageCreator* messageCreator);
558 
564  virtual cms::Message* receive();
565 
573  virtual cms::Message* receive(cms::Destination* destination);
574 
583  virtual cms::Message* receive(const std::string& destinationName);
584 
594  virtual cms::Message* receiveSelected(const std::string& selector);
595 
607  virtual cms::Message* receiveSelected(cms::Destination* destination, const std::string& selector);
608 
621  virtual cms::Message* receiveSelected(const std::string& destinationName, const std::string& selector);
622 
623  protected:
624 
625  void init();
626 
627  void destroy();
628 
629  private:
630 
634  void initDefaults();
635 
639  void createSessionPools();
640 
644  void destroySessionPools();
645 
652  void checkDefaultDestination();
653 
660  cms::Connection* getConnection();
661 
668  PooledSession* takeSession();
669 
676  void returnSession(PooledSession*& session);
677 
689  cms::MessageProducer* createProducer(cms::Session* session, cms::Destination* dest);
690 
697  void destroyProducer(cms::MessageProducer*& producer);
698 
710  cms::MessageConsumer* createConsumer(cms::Session* session, cms::Destination* dest,
711  const std::string& selector, bool noLocal);
712 
719  void destroyConsumer(cms::MessageConsumer*& consumer);
720 
726  void destroyMessage(cms::Message*& message);
727 
738  void doSend(cms::Session* session, cms::MessageProducer* producer, MessageCreator* messageCreator);
739 
747  cms::Message* doReceive(cms::MessageConsumer* consumer);
748 
756  cms::Destination* resolveDefaultDestination(cms::Session* session);
757 
758  };
759 
760 }}
761 
762 #endif /*_ACTIVEMQ_CMSUTIL_CMSTEMPLATE_H_*/