gpt4 book ai didi

java - ActiveMQ + MQTT + 订阅 "ActiveMQ.Advisory.Connection"

转载 作者:行者123 更新时间:2023-11-30 03:37:37 30 4
gpt4 key购买 nike

这是上下文:

Java 应用程序通过 MQTT (Paho 0.4.0) 从 ActiveMQ 5.9.1 订阅主题“ActiveMQ.Advisory.Connection”:

public class SupervisorMqttClient implements MqttCallback {

private MqttClient client = null;
private MemoryPersistence persistence = null;
private MqttConnectOptions connOpts = null;

private final int STATUS_OK = 0;
private final int STATUS_ERROR = 1;

private String mqttServer = null;
private String clientId = null;
private int status = STATUS_OK;

public SupervisorMqttClient() {
try {
this.init();
} catch (MqttException e) {
Logger.error(e.getLocalizedMessage());
Logger.debug(e);
}
}

private void init() throws MqttException {
Properties props = PropertiesManager.getInstance("supervisor");

mqttServer = props.getProperty("supervisor.mqtt.server");
String supervisorID = props.getProperty("supervisor.mqtt.client.number");
clientId = Supervisor.APP_NAME+"-"+supervisorID;

connOpts = new MqttConnectOptions();
connOpts.setKeepAliveInterval(30);
connOpts.setCleanSession(true); // important non-durable

persistence = new MemoryPersistence();

client = new MqttClient(mqttServer, clientId, persistence);
connectAndSubscribe();
}

private void connectAndSubscribe() throws MqttSecurityException, MqttException {
try {
client.connect(connOpts);
client.setCallback(this);
client.subscribe("ActiveMQ/Advisory/Connection");
} catch (MqttSecurityException e) {
Logger.error(e.getLocalizedMessage());
Logger.debug(e);
} catch (MqttException e) {
Logger.error(e.getLocalizedMessage());
Logger.debug(e);
processError(e);
}
}

public void publish(String orderType, JSONObject jsonExtraData) {
if (status == STATUS_ERROR) {
connectAndSubscribe();
}
if (status == STATUS_OK) {
// some code here
}
}

@Override
public void connectionLost(Throwable err) {
Logger.info("Connection lost");
}

@Override
public void deliveryComplete(IMqttDeliveryToken arg0) {
Logger.info("deliveryComplete");
}

@Override
public void messageArrived(String topic, MqttMessage msg) throws Exception {
System.out.println("MQTT Mesage Arrived[" + topic + "] Msg[" + msg.toString() + "]");
}

private void processError(MqttException e) {
status = STATUS_ERROR;
try {
if (client.isConnected()) {
Logger.error("disconnecting");
client.disconnect();
}
} catch (MqttException ex) {
Logger.error(ex.getLocalizedMessage());
Logger.debug(ex);
}
}
}

与 ActiveMQ 的连接建立良好。本主题提供有关 ActiveMQ 中连接(打开/关闭)的信息,但我的问题是我捕获的消息为空:

MQTT Mesage Arrived[ActiveMQ/Advisory/Connection] Msg[]

有什么方法可以使用 MQTT 捕获它们吗?或者我应该使用 JMS?

谢谢,乔恩·安德。

最佳答案

问题是您希望 MQTT 客户端在咨询主题上接收什么内容作为消息正文。这些建议通常包含许多信息作为消息属性,但是这些信息无法映射到 MQTT,因为 MQTT 消息没有属性。连接咨询的正文是用于创建连接的 ConnectionInfo 对象的副本。在 MQTT 方面,您对此无能为力,因为您收到的只是该对象的序列化字节,您无法对其执行任何操作。

关于java - ActiveMQ + MQTT + 订阅 "ActiveMQ.Advisory.Connection",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27486227/

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