summaryrefslogtreecommitdiff
path: root/qpid/cpp/include/qpid/client/SubscriptionManager.h
blob: b69819a8ff09cdb4bf60897b50df9ea22cda32a7 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
#ifndef QPID_CLIENT_SUBSCRIPTIONMANAGER_H
#define QPID_CLIENT_SUBSCRIPTIONMANAGER_H

/*
 *
 * Licensed to the Apache Software Foundation (ASF) under one
 * or more contributor license agreements.  See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership.  The ASF licenses this file
 * to you under the Apache License, Version 2.0 (the
 * "License"); you may not use this file except in compliance
 * with the License.  You may obtain a copy of the License at
 *
 *   http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing,
 * software distributed under the License is distributed on an
 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 * KIND, either express or implied.  See the License for the
 * specific language governing permissions and limitations
 * under the License.
 *
 */

#include "qpid/client/Session.h"
#include "qpid/client/Subscription.h"
#include "qpid/sys/Runnable.h"
#include "qpid/sys/Thread.h"
#include "qpid/client/ClientImportExport.h"
#include "qpid/client/MessageListener.h"
#include "qpid/client/LocalQueue.h"
#include "qpid/client/Handle.h"
#include <string>

namespace qpid {
namespace client {

class SubscriptionManagerImpl;

/**
 * A class to help create and manage subscriptions.
 *
 * Set up your subscriptions, then call run() to have messages
 * delivered.
 *
 * \ingroup clientapi
 *
 * \details
 *
 * <h2>Subscribing and canceling subscriptions</h2>
 *
 * <ul>
 * <li>
 * <p>subscribe()</p>
 * <pre> SubscriptionManager subscriptions(session);
 * Listener listener(subscriptions);
 * subscriptions.subscribe(listener, myQueue);</pre>
 * <pre> SubscriptionManager subscriptions(session);
 * LocalQueue local_queue;
 * subscriptions.subscribe(local_queue, string("message_queue"));</pre></li>
 * <li>
 * <p>cancel()</p>
 * <pre>subscriptions.cancel();</pre></li>
 * </ul>
 *
 * <h2>Waiting for messages (and returning)</h2>
 *
 * <ul>
 * <li>
 * <p>run()</p>
 * <pre> // Give up control to receive messages
 * subscriptions.run();</pre></li>
 * <li>
 * <p>stop()</p>
 * <pre>.// Use this code in a listener to return from run()
 * subscriptions.stop();</pre></li>
 * <li>
 * <p>setAutoStop()</p>
 * <pre>.// Return from subscriptions.run() when last subscription is cancelled
 *.subscriptions.setAutoStop(true);
 *.subscriptons.run();
 * </pre></li>
 * <li>
 * <p>Ending a subscription in a listener</p>
 * <pre>
 * void Listener::received(Message&amp; message) {
 *
 *  if (message.getData() == "That's all, folks!") {
 *       subscriptions.cancel(message.getDestination());
 *   }
 * }
 * </pre>
 * </li>
 * </ul>
 *
 */
class QPID_CLIENT_CLASS_EXTERN SubscriptionManager : public sys::Runnable, public Handle<SubscriptionManagerImpl>
{
  public:
    /** Create a new SubscriptionManager associated with a session */
    QPID_CLIENT_EXTERN SubscriptionManager(const Session& session);
    QPID_CLIENT_EXTERN SubscriptionManager(const SubscriptionManager&);
    QPID_CLIENT_EXTERN ~SubscriptionManager();
    QPID_CLIENT_EXTERN SubscriptionManager& operator=(const SubscriptionManager&);

    /**
     * Subscribe a MessagesListener to receive messages from queue.
     *
     * Provide your own subclass of MessagesListener to process
     * incoming messages. It will be called for each message received.
     *
     *@param listener Listener object to receive messages.
     *@param queue Name of the queue to subscribe to.
     *@param settings settings for the subscription.
     *@param name unique destination name for the subscription, defaults to queue name.
     */
    QPID_CLIENT_EXTERN Subscription subscribe(MessageListener& listener,
                           const std::string& queue,
                           const SubscriptionSettings& settings,
                           const std::string& name=std::string());

    /**
     * Subscribe a LocalQueue to receive messages from queue.
     *
     * Incoming messages are stored in the queue for you to retrieve.
     *
     *@param queue Name of the queue to subscribe to.
     *@param flow initial FlowControl for the subscription.
     *@param name unique destination name for the subscription, defaults to queue name.
     * If not specified, the queue name is used.
     */
    QPID_CLIENT_EXTERN Subscription subscribe(LocalQueue& localQueue,
                           const std::string& queue,
                           const SubscriptionSettings& settings,
                           const std::string& name=std::string());

    /**
     * Subscribe a MessagesListener to receive messages from queue.
     *
     * Provide your own subclass of MessagesListener to process
     * incoming messages. It will be called for each message received.
     *
     *@param listener Listener object to receive messages.
     *@param queue Name of the queue to subscribe to.
     *@param name unique destination name for the subscription, defaults to queue name.
     * If not specified, the queue name is used.
     */
    QPID_CLIENT_EXTERN Subscription subscribe(MessageListener& listener,
                           const std::string& queue,
                           const std::string& name=std::string());

