protected IMqttClient createMqttClient(String broker, String clientID, MemoryPersistence persistence) throws MqttException { return new MqttClient(broker, clientID, persistence); }
public MQTTPublisher(String address, String clientId, boolean cleanSession, int qos) throws MqttException { this.clientId = clientId; this.qos = qos; this.client = new MqttClient(address, clientId, persistence); MqttConnectOptions connOpts = new MqttConnectOptions(); connOpts.setCleanSession(cleanSession); this.client.connect(); }
MqttClient sampleClient = new MqttClient(broker, clientId, persistence); MqttConnectOptions connOpts = new MqttConnectOptions(); connOpts.setCleanSession(true);
@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); }
@Override public void onOpen(Emitter emitter) throws IOException { this.emitter = emitter; try { this.mqttClient = new MqttClient(DAO.getConfig("stream.mqtt.address", "tcp://127.0.0.1:1883"), "loklak_server_subscriber"); this.mqttClient.connect(); this.mqttClient.setCallback(this); this.mqttClient.subscribe(this.channel); } catch (MqttException e) { this.emitter.close(); } }
@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); }
MqttClient sampleClient = new MqttClient(broker, clinetId, persistence); MqttConnectOptions connectOptions = new MqttConnectOptions(); connectOptions.setCleanSession(true);
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()); } }
@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); }
@Test public void connectWithValidCredentials() throws Exception { LOG.info("*** connectWithCredentials ***"); m_client = new MqttClient("tcp://localhost:1883", "Publisher", s_pubDataStore); MqttConnectOptions options = new MqttConnectOptions(); options.setUserName("dbuser"); options.setPassword("password".toCharArray()); m_client.connect(options); assertTrue(true); }
@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(); }
@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; } }
@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); }
@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)); }
@Test public void checkSupportSSLForMultipleClient() throws Exception { LOG.info("*** checkSupportSSLForMultipleClient ***"); SSLSocketFactory ssf = configureSSLSocketFactory(); MqttConnectOptions options = new MqttConnectOptions(); options.setSocketFactory(ssf); m_client.connect(options); m_client.subscribe("/topic", 0); MqttClient secondClient = new MqttClient("ssl://localhost:8883", "secondTestClient", new MemoryPersistence()); MqttConnectOptions secondClientOptions = new MqttConnectOptions(); secondClientOptions.setSocketFactory(ssf); secondClient.connect(secondClientOptions); secondClient.publish("/topic", new MqttMessage("message".getBytes(UTF_8))); secondClient.disconnect(); m_client.disconnect(); }
MqttClient subscriberA = new MqttClient("tcp://localhost:1883", "SubscriberA", dsSubscriberA); MessageCollector cbSubscriberA = new MessageCollector(); subscriberA.setCallback(cbSubscriberA); MqttClient subscriberB = new MqttClient("tcp://localhost:1883", "SubscriberB", dsSubscriberB); MessageCollector cbSubscriberB = new MessageCollector(); subscriberB.setCallback(cbSubscriberB);