hivemq / hivemq-mqtt-client

HiveMQ MQTT Client is an MQTT 5.0 and MQTT 3.1.1 compatible and feature-rich high-performance Java client library with different API flavours and backpressure support
https://hivemq.github.io/hivemq-mqtt-client/
Apache License 2.0
855 stars 158 forks source link

What happens if exceptions are thrown in callbacks? #575

Closed inad9300 closed 7 months ago

inad9300 commented 1 year ago

I have a Mqtt5AsyncClient on which I call subscribeWith(), .callback(), etc. My question is, what would happen if an exception is thrown by the consumer passed as callback? In my experiments, the consumer is never called again after such an event, so I would like to understand more in detail what happens internally in HiveMQ in case of exception, and what is the expectation from library users. I was quite surprised by this behavior to be honest – I was expecting HiveMQ to handle the exception in such a way that future calls to the callback kept working.

pglombardo commented 1 year ago

Hi @inad9300 - @SgtSilvio might know off hand as he's the original author. Do you have a test case? We could definitely document this better with our findings.

inad9300 commented 1 year ago

I guess it depends what you mean by test case. This is basically the code I used for my experiment:

.callback(__ -> {
  System.out.println("Handling message..."); // Printed once and never again.
  throw new RuntimeException("Error handling message.");
})
rod2j commented 1 year ago

Encountered the same problem and would very much like this to be fixed.

pglombardo commented 7 months ago

Hi @inad9300 @rod2j, the current requirement is to protect against exceptions in callbacks but this could be improved and definitely at least documented.

I filed #624 to improve this behaviour based on your suggestions. If you have any other suggestions or ideas, feel free to add them into #624.

I'll close this question issue for now. If anything else remains, let me know.