Basic Event SampleΒΆ

This sample demonstrates how to register a callback to receive Event messages from the DXL fabric. Once the callback is registered, the sample sends a set number of Event messages to the fabric and waits for them all to be received by the callback.

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.2.0>sample\runsample sample.basic.EventSample

The output should appear similar to the following:

Received event: 0
Received event: 1
Received event: 2
Received event: 3
Received event: 4
Received event: 5


Received event: 994
Received event: 995
Received event: 996
Received event: 997
Received event: 998
Received event: 999
Elapsed time (ms): 441.999912262

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

The first section is responsible for registering an EventCallback for a specific topic. The DxlClient.addEventCallback() method registers the callback and subscribes to the topic.

// Register callback and subscribe

// Create and add event listener
final EventCallback myEventCallback =
    event -> {
        try {
            System.out.println("Received event: "
                + new String(event.getPayload(), Message.CHARSET_UTF8));
        } catch (UnsupportedEncodingException ex) {

        try {

        } finally {
client.addEventCallback(EVENT_TOPIC, myEventCallback);

The second section sends a set amount of Event messages via the DxlClient.sendEvent method of the DxlClient.

It then waits for all of the events to be received by the EventCallback that was previously registered.

// Send events

// Record the start time
final long startTime = System.currentTimeMillis();

// Loop and send the events
for (int count = 0; count < TOTAL_EVENTS; count++) {
    final Event event = new Event(EVENT_TOPIC);

// Wait until all events have been received
System.out.println("Waiting for events to be received...");
try {
    while (eventCount < TOTAL_EVENTS) {
} finally {

// Print the elapsed time
System.out.println("Elapsed time (ms): " + (System.currentTimeMillis() - startTime));