@Override public boolean apply(Void v) { return !client.isConnected() && !stopped; } })
/** * Returns whether this streamer is connected by delegating to the underlying {@link MqttClient#isConnected()} * * @return {@code true} if connected; {@code false} if not. * @see MqttClient#isConnected() */ public boolean isConnected() { return client.isConnected(); }
private void stopMqttClient() { try { // Check if connected so subsequent calls does not produce an already stopped exception if ( !connectionError.get() && client.get() != null && client.get().isConnected() ) { log.logDebug( getString( PKG, "MQTTProducer.Log.Closing" ) ); client.get().disconnect(); client.get().close(); } } catch ( IllegalArgumentException | MqttException e ) { logError( 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 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()); } }
assertFalse(clientForSubscribe1.isConnected()); assertTrue(clientForSubscribe2.isConnected()); LOG.info("Waiting to receive 1 sec from {}", clientForSubscribe2.getClientId()); MqttMessage messageOnB = cbSubscriber2.waitMessage(1);
/** * Use this to validate if connection to server is still active. * * @return Is MQTT client connected to the server */ public boolean isConnected() { return mqttClient.isConnected(); }
@Override public boolean isOpen() { return mqttClient != null && mqttClient.isConnected(); }
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));
@Override protected synchronized void doDisconnect(MqttClient client) throws Exception { if (client.isConnected()) client.disconnect(); }
/** * Checks if this {@link MqttClient} is connected. * * @return {@code true} if connected, {@code false} otherwise. * @since 1.0.0 */ public boolean isConnected() { try { return getPahoClient().isConnected(); } catch (KapuaException e) { // FIXME: add log return false; } }
/** * Checks if this {@link MqttClient} is connected. * * @return {@code true} if connected, {@code false} otherwise. * @since 1.0.0 */ public boolean isConnected() { try { return getPahoClient().isConnected(); } catch (KapuaException e) { // FIXME: add log return false; } }
public void destroy() { try { if (this.client.isConnected()) { this.client.disconnectForcibly(5000); } } catch (MqttException ex) { LOG.warn(ex.getMessage(), ex); } this.destroyed = true; }
private boolean clientExistsAndIsNotConnected() { boolean clientExistsAndIsNotConnected = false; long stamp = mqttClientLock.readLock(); try { clientExistsAndIsNotConnected = (mqttClient != null && !mqttClient.isConnected()); } finally { mqttClientLock.unlockRead(stamp); } return clientExistsAndIsNotConnected; }
@Then("^Client named \"(.*)\" is connected$") public void clientConnected(String clientName) throws Exception { MqttClient mqttClient = (MqttClient) stepData.get(clientName); if (mqttClient == null) { throw new Exception("Mqtt test client not found"); } assertEquals(true, mqttClient.isConnected()); }
@Then("^Client named \"(.*)\" is not connected$") public void clientNotConnected(String clientName) throws Exception { MqttClient mqttClient = (MqttClient) stepData.get(clientName); if (mqttClient == null) { throw new Exception("Mqtt test client not found"); } assertEquals(false, mqttClient.isConnected()); }
public synchronized void unSubscribeToOpenGroupTopic(Channel channel) { try { String currentId = null; if (channel != null) { currentId = String.valueOf(channel.getKey()); } final MqttClient client = connect(); if (client == null || !client.isConnected()) { return; } client.unsubscribe(OPEN_GROUP + getApplicationKey(context) + "-" + currentId); Utils.printLog(context, TAG, "UnSubscribed to topic: " + OPEN_GROUP + getApplicationKey(context) + "-" + currentId); } catch (Exception e) { e.printStackTrace(); } }
@After public void clean() throws MqttException { messageArrived = false; messageIds.clear(); if (subscriber.isConnected()) { subscriber.disconnect(); } if (sender.isConnected()) { sender.disconnect(); } subscriber.close(); sender.close(); }
@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(); } }
@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(); } }