    /**
     * Subscribe a LocalQueue to receive messages from queue.
     *
     * Incoming messages are stored in the queue for you to retrieve.
     *
     *@param queue Name of the queue to subscribe to.
     *@param name unique destination name for the subscription, defaults to queue name.
     * If not specified, the queue name is used.
     */
    QPID_CLIENT_EXTERN Subscription subscribe(LocalQueue& localQueue,
                           const std::string& queue,
                           const std::string& name=std::string());


    /** Get a single message from a queue.
     * (Note: this currently uses a subscription per invocation and is
     * thus relatively expensive. The subscription is cancelled as
     * part of each call which can trigger auto-deletion).
     *@param result is set to the message from the queue.
     *@param timeout wait up this timeout for a message to appear.
     *@return true if result was set, false if no message available after timeout.
     */
    QPID_CLIENT_EXTERN bool get(Message& result, const std::string& queue, sys::Duration timeout=0);

    /** Get a single message from a queue.
     * (Note: this currently uses a subscription per invocation and is
     * thus relatively expensive. The subscription is cancelled as
     * part of each call which can trigger auto-deletion).
     *@param timeout wait up this timeout for a message to appear.
     *@return message from the queue.
     *@throw Exception if the timeout is exceeded.
     */
    QPID_CLIENT_EXTERN Message get(const std::string& queue, sys::Duration timeout=sys::TIME_INFINITE);

    /** Get a subscription by name.
     *@throw Exception if not found.
     */
    QPID_CLIENT_EXTERN Subscription getSubscription(const std::string& name) const;

    /** Cancel a subscription. See also: Subscription.cancel() */
    QPID_CLIENT_EXTERN void cancel(const std::string& name);

    /** Deliver messages in the current thread until stop() is called.
     * Only one thread may be running in a SubscriptionManager at a time.
     * @see run
     */
    QPID_CLIENT_EXTERN void run();

    /** Start a new thread to deliver messages.
     * Only one thread may be running in a SubscriptionManager at a time.
     * @see start
     */
    QPID_CLIENT_EXTERN void start();

    /**
     * Wait for the thread started by a call to start() to complete.
     */
    QPID_CLIENT_EXTERN void wait();

    /** If set true, run() will stop when all subscriptions
     * are cancelled. If false, run will only stop when stop()
     * is called. True by default.
     */
    QPID_CLIENT_EXTERN void setAutoStop(bool set=true);

    /** Stop delivery. Causes run() to return, or the thread started with start() to exit. */
    QPID_CLIENT_EXTERN void stop();

    static const uint32_t UNLIMITED=0xFFFFFFFF;

    /** Set the flow control for a subscription. */
    QPID_CLIENT_EXTERN void setFlowControl(const std::string& name, const FlowControl& flow);

    /** Set the flow control for a subscription.
     *@param name: name of the subscription.
     *@param messages: message credit.
     *@param bytes: byte credit.
     *@param window: if true use window-based flow control.
     */
    QPID_CLIENT_EXTERN void setFlowControl(const std::string& name, uint32_t messages,  uint32_t bytes, bool window=true);

    /** Set the default settings for subscribe() calls that don't
     * include a SubscriptionSettings parameter.
     */
    QPID_CLIENT_EXTERN void setDefaultSettings(const SubscriptionSettings& s);

    /** Get the default settings for subscribe() calls that don't
     * include a SubscriptionSettings parameter.
     */
    QPID_CLIENT_EXTERN const SubscriptionSettings& getDefaultSettings() const;

    /** Get the default settings for subscribe() calls that don't
     * include a SubscriptionSettings parameter.
     */
    QPID_CLIENT_EXTERN SubscriptionSettings& getDefaultSettings();

    /**
     * Set the default flow control settings for subscribe() calls
     * that don't include a SubscriptionSettings parameter.
     *
     *@param messages: message credit.
     *@param bytes: byte credit.
     *@param window: if true use window-based flow control.
     */
    QPID_CLIENT_EXTERN void setFlowControl(uint32_t messages,  uint32_t bytes, bool window=true);

    /**
     *Set the default accept-mode for subscribe() calls that don't
     *include a SubscriptionSettings parameter.
     */
    QPID_CLIENT_EXTERN void setAcceptMode(AcceptMode mode);

    /**
     * Set the default acquire-mode subscribe()s that don't specify SubscriptionSettings.
     */
    QPID_CLIENT_EXTERN void setAcquireMode(AcquireMode mode);

    QPID_CLIENT_EXTERN void registerFailoverHandler ( boost::function<void ()> fh );

    QPID_CLIENT_EXTERN Session getSession() const;

    SubscriptionManager(SubscriptionManagerImpl*); ///<@internal

  private:
    typedef SubscriptionManagerImpl Impl;
    friend class PrivateImplRef<SubscriptionManager>;
};

/** AutoCancel cancels a subscription in its destructor */
class AutoCancel {
  public:
    AutoCancel(SubscriptionManager& sm_, const std::string& tag_) : sm(sm_), tag(tag_) {}
    ~AutoCancel() { sm.cancel(tag); }
  private:
    SubscriptionManager& sm;
    std::string tag;
};

}} // namespace qpid::client

#endif  /*!QPID_CLIENT_SUBSCRIPTIONMANAGER_H*/