ReadBufferFromRabbitMQConsumer.h 2.0 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
#pragma once

#include <Core/Names.h>
#include <Core/Types.h>
#include <IO/ReadBuffer.h>
#include <amqpcpp.h>
#include <Storages/RabbitMQ/RabbitMQHandler.h>
#include <event2/event.h>

namespace Poco
{
    class Logger;
}

namespace DB
{

using ChannelPtr = std::shared_ptr<AMQP::TcpChannel>;

class ReadBufferFromRabbitMQConsumer : public ReadBuffer
{

public:
    ReadBufferFromRabbitMQConsumer(
25 26
            ChannelPtr consumer_channel_,
            RabbitMQHandler & eventHandler_,
27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46
            const String & exchange_name_,
            const String & routing_key_,
            const size_t channel_id_,
            Poco::Logger * log_,
            char row_delimiter_,
            const bool bind_by_id_,
            const bool hash_exchange_,
            const size_t num_queues_,
            const std::atomic<bool> & stopped_);

    ~ReadBufferFromRabbitMQConsumer() override;

    void allowNext() { allowed = true; } // Allow to read next message.
    void subscribeConsumer();

private:
    using Messages = std::vector<String>;
    using Queues = std::vector<String>;

    ChannelPtr consumer_channel;
47
    RabbitMQHandler & eventHandler;
48 49 50 51 52 53 54 55 56 57 58 59 60

    const String & exchange_name;
    const String & routing_key;
    const size_t channel_id;
    const bool bind_by_id;
    const bool hash_exchange;

    Poco::Logger * log;
    char row_delimiter;
    bool stalled = false;
    bool allowed = true;
    const std::atomic<bool> & stopped;

61 62
    std::atomic<bool> exchange_declared;
    std::atomic<bool> false_param;
63 64 65 66
    const size_t num_queues;
    Queues queues;
    bool subscribed = false;
    String current_exchange_name;
K
kssenii 已提交
67 68
    size_t count_subscribed = 0;
    size_t count_bound_queues = 0;
K
Fixes  
kssenii 已提交
69
    std::atomic<bool> loop_attempt;
70 71 72 73 74

    Messages received;
    Messages messages;
    Messages::iterator current;

75 76
    std::mutex mutex;

77 78 79 80 81
    bool nextImpl() override;

    void initExchange();
    void initQueueBindings(const size_t queue_id);
    void subscribe(const String & queue_name);
82
    void startEventLoop(std::atomic<bool> & check_param);
K
kssenii 已提交
83
    void stopEventLoop();
84 85 86

};
}