Skip to content

java: add MQTT auto reconnect to IoT3 Core#325

Merged
mathieu1fb merged 5 commits intoOrange-OpenSource:masterfrom
mathieu1fb:feature/mqtt-auto-reconnect
Mar 18, 2025
Merged

java: add MQTT auto reconnect to IoT3 Core#325
mathieu1fb merged 5 commits intoOrange-OpenSource:masterfrom
mathieu1fb:feature/mqtt-auto-reconnect

Conversation

@mathieu1fb
Copy link
Copy Markdown
Collaborator

Feature

The IoT3 Core MQTT client now has an automatic reconnection policy: reconnection will automatically be tried 500 ms after a connection loss the retry delay will gradually increase up to every 5 seconds.

The IoT3 Core and Mobility also expose respectively an isMqttConnected() and isConnected() method.

Closes #323


How to test

  1. Clone the its-client project on your IDE.
  2. Ensure that you have Gradle to be able to build the java/iot3 modules (core, mobility and examples).
  3. Find the Iot3MobilityBootstrapExample class in the examples module, and set appropriate values for the following fields:
private static final String EXAMPLE_UUID = "uuid";
private static final String EXAMPLE_CONTEXT = "context";
// Bootstrap parameters
private static final String BOOTSTRAP_ID = "bootstrap_id";
private static final String BOOTSTRAP_LOGIN = "boostrap_login";
private static final String BOOTSTRAP_PASSWORD = "bootstrap_password";
private static final BootstrapHelper.Role BOOTSTRAP_ROLE = BootstrapHelper.Role.EXTERNAL_APP;
private static final String BOOTSTRAP_URI = "bootstrap.uri.com";
private static final boolean ENABLE_TELEMETRY = false;
  1. Run Iot3MobilityBootstrapExample.
  2. After a few seconds, disconnect your computer from the Internet (turn WiFi off or disconnect your Ethernet cable).
  3. After another few seconds, reconnect your computer to the Internet.

Expected results:

  1. On the console, you should see the following:
Bootstrap success
IoT3 ID: <id>
LOGIN: <login>
PASSWORD: <password>
MQTT URI: <mqtt_uri>
TELEMETRY URI: <open_telemetry_uri>
  1. On the console, you should then see that the MQTT connection has been established successfully and that mobility messages are being published and received periodically (CAM, CPM and DENM).
  2. After disconnection, you should not receive the messages anymore.
  3. After reconnection, you should start receiving messages again.

The IoT3Core MQTT client will try to reconnect automatically upon connection loss, avoiding the need to handle reconnection at a higher level.

Signed-off-by: Mathieu LEFEBVRE <mathieu1.lefebvre@orange.com>
…MQTT client

The IoT3Core MQTT client will try to reconnect with an initial delay of 500 ms, up to 5 s maximum. The default values were respectively 1 s and up to 2 minutes, which is not aggressive enough for our needs.

Signed-off-by: Mathieu LEFEBVRE <mathieu1.lefebvre@orange.com>
Signed-off-by: Mathieu LEFEBVRE <mathieu1.lefebvre@orange.com>
…IoT3Core isMqttConnected() result

Signed-off-by: Mathieu LEFEBVRE <mathieu1.lefebvre@orange.com>
…o connection status

CAM and CPM: only publish if the client is connected
DENM: publish even when the client is disconnected, so the messages are queued and sent upon reconnection

Signed-off-by: Mathieu LEFEBVRE <mathieu1.lefebvre@orange.com>
@mathieu1fb mathieu1fb added this to the Sprint 8 milestone Mar 14, 2025
@mathieu1fb mathieu1fb requested a review from fredOG-2A1 March 14, 2025 14:57
@mathieu1fb mathieu1fb self-assigned this Mar 14, 2025
@mathieu1fb mathieu1fb marked this pull request as ready for review March 14, 2025 14:59
@fredOG-2A1
Copy link
Copy Markdown
Member

I confirmed that reconnection ik OK:
Bootstrap success
IoT3 ID: o...t
LOGIN: e...p
PASSWORD: ...
MQTT URI: mqtts://d...5
TELEMETRY URI: https://m...t/
...

`Bootstrap success
IoT3 ID: o...t
LOGIN: e...p
PASSWORD: ...
MQTT URI: mqtts://d...5
TELEMETRY URI: https://m...t/
...

mars 17, 2025 9:26:59 AM com.orange.iot3core.clients.MqttClient publishMessage
INFOS: Sending message: default/inQueue/v2x/cam/...,"timestamp":1742200016921}
CAM received: {"origin":"self","source_uuid":"ora_iot3mobility_test","type":"cam"...,"timestamp":1742200016921}

//First message with no corresponding reception.
mars 17, 2025 9:27:10 AM com.orange.iot3core.clients.MqttClient publishMessage
INFOS: Sending message: default/inQueue/v2x/cam/...,"timestamp":1742200028015}
mars 17, 2025 9:27:10 AM com.orange.iot3core.clients.MqttClient onPublishComplete

//Mqtt related infos.
mars 17, 2025 9:27:29 AM com.orange.iot3core.clients.MqttClient lambda$new$0
MQTT Connection lost...
INFOS: Disconnected from MQTT broker default.mobility-integration.ocb.itsplatform.eu

//Mqtt reconnection
mars 17, 2025 9:27:30 AM com.orange.iot3core.clients.MqttClient lambda$new$1
INFOS: Connected to MQTT broker default.mobility-integration.ocb.itsplatform.eu
MQTT connection complete: default.mobility-integration.ocb.itsplatform.eu

//Messages are received.
mars 17, 2025 9:27:31 AM com.orange.iot3core.clients.MqttClient publishMessage
INFOS: Sending message: default/inQueue/v2x/cam/...,"timestamp":1742200048153}
CAM received: {"origin":"self","source_uuid":"ora_iot3mobility_test","type":"cam",...,"timestamp":1742200048153}
`

@mathieu1fb mathieu1fb merged commit f7db0d4 into Orange-OpenSource:master Mar 18, 2025
101 checks passed
@mathieu1fb mathieu1fb deleted the feature/mqtt-auto-reconnect branch March 18, 2025 08:26
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Add auto-reconnect to MQTT client

2 participants