Yes, I total agree with Ian the Ping interface is NOT well designed, and should avoid it whenever possible.
"
The requirement is, whenever network connectivity changes, I want to send an explicit forced ping to ensure the connection is live and I am able to receive message with same connection. If not it should terminate existing connection and I will reconnect then.
"
I think the connectionLost will be called each time the network connectivity changes, it means the socket is broken and then the ping is meaningless.
And I don't think you can reuse the previous connection. If the socket is broken you will need to reconnect.
So, force ping is not really necessary in my opinion.
Best Regards,
Bin Zhang(张斌)
--------------------------------------------------------------------------------------------
WebSphere MQ, IBM China Software Development Lab
Notes: Bin BJ Zhang/China/IBM
E-Mail: zhbinbj@xxxxxxxxxx
Address: Ring Building 3F, ZhongGuanCun Software Park,
DongBeiWang West Road No.8, Haidian District, Beijing, 100193, China
-------------------------------------------------------------------------------------------
Ian Craggs ---08/17/2014 05:52:50 AM---Hi Prashant, so looking at the documentation here:
From: Ian Craggs <icraggs@xxxxxxxxxxxxxxxxxxxxxxx>
To: paho-dev@xxxxxxxxxxx
Date: 08/17/2014 05:52 AM
Subject: Re: [paho-dev] Android - Paho Mqtt client does not receive messages once network connectivity changes (mobile data disabled and enabled again)
Sent by: paho-dev-bounces@xxxxxxxxxxx
Hi Prashant,
so looking at the documentation here: http://www.eclipse.org/paho/files/javadoc/index.html
I see that an interface MqttPingSender has been introduced to allow an application to implement its own ping strategy. I must admit that this does not look like the sort of interface I was expecting.
Bin, Al - could you explain to me how this is supposed to be used? Naively I might have expected a method called Ping() in the client API where additional pings could be sent by the application as required. Is anything more than this really necessary?
Ian
On 15/08/14 21:42, Prashant Kedia wrote:
Hi,
This is how I have implemented explicit forced ping as of now.
public class EnhancedMqttClient extends MqttClient {
protected static final String TAG_CLASS_NAME = EnhancedMqttClient.class
.getName();
private ExecutorService executor;
private Object lock = new Object();
public EnhancedMqttClient(Context context, String serverURI,
String clientId, MqttClientPersistence persistence)
throws MqttException {
super(serverURI, clientId, persistence);
aClient = new MyMqttAsyncClient(serverURI, clientId, persistence);
}
public void ping() throws MqttException {
try {
executor = Executors.newSingleThreadExecutor();
executor.submit(new PingExecutor()).get(10, TimeUnit.SECONDS);
} catch (TimeoutException e) {
Log.e(TAG_CLASS_NAME, "Ping Failure - Timeout");
disconnectForcibly(1000, 1000);
} catch (InterruptedException e) {
e.printStackTrace();
} catch (ExecutionException e) {
e.printStackTrace();
} finally {
executor.shutdownNow();
}
}
private class PingExecutor implements Callable<Boolean> {
@Override
public Boolean call() throws Exception {
MqttDeliveryToken token = new MqttDeliveryToken(getClientId());
token.setActionCallback(new IMqttActionListener() {
@Override
public void onSuccess(IMqttToken asyncActionToken) {
System.out.println("Ping Success");
synchronized (lock) {
lock.notify();
}
}
@Override
public void onFailure(IMqttToken asyncActionToken, Throwable exception) {
System.out.println("Ping Failed");
try {
if (isConnected()) {
disconnectForcibly(1000, 1000);
}
} catch (MqttException e) {
e.printStackTrace();
}
}
});
MqttPingReq pingMsg = new MqttPingReq();
((MyMqttAsyncClient) aClient).getClientComms().sendNoWait(pingMsg, token);
synchronized (lock) {
lock.wait();
}
return true;
}
}
}
There are two issues with this implementation.
1) onFailure callback MqttDeliveryToken does not gets called every time ping fails. That's why I had to implement timer here with the help of ExecutorService.
2) On failure I call disconnectForcibly. It terminates all the mqtt callback threads and disconnect client as expected, but it does not make a call to mqtt callback connectionLost as there is no cause for shutdown here.
Please let me know if there is any better way to implement the same.
On Fri, Aug 15, 2014 at 11:13 AM, Prashant Kedia <prashantkedia22@xxxxxxxxx> wrote:
Yes, It detects network connectivity changes.
On Fri, Aug 15, 2014 at 4:19 AM, Ian Craggs <icraggs@xxxxxxxxxxxxxxxxxxxxxxx> wrote:
Hi Prashant,
how will your application know when to send the ping? Will it detect the network connectivity change?
Ian
On 08/14/2014 04:48 PM, Prashant Kedia wrote:
--
Ian Craggs
icraggs@xxxxxxxxxx IBM United Kingdom
Committer on Paho, Mosquitto
_______________________________________________
paho-dev mailing list
paho-dev@xxxxxxxxxxx
To change your delivery options, retrieve your password, or unsubscribe from this list, visit
https://dev.eclipse.org/mailman/listinfo/paho-dev
--
Thanks and Regards,
Prashant Kedia
Co-Founder and Developer
Bizlers Technologies Pvt. Ltd.
--
Thanks and Regards,
Prashant Kedia
Co-Founder and Developer
Bizlers Technologies Pvt. Ltd.
_______________________________________________
paho-dev mailing list
paho-dev@xxxxxxxxxxx
To change your delivery options, retrieve your password, or unsubscribe from this list, visit
https://dev.eclipse.org/mailman/listinfo/paho-dev
_______________________________________________
paho-dev mailing list
paho-dev@xxxxxxxxxxx
To change your delivery options, retrieve your password, or unsubscribe from this list, visit
https://dev.eclipse.org/mailman/listinfo/paho-dev
|