Spring Mqtt集成:出站主题问题

时间:2016-10-14 07:37:41

标签: spring-integration mqtt mosquitto

您好我正在尝试使用spring集成来接收MQTT消息,处理它们并发布到另一个主题。

这是integration.xml:

   <int-mqtt:outbound-channel-adapter id="mqtt-publish"
    client-id="spring-foo-1"
    client-factory="clientFactory"
    auto-startup="true"
    url="tcp://localhost:1883"
    default-qos="0"
    default-retained="true"
    default-topic="tweets/akki" />

   <int-mqtt:message-driven-channel-adapter id="oneTopicAdapter"
    client-id="spring-foo-2"
    client-factory="clientFactory"
    auto-startup="true"
    url="tcp://localhost:1883"
    topics="mqtt/publish"
    />

    <int:service-activator input-channel="oneTopicAdapter" method="logMessages" ref="mqttLogger" output-channel="mqtt-publish"></int:service-activator>

    <bean id="mqttLogger" class="hello.mqttReceiver" />

和mqttReceiver.java:

package hello;
public class mqttReceiver {
   public String logMessages(String a){
       String processed_data = a; //TODO Process Data
       return processed_data;
   }
}

以下是我面临的问题:

  • processed_data路由到mqtt / publish而不是mqtt / akki
  • processed_data不是已发布但很多次

1 个答案:

答案 0 :(得分:4)

这是正确的,因为AbstractMqttMessageHandler首先会看到headers

String topic = (String) message.getHeaders().get(MqttHeaders.TOPIC);
    Object mqttMessage = this.converter.fromMessage(message, Object.class);
    if (topic == null && this.defaultTopic == null) {
        throw new MessageHandlingException(message,
                "No '" + MqttHeaders.TOPIC + "' header and no default topic defined");
    }

DefaultPahoMessageConverter在邮件到达时填充MqttHeaders.TOPIC的{​​{1}}标题。

在向MqttPahoMessageDrivenChannelAdapter

发送消息之前,您应该考虑使用<int:header-filter header-names="mqtt_topic"/>