【问题标题】:Qpid Proton: sending messages to two destinationsQpid Proton:向两个目的地发送消息
【发布时间】:2017-12-10 08:31:32
【问题描述】:

目前,我可以将消息发送到单个队列“devQueue”。当消息到达“devQueue”时,它也需要被发送到“localQueue”。我发现这种实施具有挑战性。我试图从“class1”类中调用另一个类“local_send”,这样我就可以连接到另一个目的地,即“localQueue”(如下面的代码所示),但没有成功。是否有任何有用的质子函数,或者我可以在 v_message() 函数内的“class1”类中使用来自 on_connection_open() 的参考变量?非常感谢这方面的任何帮助或想法。

目前代码没有进入“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);
    }
}
};

【问题讨论】:

    标签: browser message-queue connect messagebroker qpid


    【解决方案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);
    }
    

    这个 sn-p 不使用 on_sendable。发送缓冲在库中。如果您愿意,您可以像在代码中那样使用消息向量,并在有信用时使用 on_sendable(在同一处理程序中)发送。

    【讨论】:

    • 谢谢贾斯汀!这非常有帮助,而且奏效了。在重构时,我需要对消息配置有所了解。不知道如何通过这篇文章扩展它,所以发布了另一个。
    猜你喜欢
    • 2015-11-18
    • 2014-03-15
    • 2018-07-12
    • 2016-08-06
    • 2015-11-18
    • 2015-12-26
    • 2017-07-01
    • 2018-01-12
    • 2021-05-02
    相关资源
    最近更新 更多