Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/364.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 如何访问主方法EclipsePAHO中回调方法(MessageArrized)的消息到达的有效负载?_Java_Mqtt_Iot_Paho - Fatal编程技术网

Java 如何访问主方法EclipsePAHO中回调方法(MessageArrized)的消息到达的有效负载?

Java 如何访问主方法EclipsePAHO中回调方法(MessageArrized)的消息到达的有效负载?,java,mqtt,iot,paho,Java,Mqtt,Iot,Paho,问题陈述:-我正在尝试自动化MQTT流,为此我需要按顺序发布和订阅多个主题。诀窍在于,从第一次发布接收到的消息具有一些值,这些值将在下一个sub/pub命令中传递 例如 分包至topicA/abc 酒吧至topicA/abc topicA/abc上收到的消息是xyz 主题topicA/xyz的子主题 发布到主题topicA/xyz 我能够在第一个主题上接收消息,但我不知道如何在main方法中访问接收到的消息的有效负载,并将其传递并附加到下一个子主题 是否有一种方法可以将从messageArriv

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

例如

  • 分包至topicA/abc
  • 酒吧至topicA/abc
  • topicA/abc上收到的消息是xyz
  • 主题topicA/xyz的子主题
  • 发布到主题topicA/xyz
  • 我能够在第一个主题上接收消息,但我不知道如何在main方法中访问接收到的消息的有效负载,并将其传递并附加到下一个子主题

    是否有一种方法可以将从messageArrived回调方法检索到的消息有效负载获取到创建客户机实例的主方法

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

    请帮助我,因为我已经没有了这样做的选择和方法

    编辑:-

    代码片段

    主类

    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回显:-

    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());
      }
    
    }
    
    出版商类别:-

    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);
            }
        }
    }
    

    好的,你现在想做的事情就更清楚了

    简短回答否,不能将值传递回“主方法”。MQTT是异步的,这意味着您不知道订阅主题的消息何时到达


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

    如果不与我们共享任何代码,我们真的无法帮助Hi hardilib,在原始帖子中添加了相同的代码。请看一看。