2

当我运行 Eclipse 并编译我的程序时,它只工作一次,在第一次程序执行后,如果我再次尝试运行它,我会收到:

Exception in thread "main" Persistence is already in use

为了解决这个问题,我必须关闭 Eclipse 并重新打开它,每次都会发生同样的事情,我可以运行我的程序一次,否则我会收到发布的消息。

该错误是什么意思,我该如何解决?

更新

client != null
Client is Connected
synchCB->@onSuccess(): Connection Successfull
Client is CONNECTED and Resources will be released.
AsynchCB->@deliveryComplete(): 
MSG: .00088782
Exception in thread "main" Verbindung zu Client ist hergestellt (32100)
    at  
  org.eclipse.paho.client.mqttv3.internal.ExceptionHelper.createMqttException(ExceptionHelper.java:31)
    at org.eclipse.paho.client.mqttv3.internal.ClientComms.close(ClientComms.java:166)
    at org.eclipse.paho.client.mqttv3.MqttAsyncClient.close(MqttAsyncClient.java:873)
    at org.eclipse.paho.client.mqttv3.MqttClient.close(MqttClient.java:401)
    at com.example.mqtt_java.MainClass.releaseResources(MainClass.java:87)
    at com.example.mqtt_java.MainClass.main(MainClass.java:53)

代码

client = new MqttClient(serverURI, clientID);
    //client = MQTTClientFactory.newClient(IP, PORT, clientID);

    if (client != null) {
        System.out.println("client is not null");

        //client.setCallback(AsynchCB);
        //IMqttToken token = client.connectWithResult(opts);
        client.connect();
        if (client.isConnected()) {
            System.out.println("Client is Connected");

            client.publish(TOPIC, "33300".getBytes(), QoS, false);
            client.close();
            client.disconnect();
        }else {
            System.out.println("client is not connected");
        }
    }else {
        System.out.println("client = null");
    }
}

控制台输出

Exception in thread "main" Persistenz wird bereits verwendet (32200)
at   
 rg.eclipse.paho.client.mqttv3.persist.MqttDefaultFilePersistence.open(MqttDefaultFilePersistence.java:112)
at org.eclipse.paho.client.mqttv3.MqttAsyncClient.<init>(MqttAsyncClient.java:286)
at org.eclipse.paho.client.mqttv3.MqttAsyncClient.<init>(MqttAsyncClient.java:167)
at org.eclipse.paho.client.mqttv3.MqttClient.<init>(MqttClient.java:224)
at org.eclipse.paho.client.mqttv3.MqttClient.<init>(MqttClient.java:136)
at com.example.mqtt_java.MainClass.main(MainClass.java:47)
4

1 回答 1

1

检查您是否真的关闭了程序的原始版本(单击控制台中的红叉)。

您可能有一些尚未关闭的后台 (MQTT) 线程,因此保持应用程序运行并保持对持久性目录的锁定。

于 2014-11-28T11:21:55.300 回答