Qpid Proton:向两个目的地发送消息

时间:2017-12-09 18:52:24

标签: browser message-queue connect messagebroker qpid

目前,我可以将消息发送到单个队列" devQueue"。当消息到达" devQueue"时,需要将消息发送到" localQueue"同样。我发现这个实施具有挑战性。我试着打电话给另一个班级" local_send"来自" class1"所以我可以连接到另一个目的地,即#34; localQueue" (如下面的代码所示)但没有运气。是否存在任何有用的质子函数,或者我可以在" class1"中使用on_connection_open()中的引用变量。 class里面的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 个答案:

答案 0 :(得分:2)

您需要调用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(在同一个处理程序中)在有信用时发送。

相关问题