Qpid Proton: отправка сообщений по двум направлениям

В настоящее время я могу отправлять сообщения в одну очередь "devQueue". Когда сообщение поступает в "devQueue", его также необходимо отправить в "localQueue". Я считаю эту реализацию сложной. Я попытался вызвать другой класс "local_send" из класса "class1", чтобы я мог подключиться к другому месту назначения, которое является "localQueue" (как показано в коде ниже), но безуспешно. Есть ли какая-нибудь протонная функция, которая была бы полезной, или я могу использовать ссылочную переменную из on_connection_open() внутри класса "class1" внутри функции v_message()? Любая помощь или идея в этом направлении будет принята с благодарностью.

В настоящее время код не попадает в класс "local_send", и, следовательно, сообщения не отправляются в "localQueue".

class class1 : public proton::messaging_handler {
std::string url;
std::string devQueue;
std::string localQueue;
std::vector<proton::message> msgVector;
local_send snd;
std::vector<proton::message> msgV;

public:


class1(const std::string& u, const std::string& devQueue, const std::string& localQueue) :
        url(u), devQueue(devQueue), localQueue(localQueue), snd(msgVector, localQueue) {}

void on_container_start(proton::container& c) override {
    c.connect(url);
}

void on_connection_open(proton::connection& c) override {
    c.open_receiver(devQueue);
}

void on_message(proton::delivery &d, proton::message &msg) override {
    msgV.push_back(msg);
// Here, the messages are checked if they have a valid format or not and v_message() is called
}

void v_message(const pack& msg){
    this->msgVector.push_back(msg);

//I need to send the message to localQueue and hence we are running another class from here (but that is not working :( )
    local_send snd(msgVector, localQueue);
    proton::container(snd).run();
}

void on_sendable(proton::sender &s) override {
    for(auto msg: msgVector){
        s.send(msg);
    }
}
};


// Do I even need this class to send message to another destination?

class local_send : public proton::messaging_handler {
    std::string localQueue;
    std::vector<proton::message> msgVector;

public:
    local_send(std::vector<proton::message> msgVector, const std::string& localQueue) :
        msgVector(msgVector), localQueue(localQueue) {}

void on_connection_open(proton::connection& c) override {
    c.open_receiver(localQueue);
}

void on_sendable(proton::sender &s) override {
    for(auto msg: msgVector){
        s.send(msg);
    }
}
};

1 ответ

Решение

Вам нужно вызвать open_sender, чтобы создать канал для отправки сообщений. Вы можете сделать все это в одном обработчике. Псевдокод:

proton::sender snd;

on_connection_open(conn) {
    conn.open_receiver("queue1");
    snd = conn.open_sender("queue2");
}

on_message(dlv, msg) {
    // Relay message from queue1 to queue2
    snd.send(msg);
}

Этот фрагмент не использует on_sendable. Посылка буферизируется в библиотеке. Если вы предпочитаете, вы можете использовать вектор сообщений, как вы это сделали в своем коде, и использовать on_sendable (в том же обработчике) для отправки при наличии кредита.

Другие вопросы по тегам