Java Code Examples for org.eclipse.paho.client.mqttv3.MqttException#REASON_CODE_CLIENT_EXCEPTION
The following examples show how to use
org.eclipse.paho.client.mqttv3.MqttException#REASON_CODE_CLIENT_EXCEPTION .
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: CheckConnectionDialogFragment.java From PresencePublisher with MIT License | 5 votes |
private String getErrorString(Throwable e) { String message; if (e instanceof MqttException) { if (((MqttException) e).getReasonCode() == MqttException.REASON_CODE_CLIENT_EXCEPTION) { message = genericExceptionToString(e.getCause()); } else { message = e.getLocalizedMessage(); } } else { message = genericExceptionToString(e); } return context.getString(R.string.dialog_check_connection_summary_failure, message); }
Example 2
Source File: IotCoreClient.java From cloud-iot-core-androidthings with Apache License 2.0 | 5 votes |
/** * Determine whether the mqttException is an error that may be resolved by retrying or whether * the error cannot be resolved. Determined according to guidance from Cloud IoT Core's * <a href="https://cloud.google.com/iot/docs/how-tos/errors">documentation</a>. * * @return Returns true if the MQTT client should resend the message. Returns false otherwise. */ private boolean isRetryableError(MqttException mqttException) { // Retry using exponential backoff in cases where appropriate. Otherwise, log // the failure. switch (mqttException.getReasonCode()) { // Retry cases: case MqttException.REASON_CODE_SERVER_CONNECT_ERROR: case MqttException.REASON_CODE_WRITE_TIMEOUT: case MqttException.REASON_CODE_CLIENT_NOT_CONNECTED: case MqttException.REASON_CODE_CLIENT_TIMEOUT: return true; case MqttException.REASON_CODE_CLIENT_EXCEPTION: // This case happens when there is no internet connection. Unfortunately, Paho // doesn't provide a better way to get that information. if (mqttException.getCause() instanceof UnknownHostException) { return true; } case MqttException.REASON_CODE_CONNECTION_LOST: // If the MqttException's cause is an EOFException, then the client or Cloud IoT // Core closed the connection. If mRunBackgroundThread is true, then we know the // client didn't close the connection and the connection was likely closed because // the client was publishing device state updates too quickly. Mark this as a // "retryable" error so the message that caused the exception isn't discarded. if (mqttException.getCause() instanceof EOFException && mRunBackgroundThread.get()) { return true; } default: return false; } }
Example 3
Source File: IotCoreClient.java From cloud-iot-core-androidthings with Apache License 2.0 | 4 votes |
/** * Determine appropriate error to return to client based on MqttException. */ private @ConnectionCallback.DisconnectReason int getDisconnectionReason( MqttException mqttException) { switch (mqttException.getReasonCode()) { case MqttException.REASON_CODE_FAILED_AUTHENTICATION: case MqttException.REASON_CODE_NOT_AUTHORIZED: // These cases happen if the client uses an invalid IoT Core registry ID, invalid // Iot Core device ID, invalid GCP cloud region, or unregistered signing key. return ConnectionCallback.REASON_NOT_AUTHORIZED; case MqttException.REASON_CODE_CONNECTION_LOST: if (mqttException.getCause() instanceof EOFException) { // This case happens when Paho or Cloud IoT Core closes the connection. if (mRunBackgroundThread.get()) { // If mRunBackgroundThread is true, then Cloud IoT Core closed the // connection. For example, this could happen if the client used an invalid // GCP project ID, the client exceeds a rate limit set by Cloud IoT Core, or // if the MQTT broker address is invalid. return ConnectionCallback.REASON_CONNECTION_LOST; } else { // If mRunBackgroundThread is false, then the client closed the connection. return ConnectionCallback.REASON_CLIENT_CLOSED; } } if (mqttException.getCause() instanceof SSLException) { // This case happens when something goes wrong in the network that ends an // existing connection to Cloud IoT Core (e.g. the wifi driver resets). return ConnectionCallback.REASON_CONNECTION_LOST; } return ConnectionCallback.REASON_UNKNOWN; case MqttException.REASON_CODE_CLIENT_EXCEPTION: // Paho uses this reason code for several distinct error cases if (mqttException.getCause() instanceof SocketTimeoutException) { // This case could happen if the MQTT bridge port number is wrong or of there // is some other error with the MQTT bridge that keeps it from responding. return ConnectionCallback.REASON_CONNECTION_TIMEOUT; } if (mqttException.getCause() instanceof UnknownHostException) { // This case happens if the client is disconnected from the internet or if they // use an invalid hostname for the MQTT bridge. Unfortunately, Paho doesn't // provide a way to get more information. return ConnectionCallback.REASON_CONNECTION_LOST; } return ConnectionCallback.REASON_UNKNOWN; case MqttException.REASON_CODE_CLIENT_TIMEOUT: case MqttException.REASON_CODE_WRITE_TIMEOUT: return ConnectionCallback.REASON_CONNECTION_TIMEOUT; default: return ConnectionCallback.REASON_UNKNOWN; } }
Example 4
Source File: MqttPahoClient.java From joynr with Apache License 2.0 | 4 votes |
@Override public void subscribe(String topic) { boolean subscribed = false; while (!subscribed && !shutdown.get()) { logger.debug("{}: Checking for subscription to: {}", clientInformation, topic); long stamp = mqttClientLock.readLock(); try { synchronized (subscribedTopics) { if (!subscribedTopics.contains(topic)) { logger.info("{}: Attempting to subscribe to: {}", clientInformation, topic); if (mqttClient == null) { throw new MqttException(MqttException.REASON_CODE_CLIENT_NOT_CONNECTED); } mqttClient.subscribe(topic); subscribedTopics.add(topic); logger.info("{}: Subscribed to: {}", clientInformation, topic); } subscribed = true; } } catch (MqttException mqttError) { logger.debug("{}: Subscribe to {} failed: {}. Error code {}", clientInformation, topic, mqttError.getMessage(), mqttError.getReasonCode(), mqttError); switch (mqttError.getReasonCode()) { case MqttException.REASON_CODE_CLIENT_EXCEPTION: case MqttException.REASON_CODE_BROKER_UNAVAILABLE: case MqttException.REASON_CODE_CLIENT_TIMEOUT: case MqttException.REASON_CODE_CONNECT_IN_PROGRESS: case MqttException.REASON_CODE_MAX_INFLIGHT: case MqttException.REASON_CODE_NO_MESSAGE_IDS_AVAILABLE: case MqttException.REASON_CODE_SERVER_CONNECT_ERROR: case MqttException.REASON_CODE_SUBSCRIBE_FAILED: case MqttException.REASON_CODE_UNEXPECTED_ERROR: case MqttException.REASON_CODE_WRITE_TIMEOUT: case MqttException.REASON_CODE_CONNECTION_LOST: case MqttException.REASON_CODE_CLIENT_NOT_CONNECTED: case MqttException.REASON_CODE_CLIENT_DISCONNECTING: throw new JoynrIllegalStateException("Unexpected exception while subscribing to " + topic + ", error: " + mqttError, mqttError); default: throw new JoynrIllegalStateException("Unexpected exception while subscribing to " + topic + ", error: " + mqttError); } } catch (Exception e) { throw new JoynrRuntimeException("Unable to start MqttPahoClient", e); } finally { mqttClientLock.unlockRead(stamp); } } }
Example 5
Source File: MqttPahoClient.java From joynr with Apache License 2.0 | 4 votes |
@Override public void connectionLost(Throwable error) { if (error instanceof MqttException) { MqttException mqttError = (MqttException) error; int reason = mqttError.getReasonCode(); switch (reason) { // the following error codes indicate recoverable errors, hence a reconnect is initiated case MqttException.REASON_CODE_BROKER_UNAVAILABLE: case MqttException.REASON_CODE_CLIENT_TIMEOUT: case MqttException.REASON_CODE_WRITE_TIMEOUT: case MqttException.REASON_CODE_CLIENT_NOT_CONNECTED: case MqttException.REASON_CODE_INVALID_MESSAGE: case MqttException.REASON_CODE_CONNECTION_LOST: case MqttException.REASON_CODE_UNEXPECTED_ERROR: logger.debug("{}: Connection lost, trying to reconnect. Error code {}", clientInformation, reason); attemptDisconnectAndRestart(); break; case MqttException.REASON_CODE_CLIENT_EXCEPTION: String causeMessage; Throwable cause = mqttError.getCause(); if (cause != null) { causeMessage = cause.getMessage(); } else { causeMessage = "<not available>"; } logger.error("{}: Connection lost due to client exception. Cause {}", clientInformation, causeMessage); attemptDisconnectAndRestart(); break; // the following error codes indicate a configuration problem that is not recoverable through reconnecting case MqttException.REASON_CODE_INVALID_PROTOCOL_VERSION: case MqttException.REASON_CODE_INVALID_CLIENT_ID: case MqttException.REASON_CODE_FAILED_AUTHENTICATION: case MqttException.REASON_CODE_NOT_AUTHORIZED: case MqttException.REASON_CODE_SOCKET_FACTORY_MISMATCH: case MqttException.REASON_CODE_SSL_CONFIG_ERROR: logger.error("{}: Connection is incorrectly configured. Connection not possible: {}. Error code {}", clientInformation, mqttError.getMessage(), reason); shutdown(); break; // the following error codes can occur if the client is closing / already closed case MqttException.REASON_CODE_CLIENT_CLOSED: case MqttException.REASON_CODE_CLIENT_DISCONNECTING: logger.trace("{}: Connection lost due to client shutting down. Error code {}", clientInformation, reason); break; // the following error codes should not be thrown when the connectionLost() callback is called // they are listed here for the sake of completeness case MqttException.REASON_CODE_CLIENT_CONNECTED: case MqttException.REASON_CODE_SUBSCRIBE_FAILED: case MqttException.REASON_CODE_CLIENT_ALREADY_DISCONNECTED: case MqttException.REASON_CODE_CLIENT_DISCONNECT_PROHIBITED: case MqttException.REASON_CODE_CONNECT_IN_PROGRESS: case MqttException.REASON_CODE_NO_MESSAGE_IDS_AVAILABLE: case MqttException.REASON_CODE_TOKEN_INUSE: case MqttException.REASON_CODE_MAX_INFLIGHT: case MqttException.REASON_CODE_DISCONNECTED_BUFFER_FULL: default: logger.error("{}: Received error reason that should not have been thrown for connection loss: {}. Error code {}", clientInformation, mqttError.getMessage(), reason); shutdown(); } } else { logger.error("{}: Connection lost due to unknown error ", clientInformation, error); shutdown(); } }