cancel
Showing results for 
Search instead for 
Did you mean: 
cancel
Showing results for 
Search instead for 
Did you mean: 

Community Tip - Learn all about PTC Community Badges. Engage with PTC and see how many you can earn! X

Detect lost connection Java SDK

jknorr
6-Contributor

Detect lost connection Java SDK

I took one of the Java SDK application examples and wanted to write property changes into a file, as soon as the connection to the platform gets lost, so that i can try to send them again later.

My problem is that i dont know how to catch a connection loss properly.

I tried it with checking client.isConnected() before i update and push a property with updateSubscribedProperties() but somehow this works only on my Windows pc.

I have a Raspberry PI with linux and it seems like the connection won't be closed after a timeout and isConnected() returns true even if there is no real connection to ThingWorx.  And updateSubscribedProperties() doesn't throws an exception during an connection timeout on both systems.

Any idea what the reason for this could be or what a better way to detect a lost connection would be ?

Here the code of the procesScanRequest method that i call in the main of the Java SDK client to send properties periodically from the Device to the Platform:

public void processScanRequest() {

  System.out.println("ProcessScanRequest");

  Location location = getLocation();

  DateTime time = getTimestamp();

  if(client.isConnected()){

       try {

            this.setProperty("Location", location);

            this.setProperty("Timestamp", time);

            this.updateSubscribedProperties(10000);

       } catch (Exception e) {

            LOG.error("Exception occured while updating properties.", e);

       }

  }else{

       //write new property values into a file

  }

}

3 REPLIES 3
jnair1
1-Newbie
(To:jknorr)

Hi,

I am also facing the same issue. The isConnected method always return "true" even if there is no connectivity. Can anyone from Thingworx answer how to detect a connection loss?

Thanks and Regards,

Jishnu

PaiChung
22-Sapphire I
(To:jknorr)

I didn't realize isConnected wouldn't go to false, that is my expectation, but perhaps due to the time out there isn't a 'clean' closing and the value is never triggered.

A somewhat ugly alternate method is to try to execute a REST Call against the platform without any auth. If that fails, you know there no longer is a session.

Pai Chung

Yeah, I am doing something like that as a work around. Any plan to fix this in the near future?

Regards,

Jishnu

Top Tags