Basic Service SampleΒΆ

This sample demonstrates how to register a DXL service to receive Request messages and send Response messages back to an invoking DxlClient.

Prior to running this sample make sure you have completed the samples configuration step (Samples Configuration).

To run this sample execute the sample\runsample script as follows:

c:\dxlclient-java-sdk-0.1.3>sample\runsample sample.basic.ServiceSample

The output should appear similar to the following:

Service received request payload: ping
Client received response payload: pong

The code for the sample is broken into two main sections.

The first section is responsible for creating a RequestCallback that will be invoked for a specific topic associated with the service. The callback will send back a messages and send Response message with a payload of pong for any Request messages that are received.

It then creates a ServiceRegistrationInfo instance and registers the request callback with it via the ServiceRegistrationInfo.addTopic() method.

Finally it registers the service with the fabric via the DxlClient.registerServiceSync() method of the DxlClient.

//
// Register the service
//

// Create incoming request callback
final RequestCallback myRequestCallback =
    request -> {
        try {
            System.out.println("Service received request payload: "
                + new String(request.getPayload(), Message.CHARSET_UTF8));

            final Response res = new Response(request);
            res.setPayload("pong".getBytes(Message.CHARSET_UTF8));

            client.sendResponse(res);
        } catch (Exception ex) {
            ex.printStackTrace();
        }
    };

// Create service registration object
final ServiceRegistrationInfo info = new ServiceRegistrationInfo(client, "myService");

// Add a topic for the service to respond to
info.addTopic(SERVICE_TOPIC, myRequestCallback);

// Register the service with the fabric (wait up to 10 seconds for registration to complete)
client.registerServiceSync(info, TIMEOUT);

The second section sends a Request message to the service that contains a payload of ping via the DxlClient.syncRequest() method of the DxlClient.

The payloads of the Request and Response messages are printed.

//
// Invoke the service (send a request)
//

// Create the request message
final Request req = new Request(SERVICE_TOPIC);

// Populate the request payload
req.setPayload("ping".getBytes(Message.CHARSET_UTF8));

// Send the request and wait for a response (synchronous)
final Response res = client.syncRequest(req);

// Extract information from the response (Check for errors)
if (res.getMessageType() != Message.MESSAGE_TYPE_ERROR) {
    System.out.println("Client received response payload: "
        + new String(res.getPayload(), Message.CHARSET_UTF8));
} else {
    System.out.println("Error: " + ((ErrorResponse) res).getErrorMessage());
}