protected void onStopped() { try { logger.info("Disconnecting client"); mqttClient.disconnect(DISCONNECT_TIMEOUT); } catch(MqttException me) { logger.error("Error disconnecting MQTT client due to {}", new Object[]{me.getMessage()}, me); } try { logger.info("Closing client"); mqttClient.close(); mqttClient = null; } catch (MqttException me) { logger.error("Error closing MQTT client due to {}", new Object[]{me.getMessage()}, me); } }
public void publishMessage(String topic, String content, Integer qos, Boolean retain) { MqttMessage message = new MqttMessage(StringEscapeUtils.unescapeJava(content).getBytes()); message.setQos(Optional.ofNullable(qos).orElse(1)); message.setRetained(Optional.ofNullable(retain).orElse(false)); try { if(!myClient.isConnected()) { try { myClient.connect(); } catch (MqttSecurityException e1) { log.error("Could not retry connect to MQTT client for name: " + myConfig.getName() + " and ip: " + myConfig.getIp() + " with message: " + e1.getMessage()); return; } catch (MqttException e1) { log.error("Could not retry connect to MQTT client for name: " + myConfig.getName() + " and ip: " + myConfig.getIp() + " with message: " + e1.getMessage()); return; } } myClient.publish(topic, message); } catch (MqttException e) { log.error("Could not publish to MQTT client for name: " + myConfig.getName() + " and ip: " + myConfig.getIp() + " with message: " + e.getMessage()); } }
} catch (MqttException me) { log.error("reason " + me.getReasonCode()); log.error("msg " + me.getMessage()); log.error("loc " + me.getLocalizedMessage()); log.error("cause " + me.getCause());
@Override public void close() { super.close(); try { // Check if connected so subsequent calls does not produce an already stopped exception if ( mqttClient != null && mqttClient.isConnected() ) { mqttClient.disconnect(); mqttClient.close(); } } catch ( MqttException e ) { mqttConsumer.logError( e.getMessage() ); } } }
public MQTTHandler(NamedIP aConfig) { super(); log.info("Setting up handler for name: " + aConfig.getName()); MemoryPersistence persistence = new MemoryPersistence(); myConfig = aConfig; try { myClient = new MqttClient("tcp://" + myConfig.getIp(), myConfig.getName(), persistence); } catch (MqttException e) { log.error("Could not create MQTT client for name: " + myConfig.getName() + " and ip: " + myConfig.getIp() + " with message: " + e.getMessage()); } MqttConnectOptions connOpts = new MqttConnectOptions(); connOpts.setCleanSession(true); connOpts.setAutomaticReconnect(true); if(aConfig.getUsername() != null && aConfig.getUsername().trim().length() > 0) { if(aConfig.getPassword() != null && aConfig.getPassword().trim().length() > 0) { connOpts.setUserName(aConfig.getUsername().trim()); connOpts.setPassword(aConfig.getPassword().trim().toCharArray()); } } try { myClient.connect(connOpts); } catch (MqttException e) { log.error("Could not connect MQTT client for name: " + myConfig.getName() + " and ip: " + myConfig.getIp() + " with message: " + e.getMessage()); } }
} catch (MqttException me) { log.error("reason " + me.getReasonCode()); log.error("msg " + me.getMessage()); log.error("loc " + me.getLocalizedMessage()); log.error("cause " + me.getCause());
/** * Returns a <code>String</code> representation of this exception. * @return a <code>String</code> representation of this exception. */ public String toString() { String result = getMessage() + " (" + reasonCode + ")"; if (cause != null) { result = result + " - " + cause.toString(); } return result; } }
@Test public void connectWithWrongCredentials() { LOG.info("*** connectWithWrongCredentials ***"); try { m_client = new MqttClient("tcp://localhost:1883", "Publisher", s_pubDataStore); MqttConnectOptions options = new MqttConnectOptions(); options.setUserName("dbuser"); options.setPassword("wrongPassword".toCharArray()); m_client.connect(options); } catch (MqttException e) { if (e instanceof MqttSecurityException) { assertTrue(true); return; } else { assertTrue(e.getMessage(), false); return; } } assertTrue("must not be connected. cause : wrong password given to client", false); }
@Override public boolean processRow( StepMetaInterface smi, StepDataInterface sdi ) throws KettleException { Object[] row = getRow(); if ( null == row ) { setOutputDone(); return false; } try { client.get() // client is memoized, loaded on first use .publish( getTopic( row ), getMessage( row ) ); incrementLinesOutput(); putRow( getInputRowMeta(), row ); // copy row to possible alternate rowset(s). if ( checkFeedback( getLinesRead() ) && log.isBasic() ) { logBasic( getString( PKG, "MQTTProducer.Log.LineNumber" ) + getLinesRead() ); } } catch ( MqttException e ) { logError( getString( PKG, "MQTTProducer.Error.QOSNotSupported", meta.qos ) ); logError( e.getMessage(), e ); setErrors( 1 ); stopAll(); return false; } catch ( RuntimeException re ) { stopAll(); logError( re.getMessage(), re ); return false; } return true; }
public void disconnect(Connection connection){ try { connection.getClient().disconnect(); } catch( MqttException ex){ Log.e(TAG, "Exception occurred during disconnect: " + ex.getMessage()); } }
public void publish(Connection connection, String topic, String message, int qos, boolean retain){ try { String[] actionArgs = new String[2]; actionArgs[0] = message; actionArgs[1] = topic; final ActionListener callback = new ActionListener(this, ActionListener.Action.PUBLISH, connection, actionArgs); connection.getClient().publish(topic, message.getBytes(), qos, retain, null, callback); } catch( MqttException ex){ Log.e(TAG, "Exception occurred during publish: " + ex.getMessage()); } }
@Override public void onUnsubscribe(Subscription subscription) { try { connection.unsubscribe(subscription); System.out.println("Unsubscribed from: " + subscription.toString()); } catch (MqttException ex) { System.out.println("Failed to unsubscribe from " + subscription.toString() + ". " + ex.getMessage()); } } });
public void publishMessage(){ try { MqttMessage message = new MqttMessage(); message.setPayload(publishMessage.getBytes()); mqttAndroidClient.publish(publishTopic, message); addToHistory("Message Published"); if(!mqttAndroidClient.isConnected()){ addToHistory(mqttAndroidClient.getBufferedMessageCount() + " messages in buffer."); } } catch (MqttException e) { System.err.println("Error Publishing: " + e.getMessage()); e.printStackTrace(); } }
myClient.reconnect(); } catch (MqttException ex){ Log.e(TAG, "Exception occurred attempting to reconnect: " + ex.getMessage()); setConnectingState(false); handleException(resultBundle, ex); setConnectingState(true); } catch (MqttException e) { service.traceError(TAG, "Cannot reconnect to remote server." + e.getMessage()); setConnectingState(false); handleException(resultBundle, e);
/** * A connection action has been successfully completed, update the * connection object associated with the client this action belongs to and * then notify the user of success. */ private void connect() { Connection c = Connections.getInstance(context).getConnection(clientHandle); c.changeConnectionStatus(Connection.ConnectionStatus.CONNECTED); c.addAction("Client Connected"); Log.i(TAG, c.handle() + " connected."); try { ArrayList<Subscription> subscriptions = connection.getSubscriptions(); for (Subscription sub : subscriptions) { Log.i(TAG, "Auto-subscribing to: " + sub.getTopic() + "@ QoS: " + sub.getQos()); connection.getClient().subscribe(sub.getTopic(), sub.getQos()); } } catch (MqttException ex){ Log.e(TAG, "Failed to Auto-Subscribe: " + ex.getMessage()); } }
Log.e(TAG, "Exception occurred updating connection: " + connections.keySet().toString() + " : " + ex.getMessage());
/** * Returns a <code>String</code> representation of this exception. * @return a <code>String</code> representation of this exception. */ public String toString() { String result = getMessage() + " (" + reasonCode + ")"; if (cause != null) { result = result + " - " + cause.toString(); } return result; } }
/** * Returns a <code>String</code> representation of this exception. * @return a <code>String</code> representation of this exception. */ public String toString() { String result = getMessage() + " (" + reasonCode + ")"; if (cause != null) { result = result + " - " + cause.toString(); } return result; } }
/** * Returns a <code>String</code> representation of this exception. * @return a <code>String</code> representation of this exception. */ public String toString() { String result = getMessage() + " (" + reasonCode + ")"; if (cause != null) { result = result + " - " + cause.toString(); } return result; } }
public void destroy() { try { if (this.client.isConnected()) { this.client.disconnectForcibly(5000); } } catch (MqttException ex) { LOG.warn(ex.getMessage(), ex); } this.destroyed = true; }