MQTT / Camel / Custom标头参数

时间:2016-04-15 10:04:22

标签: java jms apache-camel mqtt

所以,问题是: 我需要通过Apache Camel / MQTT在头文件中发送一些自定义值。

代码示例:

import java.util.Map;

import org.apache.camel.CamelContext;
import org.apache.camel.Message;
import org.apache.camel.impl.DefaultCamelContext;

public class MQTTEntryPoint {

    private static final String BASE = "mqtt:test?host=tcp://ip_address&";
    private static final String SUBSCRIBER = BASE + "subscribeTopicName=test2";
    private static final String PUBLISHER = BASE + "publishTopicName=test2";

    public static void main(String[] args) {
        CamelContext camelContext = new DefaultCamelContext();
        Thread thread = new Thread(new Runnable() {
            @Override
            public void run() {
                Message message = camelContext.createConsumerTemplate().receive(SUBSCRIBER).getIn();
                String body = message.getBody(String.class);
                System.out.println(body);
                Map<String, Object> headers = message.getHeaders();
                for (String key : headers.keySet()) {
                    System.out.println("Header key: " + key + ", Header value: " + headers.get(key));
                }
            }
        });
        thread.start();

        camelContext.createProducerTemplate().sendBodyAndHeader(PUBLISHER, "some body value", "headerKey",
                "some header value");

    }
}

在系统中,我只能看到:

some body value
Header key: CamelMQTTSubscribeTopic, Header value: test2

如您所见,发送自定义标头值不起作用。有什么问题?

1 个答案:

答案 0 :(得分:0)

MQTT协议AFAIR仅支持单个有效负载,无需任何其他标头。