@Before public void setUp() throws Exception { String dbPath = IntegrationUtils.localH2MvStoreDBPath(); File dbFile = new File(dbPath); assertFalse(String.format("The DB storagefile %s already exists", dbPath), dbFile.exists()); startServer(); m_client = new MqttClient("ssl://localhost:8883", "TestClient", s_dataStore); // m_client = new MqttClient("ssl://test.mosquitto.org:8883", "TestClient", s_dataStore); m_callback = new MessageCollector(); m_client.setCallback(m_callback); }
@Before public void setUp() throws Exception { String dbPath = IntegrationUtils.localH2MvStoreDBPath(); File dbFile = new File(dbPath); assertFalse(String.format("The DB storagefile %s already exists", dbPath), dbFile.exists()); startServer(); m_client = new MqttClient("ssl://localhost:8883", "TestClient", s_dataStore); // m_client = new MqttClient("ssl://test.mosquitto.org:8883", "TestClient", s_dataStore); m_callback = new MessageCollector(); m_client.setCallback(m_callback); }
private void initializeClient(ProcessContext context) { // NOTE: This method is called when isConnected returns false which can happen when the client is null, or when it is // non-null but not connected, so we need to handle each case and only create a new client when it is null try { if (mqttClient == null) { logger.debug("Creating client"); mqttClient = createMqttClient(broker, clientID, persistence); mqttClient.setCallback(this); } if (!mqttClient.isConnected()) { logger.debug("Connecting client"); mqttClient.connect(connOpts); } } catch (MqttException e) { logger.error("Connection to {} lost (or was never connected) and connection failed. Yielding processor", new Object[]{broker}, e); context.yield(); } }
private void initializeClient(ProcessContext context) { // NOTE: This method is called when isConnected returns false which can happen when the client is null, or when it is // non-null but not connected, so we need to handle each case and only create a new client when it is null try { if (mqttClient == null) { logger.debug("Creating client"); mqttClient = createMqttClient(broker, clientID, persistence); mqttClient.setCallback(this); } if (!mqttClient.isConnected()) { logger.debug("Connecting client"); mqttClient.connect(connOpts); mqttClient.subscribe(topicFilter, qos); } } catch (MqttException e) { logger.error("Connection to {} lost (or was never connected) and connection failed. Yielding processor", new Object[]{broker}, e); context.yield(); } }
@Before public void setUp() throws Exception { startServer(); m_client = new MqttClient("tcp://localhost:1883", "TestClient", s_dataStore); m_messagesCollector = new MessageCollector(); m_client.setCallback(m_messagesCollector); m_publisher = new MqttClient("tcp://localhost:1883", "Publisher", s_pubDataStore); }
@Before public void setUp() throws Exception { startServer(); m_client = new MqttClient("tcp://localhost:1883", "TestClient", s_dataStore); m_messagesCollector = new MessageCollector(); m_client.setCallback(m_messagesCollector); m_publisher = new MqttClient("tcp://localhost:1883", "Publisher", s_pubDataStore); }
@Before public void setUp() throws Exception { startServer(); m_subscriber = new MqttClient("tcp://localhost:1883", "Subscriber", s_dataStore); m_messageCollector = new MessageCollector(); m_subscriber.setCallback(m_messageCollector); m_publisher = new MqttClient("tcp://localhost:1883", "Publisher", s_pubDataStore); }
@Before public void setUp() throws Exception { startServer(); m_client = new MqttClient("tcp://localhost:1883", "TestClient", s_dataStore); m_messagesCollector = new MessageCollector(); m_client.setCallback(m_messagesCollector); m_publisher = new MqttClient("tcp://localhost:1883", "Publisher", s_pubDataStore); }
@Before public void setUp() throws Exception { startServer(); m_client = new Client("localhost"); m_willSubscriber = new MqttClient("tcp://localhost:1883", "Subscriber", s_dataStore); m_messageCollector = new MessageCollector(); m_willSubscriber.setCallback(m_messageCollector); }
/** * Connect subscribe to topic and publish on the same topic */ private IMqttClient subscribeAndPublish(String topic) throws Exception { IMqttClient client = new MqttClient("tcp://localhost:1883", "SubPub"); MessageCollector collector = new MessageCollector(); client.setCallback(collector); client.connect(); client.subscribe(topic, 1); client.publish(topic, "Hello world MQTT!!".getBytes(UTF_8), 0, false); MqttMessage msg = collector.waitMessage(1); assertEquals("Hello world MQTT!!", new String(msg.getPayload(), UTF_8)); return client; } }
@Before public void setUp() throws Exception { startServer(); m_subscriber = new MqttClient("tcp://localhost:1883", "Subscriber", new MemoryPersistence()); m_callback = new MessageCollector(); m_subscriber.setCallback(m_callback); m_subscriber.connect(); m_publisher = new MqttClient("tcp://localhost:1883", "Publisher", new MemoryPersistence()); m_publisher.connect(); }
@Test public void testClientDoesntRemainSubscribedAfterASubscriptionAndServerRestart() throws Exception { // subscribe to /topic m_subscriber.connect(); // subscribe /topic m_subscriber.subscribe("/topic", 0); // unsubscribe from /topic m_subscriber.unsubscribe("/topic"); m_subscriber.disconnect(); // shutdown the integration m_server.stopServer(); // restart the integration m_server.startServer(IntegrationUtils.prepareTestProperties()); // subscriber reconnects m_subscriber = new MqttClient("tcp://localhost:1883", "Subscriber", s_dataStore); m_subscriber.setCallback(m_messageCollector); m_subscriber.connect(); // publisher publishes on /topic m_publisher = new MqttClient("tcp://localhost:1883", "Publisher", s_pubDataStore); m_publisher.connect(); m_publisher.publish("/topic", "Hello world MQTT!!".getBytes(UTF_8), 1, false); // Expected // the subscriber doesn't get notified (it's fully unsubscribed) assertNull(m_messageCollector.waitMessage(1)); }
@Override public synchronized void connectionLost(Throwable cause) { if (isRunning()) { this.logger.error("Lost connection: " + cause.getMessage() + "; retrying..."); this.connected = false; if (this.client != null) { try { this.client.setCallback(null); this.client.close(); } catch (MqttException e) { // NOSONAR } } this.client = null; scheduleReconnect(); if (this.applicationEventPublisher != null) { this.applicationEventPublisher.publishEvent(new MqttConnectionFailedEvent(this, cause)); } } }
"If no 'url' provided, connectionOptions.getServerURIs() must not be null"); this.client = this.clientFactory.getClientInstance(getUrl(), getClientId()); this.client.setCallback(this); if (this.client instanceof MqttClient) { ((MqttClient) this.client).setTimeToWait(this.completionTimeout); this.client.disconnectForcibly(this.completionTimeout); try { this.client.setCallback(null); this.client.close();
this.client.setCallback(null);
@Override public void setCallback(MqttCallback callback) { this.mqttClient.setCallback(callback); }
callback.set(invocation.getArgument(0)); return null; }).given(client).setCallback(any(MqttCallback.class));
public static List<CompletableFuture<Void>> publish(IMqttClient client, String address, List<MqttMessage> messages) throws MqttException { List<CompletableFuture<Void>> futures = Stream.generate(CompletableFuture<Void>::new) .limit(messages.size()) .collect(Collectors.toList()); Iterator<CompletableFuture<Void>> resultItr = futures.iterator(); client.setCallback((MqttDeliveryCompleteCallback) token -> resultItr.next().complete(null)); for (MqttMessage message : messages) { client.publish(address, message); } return futures; }
@Override protected void configure() { requestStaticInjection(MqttCallbackAdapter.class); for (Entry<String, MqttClientDefinition> entry : mqttClientDefinitions.entrySet()) { MqttClientDefinition clientDefinition = entry.getValue(); String clientName = entry.getKey(); IMqttClient mqttClient = mqttClients.get(clientName); bind(IMqttClient.class).annotatedWith(Names.named(clientName)).toInstance(mqttClient); MqttCallbackAdapter callbackAdapter = mqttCallbackAdapters.get(clientName); mqttClient.setCallback(callbackAdapter); MqttPublisherDefinition publisherDefinition = clientDefinition.getPublisherDefinition(); if (publisherDefinition != null) { registerPublishHandler(callbackAdapter, publisherDefinition); } MqttListenerDefinition listenerDefinition = clientDefinition.getListenerDefinition(); if (listenerDefinition != null) { registerListener(callbackAdapter, clientDefinition); } } }
_client.setCallback(_listener); _client.subscribe(GatewayUtils.getMqttTopics(_config.getTopicsSubscribe())); _logger.info("MQTT Gateway[name:{}, URI:{}, NetworkType:{}] connected successfully..", _config.getName(),