activemq-cpp-3.8.2
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_*/
ProducerExecutor(ProducerCallback *action, CmsTemplate *parent, cms::Destination *destination)
Definition: CmsTemplate.h:105
ReceiveExecutor(CmsTemplate *parent, cms::Destination *destination, const std::string &selector, bool noLocal)
Definition: CmsTemplate.h:199
SendExecutor(MessageCreator *messageCreator, CmsTemplate *parent)
Definition: CmsTemplate.h:166
Defines the interface for a factory that creates connection objects, the Connection objects returned ...
Definition: ConnectionFactory.h:41
ResolveProducerExecutor(ProducerCallback *action, CmsTemplate *parent, const std::string &destinationName)
Definition: CmsTemplate.h:137
Root of all messages.
Definition: Message.h:88
A Destination object encapsulates a provider-specific address.
Definition: Destination.h:39
#define AMQCPP_API
Definition: Config.h:30
#define NULL
Definition: Config.h:33
Callback for sending a message to a CMS destination.
Definition: ProducerCallback.h:33
virtual ~ProducerExecutor()
Definition: CmsTemplate.h:111
virtual const std::string getDefaultDestinationName() const
Gets the name of the default destination to be used for send/receive operations.
Definition: CmsTemplate.h:338
std::string selector
Definition: CmsTemplate.h:187
cms::Message * getMessage()
Definition: CmsTemplate.h:212
Messages will be consumed when the transaction commits.
Definition: Session.h:139
virtual int getDeliveryMode() const
Return the delivery mode to use when sending a message.
Definition: CmsTemplate.h:443
The client&#39;s connection to its provider.
Definition: Connection.h:70
Callback for executing any number of operations on a provided CMS Session.
Definition: SessionCallback.h:33
virtual ~ResolveProducerExecutor()
Definition: CmsTemplate.h:143
virtual long long getTimeToLive() const
Return the time-to-live of the message when sending.
Definition: CmsTemplate.h:480
A pool of CMS sessions from the same connection and with the same acknowledge mode.
Definition: SessionPool.h:39
static const long long RECEIVE_TIMEOUT_INDEFINITE_WAIT
Timeout value indicating a blocking receive without timeout.
Definition: CmsTemplate.h:72
static const long long DEFAULT_TIME_TO_LIVE
My default, messages should live forever.
Definition: CmsTemplate.h:82
virtual void setMessageIdEnabled(bool messageIdEnabled)
Definition: CmsTemplate.h:356
cms::Destination * destination
Definition: CmsTemplate.h:186
A Session object is a single-threaded context for producing and consuming messages.
Definition: Session.h:105
virtual long long getReceiveTimeout() const
Definition: CmsTemplate.h:384
virtual void setNoLocal(bool noLocal)
Definition: CmsTemplate.h:372
virtual void setDeliveryMode(int deliveryMode)
Set the delivery mode to use when sending a message.
Definition: CmsTemplate.h:436
virtual void setTimeToLive(long long timeToLive)
Set the time-to-live of the message when sending.
Definition: CmsTemplate.h:473
virtual cms::Destination * getDestination(cms::Session *session AMQCPP_UNUSED)
Definition: CmsTemplate.h:208
CmsTemplate * parent
Definition: CmsTemplate.h:95
virtual void setPubSubDomain(bool pubSubDomain)
Definition: CmsDestinationAccessor.h:68
cms::Destination * destination
Definition: CmsTemplate.h:96
virtual ~ReceiveExecutor()
Definition: CmsTemplate.h:204
virtual bool isMessageTimestampEnabled() const
Definition: CmsTemplate.h:368
ProducerCallback * action
Definition: CmsTemplate.h:94
virtual void setPriority(int priority)
Set the priority of a message when sending.
Definition: CmsTemplate.h:454
virtual cms::Destination * getDefaultDestination()
Retrieves the default destination to be used for send/receive operations.
Definition: CmsTemplate.h:311
virtual bool isNoLocal() const
Definition: CmsTemplate.h:376
virtual ~SendExecutor()
Definition: CmsTemplate.h:170
virtual void setMessageTimestampEnabled(bool messageTimestampEnabled)
Definition: CmsTemplate.h:364
CmsTemplate * parent
Definition: CmsTemplate.h:190
virtual ~ResolveReceiveExecutor()
Definition: CmsTemplate.h:239
A client uses a MessageProducer object to send messages to a Destination.
Definition: MessageProducer.h:60
virtual const cms::Destination * getDefaultDestination() const
Retrieves the default destination to be used for send/receive operations.
Definition: CmsTemplate.h:303
virtual void setExplicitQosEnabled(bool explicitQosEnabled)
Set if the QOS values (deliveryMode, priority, timeToLive) should be used for sending a message...
Definition: CmsTemplate.h:396
A client uses a MessageConsumer to received messages from a destination.
Definition: MessageConsumer.h:63
CmsTemplate simplifies performing synchronous CMS operations.
Definition: CmsTemplate.h:60
bool noLocal
Definition: CmsTemplate.h:188
cms::Message * message
Definition: CmsTemplate.h:189
virtual int getPriority() const
Return the priority of a message when sending.
Definition: CmsTemplate.h:461
Licensed to the Apache Software Foundation (ASF) under one or more contributor license agreements...
Definition: CachedConsumer.h:24
virtual void setDeliveryPersistent(bool deliveryPersistent)
Set whether message delivery should be persistent or non-persistent, specified as boolean value ("tru...
Definition: CmsTemplate.h:424
ResolveReceiveExecutor(CmsTemplate *parent, const std::string &selector, bool noLocal, const std::string &destinationName)
Definition: CmsTemplate.h:234
virtual void setReceiveTimeout(long long receiveTimeout)
Definition: CmsTemplate.h:380
virtual void setDefaultDestination(cms::Destination *defaultDestination)
Sets the destination object to be used by default for send/receive operations.
Definition: CmsTemplate.h:295
Extends the CmsAccessor to add support for resolving destination names.
Definition: CmsDestinationAccessor.h:35
virtual void setPubSubDomain(bool pubSubDomain)
Indicates whether the default destination is a topic (true) or a queue (false).
Definition: CmsTemplate.h:349
virtual cms::Destination * getDestination(cms::Session *session AMQCPP_UNUSED)
Definition: CmsTemplate.h:115
static const int DEFAULT_PRIORITY
Default message priority.
Definition: CmsTemplate.h:77
A pooled session object that wraps around a delegate session.
Definition: PooledSession.h:37
virtual void setDefaultDestinationName(const std::string &defaultDestinationName)
Sets the name of the default destination to be used from send/receive operations. ...
Definition: CmsTemplate.h:324
virtual void doInCms(cms::Session *session, cms::MessageProducer *producer)
Execute an action given a session and producer.
Definition: CmsTemplate.h:173
Creates the user-defined message to be sent by the CmsTemplate.
Definition: MessageCreator.h:34
virtual bool isMessageIdEnabled() const
Definition: CmsTemplate.h:360
static const long long RECEIVE_TIMEOUT_NO_WAIT
Timeout value indicating that a receive operation should check if a message is immediately available ...
Definition: CmsTemplate.h:67
virtual bool isExplicitQosEnabled() const
If "true", then the values of deliveryMode, priority, and timeToLive will be used when sending a mess...
Definition: CmsTemplate.h:412