在Camel Route

时间:2016-01-27 16:23:10

标签: apache-camel mqtt

我正在尝试配置Apache Camel Spring Route。我想使用来自MQTT的消息并使用他们的主题打印它们。如何获取消息发布的主题?

我正在使用Apache Camel 2.16.1
我的camel-context.xml

<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xsi:schemaLocation="
       http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
       http://camel.apache.org/schema/spring http://camel.apache.org/schema/spring/camel-spring.xsd">

    <camelContext xmlns="http://camel.apache.org/schema/spring" id="MqttCamel">
        <route>
            <from uri="mqtt:iot?host=tcp://iot.eclipse.org:1883&amp;subscribeTopicNames=#" />
            <convertBodyTo type="java.lang.String" />
            <to uri="stream:out" />
        </route>
    </camelContext>

</beans>

2 个答案:

答案 0 :(得分:1)

使用MQTT组件,主题存储在名为&#34; CamelMQTTSubscribeTopic&#34;的标题中:

{{1}}

答案 1 :(得分:1)

如果说Apache Paho,则使用CamelMqttTopic标头名称。可以使用

$ {in.header.CamelMqttTopic}

$ {header.CamelMqttTopic}

所以这个

from("paho:#")
                .log("Message read from topic ${in.header.CamelMqttTopic}.")
                .log("Message read from topic ${header.CamelMqttTopic}.")
                .bean(mqttService, "received(${header.CamelMqttTopic}, ${body})")
        ;

有效。

接收的方法是

public void received(String topic, String payload) {
        log.info("topic: {}", topic);
        log.info("payload: {}", payload);
    }

我进入了日志:

Message read from topic my/topic/name.
Message read from topic my/topic/name.
topic: my/topic/name
payload: payload message

在此处查看https://cwiki.apache.org/confluence/display/CAMEL/Paho

在Paho 2.25.1上测试