2016-10-14 37 views
2

嗨我试图使用弹簧集成来接收MQTT消息,处理它们并发布到另一个主题。春季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 and not mqtt/akki
  • The processed_data没有发布的信息,但很多时候

回答

3

这是正确的,因为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填充从MqttPahoMessageDrivenChannelAdapter上消息到达MqttHeaders.TOPIC头。

你应该考虑将消息发送到<int-mqtt:outbound-channel-adapter>

+1

您也可以使用头,浓缩塔更换主题标题前使用<int:header-filter header-names="mqtt_topic"/>(覆盖设置为true)。 –