-
Notifications
You must be signed in to change notification settings - Fork 397
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Controller.Api.MQTT: reconnect on connection failure #2602
Merged
Merged
Conversation
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
After the initial connection attempt in the activate method, we schedule the attemptConnect method to be called periodically every 60 seconds. This ensures that if the MQTT client becomes disconnected for any reason (e.g., network issues, broker restarts), the system will automatically attempt to re-establish the connection at regular intervals.
Enhanced Lifecycle Management: Modified the component's initialization and shutdown processes. The ScheduledExecutorService, responsible for managing reconnection attempts, is now initialized in the component's constructor and configured in the @activate method. This ensures that the executor is ready for use immediately and adapts to configuration updates. Graceful Shutdown: Implemented a more graceful shutdown process using ThreadPoolUtils.shutdownAndAwaitTermination in the @deactivate method. This approach ensures that ongoing reconnection attempts are properly completed before the component is deactivated, preventing potential resource leaks. Configuration Update Handling: Introduced a @Modified method to dynamically handle configuration changes. This method allows the component to adjust its behavior, such as changing the reconnection attempt interval, without needing to be fully deactivated and reactivated.
Dynamic Reconnection Interval: Added a new configuration parameter (reconnectionAttemptInterval) to the Config interface. This parameter allows users to specify the interval between reconnection attempts to the MQTT broker, enhancing the component's adaptability to network conditions or broker availability.
Sn0w3y
changed the title
Reconnect MQTT automatically in case of failure
Update ControllerApiMqttImpl.java to reconnect on Connection Failure
Apr 1, 2024
1 similar comment
Maybe this Feature would be a "nice to have" ? See Issue #2510: |
1 similar comment
sfeilmeier
changed the title
Update ControllerApiMqttImpl.java to reconnect on Connection Failure
Controller.Api.MQTT: reconnect on connection failure
Jun 16, 2024
sfeilmeier
approved these changes
Jun 16, 2024
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Add Reconnection Logic to MQTT Controller
This PR introduces reconnection logic to the MQTT Controller, ensuring robustness in the face of connection losses with the MQTT broker. The implementation schedules reconnection attempts with an exponential backoff strategy, aiming to minimize the impact on both the broker and the controller while trying to re-establish a lost connection.
Key Changes:
Scheduled Reconnection Attempts: Utilizes a single-threaded
ScheduledExecutorService
to manage reconnection attempts, allowing for delayed execution with exponential backoff.Adaptive Delay Calculation: Implements an adaptive delay calculation using a base delay, a maximum delay cap, and a multiplier to manage the delay between reconnection attempts, preventing aggressive reconnection attempts.
Integration with Component Lifecycle: Hooks the reconnection logic into the component's activation process, ensuring attempts to connect or reconnect are made upon activation. Similarly, ensures proper shutdown of the executor service upon deactivation to clean up resources.
Integration:
This implementation ensures that the MQTT Controller attempts to reconnect to the broker following a connection loss, employing an exponential backoff strategy to balance between prompt reconnection and avoiding overwhelming the broker or the network.