Refine search
String clientId = "JavaSampleProducer"; MemoryPersistence persistence = new MemoryPersistence(); MqttClient sampleClient = new MqttClient(broker, clientId, persistence); MqttConnectOptions connOpts = new MqttConnectOptions(); connOpts.setCleanSession(true); log.info("Connecting to broker: " + broker); sampleClient.connect(connOpts); log.info("Connected"); log.info("Publishing message: " + messageContent); MqttMessage message = new MqttMessage(messageContent.getBytes()); message.setQos(qos); sampleClient.publish(topic, message); log.info("Message published"); sampleClient.disconnect(); System.exit(0); } catch (MqttException me) { log.error("reason " + me.getReasonCode()); log.error("msg " + me.getMessage()); log.error("loc " + me.getLocalizedMessage()); log.error("cause " + me.getCause()); log.error("excep " + me); me.printStackTrace(); System.exit(1);
protected byte[] getVariableHeader() throws MqttException { try { ByteArrayOutputStream baos = new ByteArrayOutputStream(); DataOutputStream dos = new DataOutputStream(baos); dos.writeShort(msgId); dos.flush(); return baos.toByteArray(); } catch (IOException ex) { throw new MqttException(ex); } }
String clinetId = "JavaSampleConsumer"; MemoryPersistence persistence = new MemoryPersistence(); MqttClient sampleClient = new MqttClient(broker, clinetId, persistence); MqttConnectOptions connectOptions = new MqttConnectOptions(); connectOptions.setCleanSession(true); log.info("Connecting to broker: " + broker); sampleClient.connect(connectOptions); log.info("Connected"); sampleClient.setCallback(new MqttCallback() { @Override public void connectionLost(Throwable throwable) { sampleClient.subscribe(topic, qos); } catch (MqttException me) { log.error("reason " + me.getReasonCode()); log.error("msg " + me.getMessage()); log.error("loc " + me.getLocalizedMessage()); log.error("cause " + me.getCause()); log.error("excep " + me); me.printStackTrace(); me.printStackTrace(); System.exit(1);
@Test public void refusedServerUnavailable(TestContext context) { this.expectedReturnCode = MqttConnectReturnCode.CONNECTION_REFUSED_SERVER_UNAVAILABLE; try { MemoryPersistence persistence = new MemoryPersistence(); MqttClient client = new MqttClient(String.format("tcp://%s:%d", MQTT_SERVER_HOST, MQTT_SERVER_PORT), "12345", persistence); client.connect(); context.fail(); } catch (MqttException e) { context.assertTrue(e.getReasonCode() == MqttException.REASON_CODE_BROKER_UNAVAILABLE); } }
private void publish(TestContext context, String topic, String message, int qos) { this.async = context.async(); try { MemoryPersistence persistence = new MemoryPersistence(); MqttClient client = new MqttClient(String.format("tcp://%s:%d", MQTT_SERVER_HOST, MQTT_SERVER_PORT), "12345", persistence); client.connect(); client.publish(topic, message.getBytes(), qos, false); this.async.await(); context.assertTrue(true); } catch (MqttException e) { context.assertTrue(false); e.printStackTrace(); } }
@Test public void bruteDisconnect(TestContext context) { try { MemoryPersistence persistence = new MemoryPersistence(); MqttClient client = new MqttClient(String.format("tcp://%s:%d", MQTT_SERVER_HOST, MQTT_SERVER_PORT), "12345", persistence); client.connect(); client.close(); context.assertTrue(false); } catch (MqttException e) { context.assertTrue(e.getReasonCode() == MqttException.REASON_CODE_CLIENT_CONNECTED); e.printStackTrace(); } }
@Test public void disconnect(TestContext context) { try { MemoryPersistence persistence = new MemoryPersistence(); MqttClient client = new MqttClient(String.format("tcp://%s:%d", MQTT_SERVER_HOST, MQTT_SERVER_PORT), "12345", persistence); client.connect(); client.disconnect(); context.assertTrue(true); } catch (MqttException e) { context.assertTrue(false); e.printStackTrace(); } }
@Test public void connected(TestContext context) { try { MemoryPersistence persistence = new MemoryPersistence(); MqttClient client = new MqttClient(String.format("tcp://%s:%d", MQTT_SERVER_HOST, MQTT_SERVER_PORT), "12345", persistence); client.connect(); context.assertTrue(client.isConnected() && this.endpoint.isConnected()); } catch (MqttException e) { context.assertTrue(false); e.printStackTrace(); } }
@Override public void connect(String serverUri) throws IOException { if (client != null && client.isConnected()) { disconnect(); } Console.printText((String.format("Connect to %1$s", serverUri))); Console.printNewLine(); try { client = new org.eclipse.paho.client.mqttv3.MqttClient(serverUri, clientId, new MemoryPersistence()); client.connect(connectOptions); } catch (MqttException e) { String cause = e.getMessage(); if (e.getCause() != null) { cause = cause.concat(" : ").concat(e.getCause().getMessage()); } String errorMessage = String.format("Unable to establish an MQTT connection - %1$s", cause); throw new IOException(errorMessage, e); } }
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()); } }
connectOptions.setKeepAliveInterval(45); connectOptions.setPassword("pass".toCharArray()); MemoryPersistence persistence = new MemoryPersistence(); factory.setPersistence(persistence); final SocketFactory socketFactory = SocketFactory.getDefault(); final MqttClient client = mock(MqttClient.class); willAnswer(invocation -> client).given(factory).getClientInstance(anyString(), anyString()); given(client.isConnected()).willReturn(true); new DirectFieldAccessor(client).setPropertyValue("aClient", aClient); willAnswer(new CallsRealMethods()).given(client).connect(any(MqttConnectOptions.class)); willAnswer(new CallsRealMethods()).given(client).subscribe(any(String[].class), any(int[].class)); willReturn(alwaysComplete).given(aClient).connect(any(MqttConnectOptions.class), any(), any()); assertThat(((MqttException) e.getCause()).getReasonCode(), equalTo((int) MqttException.REASON_CODE_SUBSCRIBE_FAILED));
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()); } }
@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 synchronized void connect() { if(client != null && client.isConnected()) { return; } try { client = new MqttClient(brokerURL, deviceId); options = new MqttConnectOptions (); options.setUserName(user); options.setPassword(password.toCharArray()); client.connect(options); }catch(MqttException e) { log.error(e.getMessage(), e); } }
@Override public void connectionLost(Throwable cause) { System.err.println("Connection to MQTT server at "+client.getServerURI()+" lost. (" + location + ") " + cause); try { this.client = new MqttClient(this.uri, this.id); this.client.connect(this.options); } catch (MqttException e) { // TODO Auto-generated catch block e.printStackTrace(); } }
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); } }
@Test(expected = MqttException.class) public void checkClientAuthenticationFail() throws Exception { LOG.info("*** checkClientAuthenticationFail ***"); SSLSocketFactory ssf = configureSSLSocketFactory("unsignedclientkeystore.jks"); MqttConnectOptions options = new MqttConnectOptions(); options.setSocketFactory(ssf); // actual a "Broken pipe" is thrown, this is not very specific. try { m_client.connect(options); } catch (MqttException e) { e.printStackTrace(); throw e; } }
@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; }
@Override protected Boolean doInBackground(MqttConnectOptions... params) { options = params[0]; try { MQTT_CLIENT.connect(options); } catch (MqttSecurityException e) { if (Aware.DEBUG) Log.e(TAG, "SecurityException: " + e.getMessage()); return false; } catch (MqttException e) { if (Aware.DEBUG) Log.e(TAG, "MqttException: " + e.getMessage()); return false; } return true; }