简体   繁体   English

在Spring Integration下使用Paho接收二进制MQTT消息

[英]Use Paho under Spring Integration to receive binary MQTT message

I have this code that receives MQTT messages under Spring Integration 5.0.x / Boot 2.0. 我有这段代码在Spring Integration 5.0.x / Boot 2.0下接收MQTT消息。 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). 它适用于文本消息,但是当我尝试处理二进制消息时,它会失败,因为发生了到String的转换,这会破坏内容(在这种情况下为png图像文件)。

How can I receive the message untampered ? 如何接收不受干扰的消息?

I tried to setBytesMessageMapper on DefaultPahoMessageConverter , but this didn't change a thing. 我试图在DefaultPahoMessageConverter上设置setBytesMessageMapper ,但这并没有改变任何事情。 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. 当我使用mqtt.fx下载消息内容时,我可以证明二进制内容设置正确,因此我确定这是接收端的问题。

@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 ... 将转换器上的payloadAsBytes属性设置为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;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM