AMQP-CPP初学者问题



我是rabbitmq的新手,尝试让Listener从消息队列中读取。服务器应该很好(不是我实现的,我想是用Java实现的(。我使用C++作为使用者,并使用amqpcpp库。

这就是我迄今为止所尝试的:

int main(int argc, char* const argv[])
{
// access to the boost asio handler
// note: we suggest use of 2 threads - normally one is fin (we are simply demonstrating thread safety).
boost::asio::io_service service(2);
// handler for libev
AMQP::LibBoostAsioHandler handler(service);

// make a connection
AMQP::Address address("amqp://10.40.216.87");
AMQP::TcpConnection connection(&handler, address);
// we need a channel too
AMQP::TcpChannel channel(&connection);

//channel.onError(errorTCP);
channel.declareQueue("logmessages");
// Define callbacks and start
auto messageCb = [&channel](
const AMQP::Message &message, uint64_t deliveryTag, 
bool redelivered)
{
std::cout << "message received" << std::endl;
// acknowledge the message
// channel.ack(deliveryTag);
//processMessage(message.routingKey(), message.body());
};
// callback function that is called when the consume operation starts
auto startCb = [](const std::string &consumertag) {
std::cout << "consume operation started: " << consumertag << std::endl;
};
// callback function that is called when the consume operation failed
auto errorCb = [](const char* message) {
std::cout << "consume operation failed:" << *message << std::endl;
};
channel.consume("logmessages")
.onReceived(messageCb)
.onSuccess(startCb)
.onError(errorCb);

// create a temporary queue
/*channel.declareQueue(AMQP::exclusive).onSuccess([&connection](const std::string &name, uint32_t messagecount, uint32_t consumercount) {

// report the name of the temporary queue
std::cout << "declared queue " << name << std::endl;

// now we can close the connection
connection.close();
});*/
channel.consume("devices.state.*");

// run the handler
// a t the moment, one will need SIGINT to stop.  In time, should add signal handling through boost API.
return service.run();
}

我总是得到一个";消耗操作失败";。现在的问题是为什么。。。有什么方法可以获得更多的错误消息吗?(例如,无法连接到TCP套接字,找不到具有正确名称的队列等(。

谢谢你的建议!

在您的代码中,您试图定制*消息指针,而不是消息本身。

而不是

auto errorCb = [](const char* message) {
std::cout << "consume operation failed:" << *message << std::endl;
};

应该是

auto errorCb = [](const char* message) {
std::cout << "consume operation failed:" << message << std::endl;
};

这应该会给您正确的错误信息。请查看官方回购中的文档/示例。