MqttClient 从 2 个不同的 class 发布和订阅

MqttClient Publish and Subscribe from 2 different class

假设我有 2 个不同的项目。为简单起见,每个项目包含一个 java class。我如何让他们在 java 中使用 MqttClient 发布和订阅。这是我的代码,消息正在发布,但其他 class 未收到。首先,我创建了一个连接到 Mqtt Broker 并发送消息的 TopicPublisher class。然后我创建了一个 TopicSubscriber class 连接到同一个代理并订阅主题。但即使主题发布者正在发布消息,但另一个 class 没有给出任何 output.NOTE:这两个 class 在完全不同的文件夹中,在不同的 project.What 我在做什么这里错了吗?这是我的代码:

public class TopicPublisher{
    public static void main(String[] args) {

    String topic        = "MQTT/Examples";
    String content      = "Message from MqttPublishSample";
    int qos             = 2;
    String broker       = "tcp://iot.eclipse.org:1883";
    String clientId     = "JavaSample";
    MemoryPersistence persistence = new MemoryPersistence();

    try {
        MqttClient sampleClient = new MqttClient(broker, clientId, persistence);
        MqttConnectOptions connOpts = new MqttConnectOptions();
        connOpts.setCleanSession(true);
        System.out.println("Connecting to broker: "+broker);
        sampleClient.connect(connOpts);
        System.out.println("Connected");
        System.out.println("Publishing message: "+content);
        MqttMessage message = new MqttMessage(content.getBytes());
        message.setQos(qos);
        sampleClient.publish(topic, message);
        System.out.println("Message published");
    } catch(MqttException me) {
        System.out.println("reason "+me.getReasonCode());
        System.out.println("msg "+me.getMessage());
        System.out.println("loc "+me.getLocalizedMessage());
        System.out.println("cause "+me.getCause());
        System.out.println("excep "+me);
        me.printStackTrace();
    }
}


public class TopicSubscriber implements MqttCallback{
public static void main( String[] args )
{
    try {
        MqttClient clientSub = new MqttClient("tcp://iot.eclipse.org:1883", "Subscribing");
        clientSub.connect();
        clientSub.subscribe("MQTT/#", 2);
    } catch (MqttException me) {
        // TODO Auto-generated catch block
        System.out.println("reason "+me.getReasonCode());
        System.out.println("msg "+me.getMessage());
        System.out.println("loc "+me.getLocalizedMessage());
        System.out.println("cause "+me.getCause());
        System.out.println("excep "+me);
        me.printStackTrace();
    }



}

@Override
public void connectionLost(Throwable arg0) {
    // TODO Auto-generated method stub
    System.out.println("1");
}

@Override
public void deliveryComplete(IMqttDeliveryToken arg0) {
    // TODO Auto-generated method stub
    System.out.println("2");
}

@Override
public void messageArrived(String arg0, MqttMessage arg1) throws Exception {
    // TODO Auto-generated method stub
    System.out.println(arg1.getPayload()+"!!!!!!!");

}

}

您实现了 MqttCallback 但从未将其设置为 MqttClient 的回调。 一种解决方案是添加以下内容:

clientSub.connect();
clientSub.subscribe("MQTT/#", 2);
clientSub.setCallback(new TopicSubscriber());
Thread.sleep(10000);
clientSub.disconnect();

睡眠是必要的,否则应用程序会在执行回调之前退出。

另一种选择是使用带有 IMqttMessageListener 作为参数的订阅。这样您就可以使用不同的处理程序订阅不同的主题。

可以在这里找到一个很好的概述(德语):https://jaxenter.de/iot-allrounder-27208