Skip to content

Commit a3e3efa

Browse files
committed
Issue 549 - Trialing ignoring exceptions thrown in callbacks.
Signed-off-by: James Sutton <james.sutton@uk.ibm.com>
1 parent 7fb3938 commit a3e3efa

4 files changed

Lines changed: 46 additions & 13 deletions

File tree

org.eclipse.paho.mqttv5.client/src/main/java/org/eclipse/paho/mqttv5/client/internal/CommsCallback.java

Lines changed: 33 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -264,7 +264,13 @@ private void handleActionComplete(MqttToken token) throws MqttException {
264264
// If a callback is registered and delivery has finished
265265
// call delivery complete callback.
266266
if (mqttCallback != null && token instanceof MqttDeliveryToken && token.isComplete()) {
267-
mqttCallback.deliveryComplete((MqttDeliveryToken) token);
267+
try {
268+
mqttCallback.deliveryComplete((MqttDeliveryToken) token);
269+
} catch (Throwable ex) {
270+
// Just log the fact that an exception was thrown
271+
// @TRACE 726=Ignoring Exception thrown from deliveryComplete {0}
272+
log.fine(CLASS_NAME, methodName, "726", new Object[] { ex });
273+
}
268274
}
269275
// Now call async action completion callbacks
270276
fireActionEvent(token);
@@ -318,9 +324,8 @@ public void connectionLost(MqttException cause, MqttDisconnect message) {
318324
reconnectInternalCallback.disconnected(disconnectResponse);
319325
}
320326
} catch (Throwable t) {
321-
// Just log the fact that a throwable has caught connection lost
322-
// is called during shutdown processing so no need to do anything else
323-
// @TRACE 720=exception from connectionLost {0}
327+
// Just log the fact that an exception was thrown
328+
// @TRACE 720=Ignoring Exception thrown from connectionLost {0}
324329
log.fine(CLASS_NAME, methodName, "720", new Object[] { t });
325330
}
326331
}
@@ -394,8 +399,15 @@ public void messageArrived(MqttPublish sendMessage) {
394399
* The {@link MqttAuth} message.
395400
*/
396401
public void authMessageReceived(MqttAuth authMessage) {
402+
String methodName = "authMessageReceived";
397403
if (mqttCallback != null) {
398-
mqttCallback.authPacketArrived(authMessage.getReturnCode(), authMessage.getProperties());
404+
try {
405+
mqttCallback.authPacketArrived(authMessage.getReturnCode(), authMessage.getProperties());
406+
} catch (Throwable ex) {
407+
// Just log the fact that an exception was thrown
408+
// @TRACE 727=Ignoring Exception thrown from authPacketArrived {0}
409+
log.fine(CLASS_NAME, methodName, "727", new Object[] { ex });
410+
}
399411
}
400412
}
401413

@@ -411,7 +423,13 @@ public void mqttErrorOccurred(MqttException exception) {
411423
final String methodName = "mqttErrorOccurred";
412424
log.warning(CLASS_NAME, methodName, "721", new Object[] { exception.getMessage() });
413425
if (mqttCallback != null) {
414-
mqttCallback.mqttErrorOccurred(exception);
426+
try {
427+
mqttCallback.mqttErrorOccurred(exception);
428+
} catch (Exception ex) {
429+
// Just log the fact that an exception was thrown
430+
// @TRACE 724=Ignoring Exception thrown from mqttErrorOccurred: {0}
431+
log.fine(CLASS_NAME, methodName, "724", new Object[] { ex });
432+
}
415433
}
416434
}
417435

@@ -467,7 +485,7 @@ public void messageArrivedComplete(int messageId, int qos) throws MqttException
467485
this.clientComms.deliveryComplete(messageId);
468486
MqttPubComp pubComp = new MqttPubComp(MqttReturnCode.RETURN_CODE_SUCCESS, messageId, new MqttProperties());
469487
// @TRACE 723=Creating MqttPubComp due to manual ACK: {0}
470-
log.info(CLASS_NAME, "messageArrivedComplete", "723", new Object[] {pubComp.toString()});
488+
log.info(CLASS_NAME, "messageArrivedComplete", "723", new Object[] { pubComp.toString() });
471489

472490
this.clientComms.internalSend(pubComp, new MqttToken(clientComms.getClient().getClientId()));
473491
}
@@ -573,6 +591,7 @@ public void removeMessageListeners() {
573591

574592
protected boolean deliverMessage(String topicName, int messageId, MqttMessage aMessage) throws Exception {
575593
boolean delivered = false;
594+
String methodName = "deliverMessage";
576595

577596
if (aMessage.getProperties().getSubscriptionIdentifiers().isEmpty()) {
578597
// No Subscription IDs, use topic filter matching
@@ -602,7 +621,13 @@ protected boolean deliverMessage(String topicName, int messageId, MqttMessage aM
602621
*/
603622
if (mqttCallback != null && !delivered) {
604623
aMessage.setId(messageId);
605-
mqttCallback.messageArrived(topicName, aMessage);
624+
try {
625+
mqttCallback.messageArrived(topicName, aMessage);
626+
} catch (Exception ex) {
627+
// Just log the fact that an exception was thrown
628+
// @TRACE 725=Ignoring Exception thrown from messageArrived: {0}
629+
log.fine(CLASS_NAME, methodName, "725", new Object[] { ex });
630+
}
606631
delivered = true;
607632
}
608633

org.eclipse.paho.mqttv5.client/src/main/java/org/eclipse/paho/mqttv5/client/internal/ConnectActionListener.java

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -159,7 +159,12 @@ public void onSuccess(IMqttToken token) {
159159

160160
if (mqttCallback != null) {
161161
String serverURI = comms.getNetworkModules()[comms.getNetworkModuleIndex()].getServerURI();
162-
mqttCallback.connectComplete(reconnect, serverURI);
162+
try {
163+
mqttCallback.connectComplete(reconnect, serverURI);
164+
} catch (Throwable ex) {
165+
// Just catch any exceptions thrown here and ignore.
166+
}
167+
163168
}
164169

165170
}

org.eclipse.paho.mqttv5.client/src/main/resources/org/eclipse/paho/mqttv5/client/internal/nls/logcat.properties

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -162,10 +162,16 @@
162162
716=call onSuccess key={0}
163163
717=call onFailure key {0}
164164
719=callback threw ex:
165-
720=exception from connectionLost {0}
165+
720=Ignoring Exception thrown from connectionLost {0}
166+
725=Ignoring Exception thrown from messageArrived: {0}
167+
726=726=Ignoring Exception thrown from deliveryComplete {0}
166168
721=Non-Critical MQTT error thrown, passing back to application={0}
167169
722=Server initiated disconnect, connection closed. Disconnect={0}
168170
723=Creating MqttPubComp due to manual ACK: {0}
171+
724=Ignoring Exception thrown from mqttErrorOccurred: {0}
172+
725=Ignoring Exception thrown from messageArrived: {0}
173+
726=Ignoring Exception thrown from deliveryComplete {0}
174+
727=Ignoring Exception thrown from authPacketArrived {0}
169175
800=stopping sender
170176
801=stopped
171177
802=network send key={0} msg={1}

org.eclipse.paho.mqttv5.client/src/test/java/org/eclipse/paho/mqttv5/client/test/BasicTest.java

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,6 @@
88
import org.eclipse.paho.mqttv5.client.IMqttDeliveryToken;
99
import org.eclipse.paho.mqttv5.client.IMqttToken;
1010
import org.eclipse.paho.mqttv5.client.MqttAsyncClient;
11-
import org.eclipse.paho.mqttv5.client.MqttCallback;
12-
import org.eclipse.paho.mqttv5.client.MqttDisconnectResponse;
1311
import org.eclipse.paho.mqttv5.client.test.logging.LoggingUtilities;
1412
import org.eclipse.paho.mqttv5.client.test.properties.TestProperties;
1513
import org.eclipse.paho.mqttv5.client.test.utilities.MqttV5Receiver;
@@ -18,7 +16,6 @@
1816
import org.eclipse.paho.mqttv5.common.MqttException;
1917
import org.eclipse.paho.mqttv5.common.MqttMessage;
2018
import org.eclipse.paho.mqttv5.common.MqttSubscription;
21-
import org.eclipse.paho.mqttv5.common.packet.MqttProperties;
2219
import org.junit.Assert;
2320
import org.junit.BeforeClass;
2421
import org.junit.Test;

0 commit comments

Comments
 (0)