Skip to content

Conversation

artembilan
Copy link
Member

Fixes #3454

The message converter may return null when we try to covert from the
MQTT message.
The thrown exception may also reset the client connect.

  • Fix MqttPahoMessageDrivenChannelAdapter to catch any conversion errors
    (including null result) and try to send an ErrorMessage with that info
    into the provided errorChannel.
    Otherwise re-throw it as as

Cherry-pick to 5.4.x & 5.3.x

Fixes spring-projects#3454

The message converter may return null when we try to covert from the
MQTT message.
The thrown exception may also reset the client connect.

* Fix `MqttPahoMessageDrivenChannelAdapter` to catch any conversion errors
(including `null` result) and try to send an `ErrorMessage` with that info
into the provided `errorChannel`.
Otherwise re-throw it as as

**Cherry-pick to `5.4.x` & `5.3.x`**
@artembilan
Copy link
Member Author

I will do back-port to 5.2.x after review and merging: it does not support MessageBuilder contract yet.

Thanks

conversionException = new MessageConversionException(message, "Failed to convert from MQTT Message",
conversionError);
}
throw conversionException;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should we still throw here? Or just log as ERROR?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Well, I tried to make it consistent with what we have so far in the MessageProducerSupport.sendMessage():

try {
	this.messagingTemplate.send(getRequiredOutputChannel(), message);
}
catch (RuntimeException ex) {
	if (!sendErrorMessageIfNecessary(message, ex)) {
		throw ex;
	}
}

We probably may consider some general ErrorHandler strategy. However it looks like we have it already for this kind of channel adapters via an ErrorHandlingTaskExecutor impl. Nevertheless I don't see that we have some way to inject it into the MqttAsyncClient...

Probably the matter for a separate issue and discussion over there...

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Right, but the problem here is that throwing an exception causes the client to close. Conversion exceptions are generally fatal anyway (no point in retrying) since a redelivery will fail again.

However, it would be a behavior change so we'd probably have to add a flag.

Or, we can just leave it as you have it; up to you.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Well, we have a flag - errorChannel and no exceptions are thrown back to the client!
I would stay with this behavior for a while: probably everybody is going to be happy to swallow and process those wrong messages in the error flow instead of just silent (log) drop to satisfy the client connection stability.

@artembilan
Copy link
Member Author

@garyrussell ,

let me know if anything else you'd like to see fixed with this PR!
Thanks

@garyrussell garyrussell merged commit 90ca3d6 into spring-projects:master Jan 8, 2021
garyrussell pushed a commit that referenced this pull request Jan 8, 2021
* GH-3454: From MQTT conversion error - to error ch

Fixes #3454

The message converter may return null when we try to covert from the
MQTT message.
The thrown exception may also reset the client connect.

* Fix `MqttPahoMessageDrivenChannelAdapter` to catch any conversion errors
(including `null` result) and try to send an `ErrorMessage` with that info
into the provided `errorChannel`.
Otherwise re-throw it as as

**Cherry-pick to `5.4.x` & `5.3.x`**

* * Apply review language-specific changes
garyrussell pushed a commit that referenced this pull request Jan 8, 2021
* GH-3454: From MQTT conversion error - to error ch

Fixes #3454

The message converter may return null when we try to covert from the
MQTT message.
The thrown exception may also reset the client connect.

* Fix `MqttPahoMessageDrivenChannelAdapter` to catch any conversion errors
(including `null` result) and try to send an `ErrorMessage` with that info
into the provided `errorChannel`.
Otherwise re-throw it as as

**Cherry-pick to `5.4.x` & `5.3.x`**

* * Apply review language-specific changes
@garyrussell
Copy link
Contributor

Cherry-picked to 5.4.x and to 5.3.x after resolving conflicts.

Ready for 5.2.x backport.

artembilan added a commit that referenced this pull request Jan 8, 2021
* GH-3454: From MQTT conversion error - to error ch

Fixes #3454

The message converter may return null when we try to covert from the
MQTT message.
The thrown exception may also reset the client connect.

* Fix `MqttPahoMessageDrivenChannelAdapter` to catch any conversion errors
(including `null` result) and try to send an `ErrorMessage` with that info
into the provided `errorChannel`.
Otherwise re-throw it as as

**Cherry-pick to `5.4.x` & `5.3.x`**

* * Apply review language-specific changes

# Conflicts:
#	spring-integration-mqtt/src/main/java/org/springframework/integration/mqtt/inbound/MqttPahoMessageDrivenChannelAdapter.java
#	spring-integration-mqtt/src/test/java/org/springframework/integration/mqtt/MqttAdapterTests.java
#	src/reference/asciidoc/mqtt.adoc
@artembilan
Copy link
Member Author

Back-ported to 5.2.x as 71d4175

@artembilan artembilan deleted the GH-3454 branch January 8, 2021 18:51
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Paho loses connection if the message converter returns null
2 participants