Skip to content

Commit

Permalink
java/iot3core: update Iot3CoreExample class with IoT3Core Builder exa…
Browse files Browse the repository at this point in the history
…mple and better MQTT test cases
  • Loading branch information
mathieu1fb committed Oct 22, 2024
1 parent b7e6ce6 commit 5ccf887
Showing 1 changed file with 38 additions and 31 deletions.
69 changes: 38 additions & 31 deletions java/iot3/examples/src/main/java/com/orange/Iot3CoreExample.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,28 +9,37 @@

public class Iot3CoreExample {

// MQTT parameters
private static final String EXAMPLE_MQTT_HOST = "mqtt_host";
private static final int EXAMPLE_MQTT_PORT_TCP = 1883;
private static final int EXAMPLE_MQTT_PORT_TLS = 8883;
private static final String EXAMPLE_MQTT_USERNAME = "username";
private static final String EXAMPLE_MQTT_PASSWORD = "password";
private static final String EXAMPLE_MQTT_CLIENT_ID = "client_id";
private static final String EXAMPLE_OTL_HOST = "open_telemetry_host";
private static final String EXAMPLE_MQTT_USERNAME = "mqtt_username";
private static final String EXAMPLE_MQTT_PASSWORD = "mqtt_password";
private static final String EXAMPLE_MQTT_CLIENT_ID = "mqtt_client_id";
// OpenTelemetry parameters
private static final String EXAMPLE_OTL_HOST = "telemetry_host";
private static final int EXAMPLE_OTL_PORT = 4318;
private static final String EXAMPLE_OTL_ENDPOINT = "/otl/endpoint";
private static final String EXAMPLE_OTL_ENDPOINT = "/telemetry/endpoint";
private static final String EXAMPLE_OTL_USERNAME = "telemetry_username";
private static final String EXAMPLE_OTL_PASSWORD = "telemetry_password";

private static IoT3Core ioT3Core;

public static void main(String[] args) {
// instantiate IoT3Core and its callback
ioT3Core = new IoT3Core(
EXAMPLE_MQTT_HOST,
EXAMPLE_MQTT_PORT_TCP,
EXAMPLE_MQTT_PORT_TLS,
EXAMPLE_MQTT_USERNAME,
EXAMPLE_MQTT_PASSWORD,
EXAMPLE_MQTT_CLIENT_ID,
new IoT3CoreCallback() {
ioT3Core = new IoT3Core.IoT3CoreBuilder()
.mqttParams(EXAMPLE_MQTT_HOST,
EXAMPLE_MQTT_PORT_TCP,
EXAMPLE_MQTT_PORT_TLS,
EXAMPLE_MQTT_USERNAME,
EXAMPLE_MQTT_PASSWORD,
EXAMPLE_MQTT_CLIENT_ID)
.telemetryParams(EXAMPLE_OTL_HOST,
EXAMPLE_OTL_PORT,
EXAMPLE_OTL_ENDPOINT,
EXAMPLE_OTL_USERNAME,
EXAMPLE_OTL_PASSWORD)
.callback(new IoT3CoreCallback() {
@Override
public void mqttConnectionLost(Throwable throwable) {
System.out.println("MQTT connection lost...");
Expand Down Expand Up @@ -64,32 +73,30 @@ public void mqttUnsubscriptionComplete(Throwable unsubscribeFailure) {
if(unsubscribeFailure == null) System.out.println("MQTT unsubscription successful");
else System.out.println("MQTT unsubscription failed");
}
},
EXAMPLE_OTL_HOST,
EXAMPLE_OTL_PORT,
EXAMPLE_OTL_ENDPOINT);
})
.build();
}

private static void onConnectionComplete() {
// subscribe to some topics
ioT3Core.mqttSubscribe("test/iot3");
ioT3Core.mqttSubscribe("test/iot3/core");

try (ScheduledExecutorService executorService = Executors.newSingleThreadScheduledExecutor()) {
// publish a message on a topic that we have not subscribed to
// subscribe to the root test topic and to all iot3 topics using the wildcard #
ioT3Core.mqttSubscribe("test");
ioT3Core.mqttSubscribe("test/iot3/#");
try {
ScheduledExecutorService executorService = Executors.newSingleThreadScheduledExecutor();
// publish a message on a topic that we have subscribed to
executorService.schedule(() -> publishMessage("test",
"This is a test message, it should not come back"),
"This is a test message, it should come back"),
2, TimeUnit.SECONDS);
// publish a message on a topic that we have subscribed to
executorService.schedule(() -> publishMessage("test/iot3",
"This is an iot3 message, it should come back"),
// publish a message on a topic that we have not subscribed to
executorService.schedule(() -> publishMessage("test/iot",
"This is an iot test message, it should not come back"),
4, TimeUnit.SECONDS);
// publish a message on a topic that we have subscribed to with the wildcard #
executorService.schedule(() -> publishMessage("test/iot3/#",
"This is an iot3 core message, it should also come back"),
6, TimeUnit.SECONDS);
executorService.schedule(() -> publishMessage("test/iot3/core",
"This is an iot3 core test message, it should also come back"),
8, TimeUnit.SECONDS);
// disconnect the clients of IoT3Core
executorService.schedule(ioT3Core::disconnectAll, 8, TimeUnit.SECONDS);
executorService.schedule(ioT3Core::disconnectAll, 10, TimeUnit.SECONDS);
} catch (Exception e) {
System.out.println("Error: " + e);
}
Expand Down

0 comments on commit 5ccf887

Please sign in to comment.