/** * 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(); }
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(); }
public MQTTQueueMessage(String topic, MqttMessage message) { this.topic = topic; payload = message.getPayload(); qos = message.getQos(); retained = message.isRetained(); duplicate = message.isDuplicate(); }
@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); }
@Test public void checkClientAuthentication() throws Exception { LOG.info("*** checkClientAuthentication ***"); SSLSocketFactory ssf = configureSSLSocketFactory("signedclientkeystore.jks"); MqttConnectOptions options = new MqttConnectOptions(); options.setSocketFactory(ssf); m_client.connect(options); m_client.subscribe("/topic", 0); m_client.disconnect(); }
/** * 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; } }
public void publish(String channel, String message) { channel = channel.replaceAll("#", ""); DAO.log("Publishing " + message.substring(0, 10) + " to " + channel); MqttMessage msg = new MqttMessage(message.getBytes()); msg.setQos(this.qos); try { this.client.publish(channel, msg); } catch (MqttException e) { DAO.severe("Failed to publish message to channel " + channel + " due to MQTT exception", e); } catch (IllegalArgumentException e) { DAO.severe("Failed to publish message to channel " + channel + " because of invalid channel name", e); } catch (Exception e) { DAO.severe("Failed to publish message to channel " + channel + " due to unknown exception", e); } }
@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; } }
@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() ); } } }
@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); }
protected IMqttClient createMqttClient(String broker, String clientID, MemoryPersistence persistence) throws MqttException { return new MqttClient(broker, clientID, persistence); }
@Override public void onClose() { try { this.mqttClient.close(); this.mqttClient.disconnect(); } catch (MqttException e) { DAO.severe("Error terminating the stream client for API request", e); } }
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); } }
@Override public void messageArrived(String topic, MqttMessage message) throws Exception { this.emitter.data(message.toString()); }
protected boolean isConnected(){ return (mqttClient != null && mqttClient.isConnected()); }
@Test public void checkSupportSSL() throws Exception { LOG.info("*** checkSupportSSL ***"); SSLSocketFactory ssf = configureSSLSocketFactory(); MqttConnectOptions options = new MqttConnectOptions(); options.setSocketFactory(ssf); m_client.connect(options); m_client.subscribe("/topic", 0); m_client.disconnect(); }
@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 boolean apply(Void v) { return !client.isConnected() && !stopped; } })
@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); }