Repository navigation
Allow MQTTHandlers to replace other MQTTHandlers - #3656
richturner wants to merge 7 commits into
Conversation
Adds `MQTTHandler.getReplacedHandlers()` so a custom project can take over the topic of a built-in handler (e.g. `UserAssetProvisioningMQTTHandler`). A handler that extends another loaded handler replaces it implicitly. Replaced handlers are discarded on load, so they are never initialized or started and never consume messages; ordering by priority alone was not enough because every handler receives published messages on its topics.
stop() reloaded the handlers via ServiceLoader, so it stopped new instances that were never initialized instead of the running ones, and ignored replaced handlers. It now stops the loaded handlers in reverse priority order.
|
Validated this in a custom project and it works |
ebariaux
left a comment
There was a problem hiding this comment.
Other codex findings
- [P2] Test which handler instances receive lifecycle callbacks — manager/src/main/java/org/openremote/manager/mqtt/MQTTBrokerService.java:462-463
X509ProvisioningProofTest constructs its handler directly, so it would still pass if the broker initialized a replaced provider or reverted to stopping freshly loaded instances. Add a Spock unit spec in manager/src/test/groovy with a replacement and an unrelated peer; assert that only the selected instances initialize and start, and that those same instances stop once in reverse order.
And you did not revert the change on the challenge, so this still needs to be indicated as a breaking change for this release.
|
Changes made based on the review; prefix revert means the PR no longer needs the Breaking Change label. |
ebariaux
left a comment
There was a problem hiding this comment.
One small finding remaining about regression test, nothing major.
Agent also found related issue that was present before this PR, to be addressed separately: #3663
The PR fixes normal shutdown. There's a remaining finding about whether the test would catch that bug coming back.
An MQTT handler stores state on the instance that started. For example, start() stores a client session in a field, and stop() reads that field to close the session. The old broker ran ServiceLoader again during shutdown. That created a fresh object of the same handler class, with an empty session field. The handler that had actually started never received its cleanup callback. The PR fixes this by keeping the selected instances and stopping those same objects. MQTTHandler.java
The test gap is one method call. In production, the container calls broker.stop(container), which now calls stopHandlers(). The test calls stopHandlers() directly, skipping the method where the original bug lived. I restored the old broker.stop(container) in an isolated copy and all five tests still passed. Calling broker.stop(container) in the test would let its existing assertions catch the regression. MQTTHandlerReplacementTest.groovy
The separate, pre-existing issue happens during cleanup after a failed startup. The sequence I reproduced was:
- One handler finishes init(). The next handler throws during init().
- The container begins shutdown. Artemis would have been created in the later start() phase, so the broker’s server field is still null.
- broker.stop(container) calls server.stop() first. That throws before execution reaches stopHandlers().
The first handler therefore received init() but never received stop(). Any resource that depends on its cleanup callback can stay open.
Calling stopHandlers() directly skipped the method where the original bug lived, so the spec still passed with the old ServiceLoader-based stop(). Stub the broker's server and debouncer and call stop(container) as the container does.
|



This PR introduces
MQTTHandler.getReplacedHandlers()to allow one MQTTHandler to replace one or more other MQTTHandler(s) by class name.This is an alternative approach to #3590 which this PR also reverts.