gpt4 book ai didi

java - MQTT/Camel/自定义 header 参数

转载 作者:太空宇宙 更新时间:2023-11-04 12:42:56 24 4
gpt4 key购买 nike

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

代码示例:

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

如您所见,发送自定义 header 值不起作用。怎么了?

最佳答案

MQTT 协议(protocol) AFAIR 仅支持单个有效负载,没有任何附加 header 。

关于java - MQTT/Camel/自定义 header 参数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36644161/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com