Examples on how to use the request-reply API .
Examples on how to use the request-reply API .
Request-Reply code examples.
Request-Reply Examples
Requesters and Repliers provide a way to use the Request-Reply communication pattern on top of the DDS entities. An application uses a Requester to send requests to a Replier; another application using a Replier receives a request and can send one or more replies for that request. The Requester that sent the request (and only that one) will receive the reply (or replies).
DDS Types
RTI Connext uses DDS data types for sending and receiving requests and replies. Valid types are those generated by the rtiddsgen code generator, the DDS built-in types, and DynamicData
. Refer to the Core Libraries User's Manual and the following links for more information:
Set up
Requester: sending requests and receiving replies
Replier: receiving requests and sending replies
Error handling
- Note
- To use Request-Reply you need to include the additional
rticonnextmsg.jar
to build your Java application.
Creating a Requester
- Setting up a participant
- Creating a Requester
DomainParticipant participant =
DomainParticipantFactory.get_instance().create_participant(
domainId, DomainParticipantFactory.PARTICIPANT_QOS_DEFAULT,
null, StatusKind.STATUS_MASK_NONE);
Requester<Foo, Bar> requester =
new Requester<Foo, Bar>(
participant, "TestService",
FooTypeSupport.get_instance(),
BarTypeSupport.get_instance());
- Finalizing a Requester
- See also
- Creating a Requester with optional parameters
-
Configuring Request-Reply QoS profiles
Creating a Requester with optional parameters
- Setting up a participant
- Creating a Requester with optional parameters
DomainParticipant participant =
DomainParticipantFactory.get_instance().create_participant(
0, DomainParticipantFactory.PARTICIPANT_QOS_DEFAULT,
null, StatusKind.STATUS_MASK_NONE);
Requester<Foo, Bar> requester =
new Requester<Foo, Bar>(
new RequesterParams(
participant,
FooTypeSupport.get_instance(),
BarTypeSupport.get_instance())
.setServiceName("TestService")
.setQosProfile("RequestReplyExampleProfiles",
"RequesterExampleProfile"));
- See also
- Creating a Requester
Basic Requester example
Taking loaned samples
- See also
- Basic Requester example
-
Basic Replier example
-
Taking samples by copy
Taking samples by copy
- Get copies of the replies:
ArrayList<Sample<Bar>> replies = new ArrayList<Sample<Bar>>();
requester.takeReplies(replies, 10);
for (Sample<Bar> reply : replies) {
System.out.println("Received: " + reply.getData().message);
}
List<Sample<Bar>> newList = requester.takeReplies(null, 10);
for (Sample<Bar> reply : newList) {
System.out.println("Received: " + reply.getData().message);
}
- See also
- Basic Replier example
-
Taking loaned samples
Correlating requests and replies
- Creating a Requester
- Example 1) Waiting for a reply for a specific request
WriteSample<Foo> request1 = requester.createRequestSample();
request1.getData().message = "Request 1";
WriteSample<Foo> request2 = requester.createRequestSample();
request2.getData().message = "Request 2";
requester.sendRequest(request1);
requester.sendRequest(request2);
Sample<Bar> reply = requester.createReplySample();
requester.waitForReplies(1, MAX_WAIT, request2.getIdentity());
requester.takeReply(reply, request2.getIdentity());
assert reply.getRelatedIdentity().equals(request2.getIdentity());
System.out.println("Received reply: " + reply.getData().message);
requester.waitForReplies(1, MAX_WAIT, request1.getIdentity());
requester.takeReply(reply, request1.getIdentity());
assert reply.getRelatedIdentity().equals(request1.getIdentity());
System.out.println("Received reply: " + reply.getData().message);
- Example 2) Correlate reply after receiving it
WriteSample<Foo> request1 = requester.createRequestSample();
request1.getData().message = "Request 1";
WriteSample<Foo> request2 = requester.createRequestSample();
request2.getData().message = "Request 2";
requester.sendRequest(request1);
requester.sendRequest(request2);
boolean received = requester.waitForReplies(2, MAX_WAIT);
if (!received) {
System.out.println("Replies not received");
return;
}
Sample.Iterator<Bar> replies = requester.takeReplies();
try {
while(replies.hasNext()) {
Sample<Bar> reply = replies.next();
if (reply.getRelatedIdentity().equals(
request1.getIdentity())) {
System.out.println(
"Received reply for request 1: " + reply.getData().message);
} else if (reply.getRelatedIdentity().equals(
request2.getIdentity())) {
System.out.println(
"Received reply for request 2: " + reply.getData().message);
} else {
System.out.println("Received unexpected reply");
}
}
} finally {
replies.close();
}
- See also
- Basic Requester example
-
Basic Replier example
Creating a Replier
- Setting up a participant
- Creating a Replier
DomainParticipant participant =
DomainParticipantFactory.get_instance().create_participant(
domain_id, DomainParticipantFactory.PARTICIPANT_QOS_DEFAULT,
null, StatusKind.STATUS_MASK_NONE);
Replier<Foo, Bar> replier =
new Replier<Foo, Bar>(
participant, "TestService",
FooTypeSupport.get_instance(),
BarTypeSupport.get_instance());
- Finalizing a Replier
Basic Replier example
- Creating a Replier
- Basic Replier example
Sample<Foo> request = replier.createRequestSample();
boolean received = replier.receiveRequest(request, MAX_WAIT);
if (!received) {
System.out.println("Request not received");
return false;
}
if (request.getInfo().valid_data) {
Bar reply = new Bar();
reply.message = "Reply for " + request.getData().message;
replier.sendReply(reply, request.getIdentity());
}
- See also
- Basic Requester example
SimpleReplier example
- Implement a listener
class MySimpleReplierListener implements SimpleReplierListener<Foo, Bar> {
private Bar reply = new Bar();
public Bar onRequestAvailable(Sample<Foo> request) {
if (request.getInfo().valid_data) {
reply.message = "Simple reply for " + request.getData().message;
return reply;
} else {
return null;
}
}
public void returnLoan(Bar reply) {
}
}
- And create a SimpleReplier with the listener:
SimpleReplier<Foo, Bar> replier =
new SimpleReplier<Foo, Bar>(
participant, "TestService",
new MySimpleReplierListener(),
FooTypeSupport.get_instance(),
BarTypeSupport.get_instance());
- See also
- Basic Requester example
Error handling example
- Catching an exception from the request-reply API
Logger.get_instance().set_verbosity(
LogVerbosity.NDDS_CONFIG_LOG_VERBOSITY_WARNING);
WriteSample<Bar> reply = replier.createReplySample();
SampleIdentity_t invalidRequestId = new SampleIdentity_t();
try {
replier.sendReply(reply, invalidRequestId);
} catch (RETCODE_BAD_PARAMETER ex) {
System.out.println("Exception while sending reply: " + ex);
}
Configuring Request-Reply QoS profiles
If you do not specify your own quality of service parameters (in com.rti.connext.requestreply.RequesterParams and com.rti.connext.requestreply.ReplierParams<TReq,TRep>), a com.rti.connext.requestreply.Requester<TReq,TRep> and com.rti.connext.requestreply.Replier<TReq,TRep> are created using a default configuration. That configuration is equivalent to the one in the following QoS profile called "default":
<?xml version="1.0" encoding="UTF-8"?>
<dds xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="file:////rti/jenkins/workspace/connextdds_htmldocs_support_connextdds_7.3.0/build/nddsgen.2.0/resource/schema/rti_dds_qos_profiles.xsd">
<qos_library name="HelloWorld_Library">
<qos_profile name="HelloWorld_Profile" base_name="BuiltinQosLib::Generic.StrictReliable" is_default_qos="true">
<datawriter_qos>
<publication_name>
<name>HelloWorldDataWriter</name>
</publication_name>
</datawriter_qos>
<datareader_qos>
<subscription_name>
<name>HelloWorldDataReader</name>
</subscription_name>
</datareader_qos>
<domain_participant_qos>
<participant_name>
<name>HelloWorldParticipant</name>
<role_name>HelloWorldParticipantRole</role_name>
</participant_name>
</domain_participant_qos>
</qos_profile>
</qos_library>
</dds>
You can use the profile called "RequesterExampleProfile", which modifies some parameters from the default. The example Creating a Requester with optional parameters shows how to create a com.rti.connext.requestreply.Requester<TReq,TRep> using this profile.
- See also
- Creating a Requester with optional parameters
-
Configuring QoS Profiles with XML