Home > Blockchain >  MQTT headers with Spring Integration
MQTT headers with Spring Integration

Time:06-08

I'm using MQTT with Spring Integration.

I would like to include a contentType header in the MQTT message.

I'm writing to the MQTT broker in this way:

  @Bean
  public IntegrationFlow outgoingMqttMsgFlow() {
    return IntegrationFlows.from("outgoingMqttMsgChannel")
        .enrichHeaders(headers -> headers.header(MqttHeaders.TOPIC, mqttTopic))
        .enrichHeaders(headers -> headers.header(MessageHeaders.CONTENT_TYPE, "usp.msg", true))
        .handle(new MqttPahoMessageHandler(mqttBroker, UUID.randomUUID().toString())).get();
  }

When I see the MQTT messages in RabbitMQ I see this:

enter image description here

This is, it seems that content type header is not being included in the message (only x-mqtt-dup and x-mqtt-publish-qos).

Am I doing something wrong?

Thanks in advance.

CodePudding user response:

MQTT v3 has no concept of "headers"; those headers are just the rabbitmq MQTT plugin's mechanism to handle QOS.

You would need to embed the content type in the payload somehow.

CodePudding user response:

MQTT does not have headers but properties, but they represent a similar concept.

MQTT properties are supported since version v5.

If you want to be able to map Spring Integration headers into MQTT properties you have to use Mqttv5PahoMessageHandler.

There is an example in https://docs.spring.io/spring-integration/reference/html/mqtt.html

Finally, take into account that since RabbitMQ does not support MQTT v5, it neither supports MQTT properties. Use Mosquitto instead.

  • Related