此内容没有您所选择的语言版本。
14.2. Request/Response C++ Example
This example is a client and server that use the request/response pattern. The server creates a service queue and waits for a message to arrive. If it receives a message, it sends a message back to the sender.
Receiver receiver = session.createReceiver("service_queue; {create: always}");
Message request = receiver.fetch();
const Address& address = request.getReplyTo(); // Get "reply-to" from request ...
if (address) {
Sender sender = session.createSender(address); // ... send response to "reply-to"
Message response("pong!");
sender.send(response);
session.acknowledge();
}
The client creates a sender for the service queue, and also creates a response queue that is deleted when the client closes the receiver for the response queue. In the C++ client, if the address starts with the character
#, it is given a unique name.
Sender sender = session.createSender("service_queue");
Receiver receiver = session.createReceiver("#response-queue; {create:always}");
Address responseQueue = receiver.getAddress();
Message request;
request.setReplyTo(responseQueue);
request.setContent("ping");
sender.send(request);
Message response = receiver.fetch();
std::cout << request.getContent() << " -> " << response.getContent() << std::endl;
The client sends the string ping to the server. The server sends the response pong back to the same client, using the replyTo property.