5.5.2. The Client Application

5.5.2. The Client Application

The first program in the request-response example, client.cpp, sets up a private response queue to receive responses from the server, then sends messages the server, listening to the response queue for the server's responses.

stringstream response_queue;
response_queue << "client" << session.getId().getName();

// Use the name of the response queue as the routing key

session.queueDeclare(arg::queue=response_queue.str());
session.exchangeBind(arg::exchange="amq.direct", arg::queue=response_queue.str(), arg::bindingKey=response_queue.str());

// Create a listener for the response queue and listen for response messages.
std::cout << "Activating response queue listener for: " << response_queue.str() << std::endl;
SubscriptionManager subscriptions(session);
Listener listener(subscriptions);
subscriptions.subscribe(listener, response_queue.str());

Set some properties that will be used for all requests. The routing key for a request is request. The reply-to property is set to the routing key for the client's private queue.

request.getDeliveryProperties().setRoutingKey("request");
request.getMessageProperties().setReplyTo(ReplyTo("amq.direct", response_queue.str()));

Now send some requests...

string s[] = {
    "Twas brillig, and the slithy toves",
    "Did gire and gymble in the wabe.",
    "All mimsy were the borogroves,",
    "And the mome raths outgrabe."
};

for (int i=0; i<4; i++) {
    request.setData(s[i]);
    session.messageTransfer(arg::content=request, arg::destination="amq.direct");
    std::cout << "Request: " << s[i] << std::endl;
}

And wait for responses to arrive:

std::cout << "Waiting for all responses to arrive ..." << std::endl;
subscriptions.run();