I have this code that receives MQTT messages under Spring Integration 5.0.x / Boot 2.0. It works fine for text messages, but when I try to process binary messages it fails because a conversion to String
happens and this corrupts the content (in this case: a png image file).
How can I receive the message untampered ?
I tried to setBytesMessageMapper
on DefaultPahoMessageConverter
, but this didn't change a thing.
When I download the messages content using mqtt.fx I can proof that the binary content was set correctly, so I am sure this is a problem on the receiving end.
@Bean
public MessageProducer inbound() {
MqttPahoMessageDrivenChannelAdapter adapter = new MqttPahoMessageDrivenChannelAdapter("tcp://iot.eclipse.org:1883",
"foo", "bar");
adapter.setCompletionTimeout(5000);
DefaultPahoMessageConverter converter = new DefaultPahoMessageConverter();
adapter.setConverter(converter);
adapter.setQos(1);
adapter.setOutputChannel(mqttInputChannel());
return adapter;
}
...
@Bean
@ServiceActivator(inputChannel = "mqttInputChannel")
public MessageHandler handler() {
return new MessageHandler() {
@Override
public void handleMessage(Message<?> message) throws MessagingException {
System.out.println("message received on " + new Date());
Object payload = message.getPayload();
...
};
}
Set the payloadAsBytes
property on the converter to true
...
/**
* True if the converter should not convert the message payload to a String.
* Ignored if a {@link BytesMessageMapper} is provided.
*
* @param payloadAsBytes The payloadAsBytes to set.
* @see #setBytesMessageMapper(BytesMessageMapper)
*/
public void setPayloadAsBytes(boolean payloadAsBytes) {
this.payloadAsBytes = payloadAsBytes;
}