Eclipse Paho主方法中如何获取回调方法(messageArrived)到达的消息的payload?

How to access the payload of the message arrived of the callback method (messageArrived) in the main method Eclipse Paho?

问题陈述:- 我正在尝试自动化 MQTT 流程,为此我需要按顺序发布和订阅多个主题。技巧部分是从第一次发布收到的消息有一些值,将在下一个 sub/pub 命令中传递。

例如。

  1. 分给 topicA/abc
  2. 发布到 topicA/abc
  3. 在 topicA/abc 收到的消息是 xyz
  4. 子主题 topicA/xyz
  5. 发布到主题 topicA/xyz

我能够收到关于第一个主题的消息,但我不知道如何在 main 方法中访问接收到的消息的有效负载,并将其传递并附加到下一个主题以供下一个子主题使用。

有没有办法从 messageArrived 回调方法获取消息负载到创建客户端实例的主方法?

注意:- 我使用单个客户端进行发布和订阅。

请帮助我,因为我 运行 没有选择和方法。

已编辑:-

代码片段

主要class

public class MqttOverSSL {
String deviceId;
MqttClient client = null;

public MqttOverSSL() {

}

public MqttOverSSL(String deviceId) throws MqttException, InterruptedException {
    this.deviceId = deviceId;
    MqttConnection mqttConObj = new MqttConnection();
    this.client = mqttConObj.mqttConnection();
}

public void getLinkCodeMethod() throws MqttException, InterruptedException {

    client.subscribe("abc/multi/" + deviceId + "/linkcode", 0);
    publish(client, "abc/multi/" + deviceId + "/getlinkcode", 0, "".getBytes());


}

}

Mqtt Claback 实现:-

public class SimpleMqttCallBack implements MqttCallback {

  String arrivedMessage;

  @Override
  public void connectionLost(Throwable throwable) {
    System.out.println("Connection to MQTT broker lost!");
  }

  @Override
  public void messageArrived(String s, MqttMessage mqttMessage) throws Exception {

    arrivedMessage = mqttMessage.toString();

    System.out.println("Message received:\t" + arrivedMessage);

    linkCode(arrivedMessage);
  }

  @Override
  public void deliveryComplete(IMqttDeliveryToken iMqttDeliveryToken) {
    System.out.println("Delivery complete callback: Publish Completed "+ Arrays.toString(iMqttDeliveryToken.getTopics()));
  }


  public void linkCode(String arrivedMessage) throws MqttException {
    System.out.println("String is "+ arrivedMessage);
    Gson g = new Gson();
    GetCode code = g.fromJson(arrivedMessage, GetCode.class);
    System.out.println(code.getLinkCode());
  }

}

发布者 class:-

public class Publisher {
    public static void publish(MqttClient client, String topicName, int qos, byte[] payload) throws MqttException {

        String time = new Timestamp(System.currentTimeMillis()).toString();
        log("Publishing at: "+time+ " to topic \""+topicName+"\" qos "+qos);

        // Create and configure a message
        MqttMessage message = new MqttMessage(payload);
        message.setQos(qos);

        // Send the message to the server, control is not returned until
        // it has been delivered to the server meeting the specified
        // quality of service.
        client.publish(topicName, message);
    }

    static private void log(String message) {
        boolean quietMode   = false;
        if (!quietMode) {
            System.out.println(message);
        }
    }
}

好的,你现在想做什么更清楚了。

简答不,您不能将值传回 "main method"。 MQTT 是异步的,这意味着您不知道您订阅的主题的消息何时到达。

您需要更新您的代码以处理检查传入消息的主题是什么,然后处理您想要对 messageArrived() 处理程序中的响应执行的任何操作。如果您有一系列任务要做,那么您可能需要实现所谓的状态机,以便跟踪您在序列中的位置。