Examples on how to use the request-reply API.
More...
Examples on how to use the request-reply API.
Request-Reply code examples.
Request-Reply Examples
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 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
Creating a Requester
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
- Creating a Requester
- Creating a Requester with optional parameters
- Basic Requester example
Foo request = new Foo();
request.message = "A request";
requester.sendRequest(request);
Sample<Bar> reply = requester.createReplySample();
boolean received = requester.receiveReply(reply , MAX_WAIT);
if (received) {
if (reply.getInfo().valid_data) {
System.out.println("Received reply: " + reply.getData().message);
} else {
System.out.println("Received invalid reply");
}
} else {
System.out.println("Reply not received");
}
- Basic Requester example using the String built-in type
Requester<String, String> requester =
new Requester<String, String>(
new RequesterParams(
participant,
StringTypeSupport.get_instance(),
StringTypeSupport.get_instance())
.setServiceName("TestServiceString")
.setQosProfile("MyLibrary", "RequesterProfile"));
requester.sendRequest("A request");
Sample<String> reply = requester.createReplySample();
boolean received = requester.receiveReply(reply , MAX_WAIT);
if (received && reply.getInfo().valid_data) {
System.out.println("Received reply: " + reply.getData());
}
- Basic Requester example using DynamicData.
DynamicDataTypeSupport fooDynamicTypeSupport = new DynamicDataTypeSupport(
FooTypeCode.VALUE, new DynamicDataTypeProperty_t());
DynamicDataTypeSupport barDynamicTypeSupport = new DynamicDataTypeSupport(
BarTypeCode.VALUE, new DynamicDataTypeProperty_t());
Requester<DynamicData, DynamicData> requester =
new Requester<DynamicData, DynamicData>(
participant, "TestService",
fooDynamicTypeSupport,
barDynamicTypeSupport);
DynamicData foo = (DynamicData) fooDynamicTypeSupport.create_data();
foo.set_string("message", DynamicData.MEMBER_ID_UNSPECIFIED,
"A dynamic request");
requester.sendRequest(foo);
Sample<DynamicData> reply = requester.createReplySample();
boolean received = requester.receiveReply(reply , MAX_WAIT);
if (received && reply.getInfo().valid_data) {
System.out.println("Received reply: " +
reply.getData().get_string("message", DynamicData.MEMBER_ID_UNSPECIFIED));
}
- See also
- Basic Replier example
Taking loaned samples
- Get iterator to loaned replies (no copies)
Sample.Iterator<Bar> replies = requester.takeReplies();
try {
while (replies.hasNext()) {
Sample<Bar> reply = replies.next();
System.out.println("Received: " + reply.getData().message);
}
} finally {
replies.close();
}
A more compact version using a try-with-resources
block (since Java 7):
try (Sample.Iterator<String> replies = requester.getReplies()) {
while (replies.hasNext()) {
Sample<String> reply = replies.next();
System.out.println("Received: " + reply.getData());
}
}
- 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"?>
<dds xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="file:////rti/jenkins/workspace/connextdds_htmldocs_release_connextdds_6.1.1/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