4.2. 接收信息
此客户端程序使用 < connection-url > 连接到服务器,为源 <address& gt; 创建一个接收器,并接收信息直到终止或到达 < count> 信息。
示例:接收信息
#include <proton/connection.hpp>
#include <proton/container.hpp>
#include <proton/delivery.hpp>
#include <proton/message.hpp>
#include <proton/messaging_handler.hpp>
#include <proton/receiver.hpp>
#include <proton/source.hpp>
#include <iostream>
#include <string>
struct receive_handler : public proton::messaging_handler {
std::string conn_url_ {};
std::string address_ {};
int desired_ {0};
int received_ {0};
void on_container_start(proton::container& cont) override {
cont.connect(conn_url_);
// To connect with a user and password:
//
// proton::connection_options opts {};
// opts.user("<user>");
// opts.password("<password>");
//
// cont.connect(conn_url_, opts);
}
void on_connection_open(proton::connection& conn) override {
conn.open_receiver(address_);
}
void on_receiver_open(proton::receiver& rcv) override {
std::cout << "RECEIVE: Opened receiver for source address '"
<< rcv.source().address() << "'\n";
}
void on_message(proton::delivery& dlv, proton::message& msg) override {
std::cout << "RECEIVE: Received message '" << msg.body() << "'\n";
received_++;
if (received_ == desired_) {
dlv.receiver().close();
dlv.connection().close();
}
}
};
int main(int argc, char** argv) {
if (argc != 3 && argc != 4) {
std::cerr << "Usage: receive <connection-url> <address> [<message-count>]\n";
return 1;
}
receive_handler handler {};
handler.conn_url_ = argv[1];
handler.address_ = argv[2];
if (argc == 4) {
handler.desired_ = std::stoi(argv[3]);
}
proton::container cont {handler};
try {
cont.run();
} catch (const std::exception& e) {
std::cerr << e.what() << "\n";
return 1;
}
return 0;
}
#include <proton/connection.hpp>
#include <proton/container.hpp>
#include <proton/delivery.hpp>
#include <proton/message.hpp>
#include <proton/messaging_handler.hpp>
#include <proton/receiver.hpp>
#include <proton/source.hpp>
#include <iostream>
#include <string>
struct receive_handler : public proton::messaging_handler {
std::string conn_url_ {};
std::string address_ {};
int desired_ {0};
int received_ {0};
void on_container_start(proton::container& cont) override {
cont.connect(conn_url_);
// To connect with a user and password:
//
// proton::connection_options opts {};
// opts.user("<user>");
// opts.password("<password>");
//
// cont.connect(conn_url_, opts);
}
void on_connection_open(proton::connection& conn) override {
conn.open_receiver(address_);
}
void on_receiver_open(proton::receiver& rcv) override {
std::cout << "RECEIVE: Opened receiver for source address '"
<< rcv.source().address() << "'\n";
}
void on_message(proton::delivery& dlv, proton::message& msg) override {
std::cout << "RECEIVE: Received message '" << msg.body() << "'\n";
received_++;
if (received_ == desired_) {
dlv.receiver().close();
dlv.connection().close();
}
}
};
int main(int argc, char** argv) {
if (argc != 3 && argc != 4) {
std::cerr << "Usage: receive <connection-url> <address> [<message-count>]\n";
return 1;
}
receive_handler handler {};
handler.conn_url_ = argv[1];
handler.address_ = argv[2];
if (argc == 4) {
handler.desired_ = std::stoi(argv[3]);
}
proton::container cont {handler};
try {
cont.run();
} catch (const std::exception& e) {
std::cerr << e.what() << "\n";
return 1;
}
return 0;
}
运行示例
要运行示例程序,请将其复制到本地文件,对其进行编译并从命令行执行。如需更多信息,请参阅 第 3 章 开始使用。
g++ receive.cpp -o receive -std=c++11 -lstdc++ -lqpid-proton-cpp ./receive amqp://localhost queue1
$ g++ receive.cpp -o receive -std=c++11 -lstdc++ -lqpid-proton-cpp
$ ./receive amqp://localhost queue1