您好我正在尝试使用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
不是已发布但很多次答案 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"/>