activemq-cpp-3.9.5
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
27#include <cms/DeliveryMode.h>
28#include <string>
29
30namespace activemq {
31namespace cmsutil {
32
33 // Forward declarations.
34 class MessageCreator;
35
60 class AMQCPP_API CmsTemplate : public CmsDestinationAccessor {
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
89 class ProducerExecutor;
90 friend class ProducerExecutor;
91 class ProducerExecutor : public SessionCallback {
92 protected:
93
95 CmsTemplate* parent;
97
98 private:
99
100 ProducerExecutor(const ProducerExecutor&);
101 ProducerExecutor& operator=(const ProducerExecutor&);
102
103 public:
104
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
123 class ResolveProducerExecutor;
125 class ResolveProducerExecutor : public ProducerExecutor {
126 private:
127
128 std::string destinationName;
129
130 private:
131
132 ResolveProducerExecutor(const ResolveProducerExecutor&);
133 ResolveProducerExecutor& operator=(const ResolveProducerExecutor&);
134
135 public:
136
138 CmsTemplate* parent,
139 const std::string& destinationName)
140 : ProducerExecutor(action, parent, NULL), destinationName(destinationName) {
141 }
142
144
146 };
147
151 class SendExecutor;
152 friend class SendExecutor;
153 class SendExecutor : public ProducerCallback {
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
181 class ReceiveExecutor;
182 friend class ReceiveExecutor;
183 class ReceiveExecutor : public SessionCallback {
184 protected:
185
187 std::string selector;
190 CmsTemplate* parent;
191
192 private:
193
194 ReceiveExecutor(const ReceiveExecutor&);
195 ReceiveExecutor& operator=(const ReceiveExecutor&);
196
197 public:
198
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
220 class ResolveReceiveExecutor;
222 class ResolveReceiveExecutor : public ReceiveExecutor {
223 private:
224
225 std::string destinationName;
226
227 private:
228
229 ResolveReceiveExecutor(const ResolveReceiveExecutor&);
230 ResolveReceiveExecutor& operator=(const ResolveReceiveExecutor&);
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
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
283
284 virtual ~CmsTemplate();
285
295 virtual void setDefaultDestination(cms::Destination* defaultDestination) {
296 this->defaultDestination = defaultDestination;
297 }
298
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
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_*/
#define AMQCPP_API
Definition Config.h:30
virtual bool isPubSubDomain() const
Definition CmsDestinationAccessor.h:64
virtual void setPubSubDomain(bool pubSubDomain)
Definition CmsDestinationAccessor.h:68
ProducerExecutor(ProducerCallback *action, CmsTemplate *parent, cms::Destination *destination)
Definition CmsTemplate.h:105
virtual ~ProducerExecutor()
Definition CmsTemplate.h:111
virtual cms::Destination * getDestination(cms::Session *session AMQCPP_UNUSED)
Definition CmsTemplate.h:115
cms::Destination * destination
Definition CmsTemplate.h:96
ProducerCallback * action
Definition CmsTemplate.h:94
virtual void doInCms(cms::Session *session)
Execute any number of operations against the supplied CMS session.
CmsTemplate * parent
Definition CmsTemplate.h:95
cms::Destination * destination
Definition CmsTemplate.h:186
ReceiveExecutor(CmsTemplate *parent, cms::Destination *destination, const std::string &selector, bool noLocal)
Definition CmsTemplate.h:199
virtual void doInCms(cms::Session *session)
Execute any number of operations against the supplied CMS session.
virtual ~ReceiveExecutor()
Definition CmsTemplate.h:204
virtual cms::Destination * getDestination(cms::Session *session AMQCPP_UNUSED)
Definition CmsTemplate.h:208
cms::Message * getMessage()
Definition CmsTemplate.h:212
CmsTemplate * parent
Definition CmsTemplate.h:190
bool noLocal
Definition CmsTemplate.h:188
std::string selector
Definition CmsTemplate.h:187
cms::Message * message
Definition CmsTemplate.h:189
virtual cms::Destination * getDestination(cms::Session *session)
ResolveProducerExecutor(ProducerCallback *action, CmsTemplate *parent, const std::string &destinationName)
Definition CmsTemplate.h:137
virtual ~ResolveProducerExecutor()
Definition CmsTemplate.h:143
virtual cms::Destination * getDestination(cms::Session *session)
ResolveReceiveExecutor(CmsTemplate *parent, const std::string &selector, bool noLocal, const std::string &destinationName)
Definition CmsTemplate.h:234
virtual ~ResolveReceiveExecutor()
Definition CmsTemplate.h:239
virtual ~SendExecutor()
Definition CmsTemplate.h:170
SendExecutor(MessageCreator *messageCreator, CmsTemplate *parent)
Definition CmsTemplate.h:166
virtual void doInCms(cms::Session *session, cms::MessageProducer *producer)
Execute an action given a session and producer.
Definition CmsTemplate.h:173
virtual void setExplicitQosEnabled(bool explicitQosEnabled)
Set if the QOS values (deliveryMode, priority, timeToLive) should be used for sending a message.
Definition CmsTemplate.h:396
static const long long DEFAULT_TIME_TO_LIVE
My default, messages should live forever.
Definition CmsTemplate.h:82
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 void execute(ProducerCallback *action)
Executes the given action and provides it with a CMS Session and producer.
virtual void setDeliveryMode(int deliveryMode)
Set the delivery mode to use when sending a message.
Definition CmsTemplate.h:436
virtual void execute(cms::Destination *dest, ProducerCallback *action)
Executes the given action and provides it with a CMS Session and producer.
virtual void send(MessageCreator *messageCreator)
Convenience method for sending a message to the default destination.
virtual cms::Message * receiveSelected(const std::string &destinationName, const std::string &selector)
Performs a synchronous read from the specified destination, consuming only messages identified by the...
virtual long long getTimeToLive() const
Return the time-to-live of the message when sending.
Definition CmsTemplate.h:480
virtual void setReceiveTimeout(long long receiveTimeout)
Definition CmsTemplate.h:380
CmsTemplate(cms::ConnectionFactory *connectionFactory)
virtual void setDefaultDestination(cms::Destination *defaultDestination)
Sets the destination object to be used by default for send/receive operations.
Definition CmsTemplate.h:295
virtual void send(cms::Destination *dest, MessageCreator *messageCreator)
Convenience method for sending a message to the specified destination.
virtual void execute(const std::string &destinationName, ProducerCallback *action)
Executes the given action and provides it with a CMS Session and producer.
virtual cms::Message * receiveSelected(cms::Destination *destination, const std::string &selector)
Performs a synchronous read from the specified destination, consuming only messages identified by the...
static const int DEFAULT_PRIORITY
Default message priority.
Definition CmsTemplate.h:77
virtual bool isNoLocal() const
Definition CmsTemplate.h:376
virtual cms::Message * receive()
Performs a synchronous read from the default destination.
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 const cms::Destination * getDefaultDestination() const
Retrieves the default destination to be used for send/receive operations.
Definition CmsTemplate.h:303
virtual cms::Destination * getDefaultDestination()
Retrieves the default destination to be used for send/receive operations.
Definition CmsTemplate.h:311
virtual void setTimeToLive(long long timeToLive)
Set the time-to-live of the message when sending.
Definition CmsTemplate.h:473
static const long long RECEIVE_TIMEOUT_INDEFINITE_WAIT
Timeout value indicating a blocking receive without timeout.
Definition CmsTemplate.h:72
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
virtual int getDeliveryMode() const
Return the delivery mode to use when sending a message.
Definition CmsTemplate.h:443
void destroy()
Shuts down this object and destroys any allocated resources.
virtual long long getReceiveTimeout() const
Definition CmsTemplate.h:384
virtual const std::string getDefaultDestinationName() const
Gets the name of the default destination to be used for send/receive operations.
Definition CmsTemplate.h:338
virtual void execute(SessionCallback *action)
Executes the given action within a CMS Session.
virtual bool isMessageTimestampEnabled() const
Definition CmsTemplate.h:368
virtual cms::Message * receive(cms::Destination *destination)
Performs a synchronous read from the specified destination.
virtual cms::Message * receive(const std::string &destinationName)
Performs a synchronous read from the specified destination.
void init()
Initializes this object and prepares it for use.
virtual int getPriority() const
Return the priority of a message when sending.
Definition CmsTemplate.h:461
virtual bool isMessageIdEnabled() const
Definition CmsTemplate.h:360
virtual void setPriority(int priority)
Set the priority of a message when sending.
Definition CmsTemplate.h:454
virtual void setPubSubDomain(bool pubSubDomain)
Indicates whether the default destination is a topic (true) or a queue (false).
Definition CmsTemplate.h:349
virtual void setNoLocal(bool noLocal)
Definition CmsTemplate.h:372
virtual cms::Message * receiveSelected(const std::string &selector)
Performs a synchronous read consuming only messages identified by the given selector.
virtual void setMessageTimestampEnabled(bool messageTimestampEnabled)
Definition CmsTemplate.h:364
virtual void send(const std::string &destinationName, MessageCreator *messageCreator)
Convenience method for sending a message to the specified destination.
virtual void setMessageIdEnabled(bool messageIdEnabled)
Definition CmsTemplate.h:356
virtual void setDeliveryPersistent(bool deliveryPersistent)
Set whether message delivery should be persistent or non-persistent, specified as boolean value ("tru...
Definition CmsTemplate.h:424
Creates the user-defined message to be sent by the CmsTemplate.
Definition MessageCreator.h:34
A pooled session object that wraps around a delegate session.
Definition PooledSession.h:37
Callback for sending a message to a CMS destination.
Definition ProducerCallback.h:33
Callback for executing any number of operations on a provided CMS Session.
Definition SessionCallback.h:33
A pool of CMS sessions from the same connection and with the same acknowledge mode.
Definition SessionPool.h:39
Defines the interface for a factory that creates connection objects, the Connection objects returned ...
Definition ConnectionFactory.h:41
The client's connection to its provider.
Definition Connection.h:70
A Destination object encapsulates a provider-specific address.
Definition Destination.h:39
A client uses a MessageConsumer to received messages from a destination.
Definition MessageConsumer.h:63
Root of all messages.
Definition Message.h:88
A client uses a MessageProducer object to send messages to a Destination.
Definition MessageProducer.h:60
A Session object is a single-threaded context for producing and consuming messages.
Definition Session.h:105
@ SESSION_TRANSACTED
Messages will be consumed when the transaction commits.
Definition Session.h:139
#define NULL
Definition Config.h:33
Definition CachedConsumer.h:25
Licensed to the Apache Software Foundation (ASF) under one or more contributor license agreements.
Definition CachedConsumer.h:24