The second program in the request-response example, server.cpp, uses the reply-to property as the routing key for responses.
The main body of server.cpp creates an exclusive queue for requests, then waits for messages to arrive.
string request_queue = "request"; session.queueDeclare(arg::queue=request_queue); session.exchangeBind(arg::exchange="amq.direct", arg::queue=request_queue, arg::bindingKey=request_queue); SubscriptionManager subscriptions(session); Listener listener(subscriptions, session); subscriptions.subscribe(listener, request_queue); subscriptions.run();
The listener's received() method converts the request's content to upper case, then sends a response to the broker, using the request's reply-to property as the routing key for the response.
void Listener::received(Message& request) {
Message response;
string routingKey;
if (request.getMessageProperties().hasReplyTo()) {
routingKey = request.getMessageProperties().getReplyTo().getRoutingKey();
} else {
std::cout << "Error: " << "No routing key for request (" << request.getData() << ")" << std::endl;
return;
}
std::cout << "Request:: " << request.getData() << " (" << routingKey << ")" << std::endl;
// Transform message content to upper case
std::string s = request.getData();
std::transform (s.begin(), s.end(), s.begin(), toupper);
response.setData(s);
// Send it back to the user
response.getDeliveryProperties().setRoutingKey(routingKey);
session.messageTransfer(arg::content=response, arg::destination="amq.direct");
if ( request.getData() == "That's all, folks!" )
dispatcher.stop();
}