简体   繁体   中英

Connection lost on MQTT subscribe to Internet of Things Server

I'm writing a Java application to publish/subscribe to Internet of Things MQTT server using the Eclipse Paho Lib (org.eclipse.paho.client.mqtt3-1.0.2.jar), both on Device and Application side.

Connect works well with both credential types, and same seems to be the publish... What gives me the error is the subscribe:

Trying it by mosquitto_sub command line, it loops like this:

Client a:u5o0ux:tws sending CONNECT
Client a:u5o0ux:tws received CONNACK
Client a:u5o0ux:tws sending SUBSCRIBE (Mid: 1, Topic: matteo, QoS: 0)
Client a:u5o0ux:tws sending CONNECT
Client a:u5o0ux:tws received CONNACK
Client a:u5o0ux:tws sending SUBSCRIBE (Mid: 2, Topic: matteo, QoS: 0)
Client a:u5o0ux:tws sending CONNECT
Client a:u5o0ux:tws received CONNACK
Client a:u5o0ux:tws sending SUBSCRIBE (Mid: 3, Topic: matteo, QoS: 0)
Client a:u5o0ux:tws sending CONNECT
Client a:u5o0ux:tws received CONNACK
Client a:u5o0ux:tws sending SUBSCRIBE (Mid: 4, Topic: matteo, QoS: 0)
...

And so on.

When trying from java with an MqttAsyncClient, the subcribe() method returns, but then the waitForCompletion() method istantly trows:

Connection lost (32109) - java.io.EOFException

This is the code I am running:

String tmpDir = System.getProperty("java.io.tmpdir");
MqttDefaultFilePersistence dataStore = new MqttDefaultFilePersistence(tmpDir); 

...

// Construct a non blocking MQTT client instance
client = new MqttAsyncClient(getMQTTBrokerURL(), clientId, dataStore);

// Set this wrapper as the callback handler
client.setCallback(this);

and then:

connect();

...

IMqttToken subToken = client.subscribe(topic, qos, null, null);
subToken.waitForCompletion();

Also, this error makes the lib to not relese the persistence path user by the AsyncClient, making it to trow a "Persistence Already in Use" exception on every retry, until I stop the JVM and manually clear that path, but i suppose this to be some sort of library bug.

Unfortunately, I can't (or don't know how) access the IoT-side mqtt server to realize what's going on in there.

Any ideas? Thank you

It appears your problem is likely due to invalid topic "matteo".

For connecting to the IoT Foundation in Bluemix, you will need to follow the topic format as outlined in the IBM Internet of Things Foundation documentation here: https://docs.internetofthings.ibmcloud.com/messaging/applications.html

If both mosquitto_sub and your Paho Java client are having problems at the same point (SUBSCRIBE), that implies the problem with your broker.

I tried running this:

mosquitto_sub -t matteo -i 'a:u5o0ux:tws' -d -h test.mosquitto.org

And got the output:

Client a:u5o0ux:tws sending CONNECT
Client a:u5o0ux:tws received CONNACK
Client a:u5o0ux:tws sending SUBSCRIBE (Mid: 1, Topic: matteo, QoS: 0)
Client a:u5o0ux:tws received SUBACK
Subscribed (mid: 1): 0

Maybe you could try connecting your client to test.mosquitto.org , iot.eclipse.org or one of the other public MQTT brokers out there. Or you could run your own copy of a broker locally for testing.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM