Added onSize() method to DeferredGet

This commit is contained in:
Emiel Bruijntjes 2014-08-28 10:02:01 +02:00
parent c7b3f71f14
commit ebcdd71848
3 changed files with 29 additions and 0 deletions

View File

@ -24,6 +24,7 @@ using EmptyCallback = std::function<void()>;
using MessageCallback = std::function<void(const Message &message, uint64_t deliveryTag, bool redelivered)>;
using QueueCallback = std::function<void(const std::string &name, uint32_t messagecount, uint32_t consumercount)>;
using DeleteCallback = std::function<void(uint32_t deletedmessages)>;
using SizeCallback = std::function<void(uint32_t messagecount)>;
using ConsumeCallback = std::function<void(const std::string &consumer)>;
using CancelCallback = std::function<void(const std::string &consumer)>;

View File

@ -33,6 +33,12 @@ private:
* @var EmptyCallback
*/
EmptyCallback _emptyCallback;
/**
* Callback with the number of messages still in the queue
* @var SizeCallback
*/
SizeCallback _sizeCallback;
/**
* Report success when a message is indeed expected
@ -121,6 +127,19 @@ public:
// allow chaining
return *this;
}
/**
* Register a function to be called when size information is known
* @param callback the callback to execute
*/
DeferredGet &onSize(const SizeCallback &callback)
{
// store callback
_sizeCallback = callback;
// allow chaining
return *this;
}
};
/**

View File

@ -28,6 +28,12 @@ Deferred *DeferredGet::reportSuccess(uint32_t messageCount) const
auto messageCallback = _messageCallback;
auto finalizeCallback = _finalizeCallback;
auto *channel = _channel;
// install a monitor because the channel could be destructed
Monitor monitor(channel);
// report the size (technically, the channel object could be destructed now, but we ignore that case)
if (_sizeCallback) _sizeCallback(messageCount);
// we now know the name, so we can install the message callback on the channel
_channel->install("", [channel, messageCallback, finalizeCallback](const Message &message, uint64_t deliveryTag, bool redelivered) {
@ -54,6 +60,9 @@ Deferred *DeferredGet::reportSuccess(uint32_t messageCount) const
*/
Deferred *DeferredGet::reportSuccess() const
{
// report the size
if (_sizeCallback) _sizeCallback(0);
// check if a callback was set
if (_emptyCallback) _emptyCallback();