public CallbackConnection callbackConnection() { if( !isCleanSession() && ( getClientId()==null || getClientId().length==0 )) { throw new IllegalArgumentException("The client id MUST be configured when clean session is set to false"); } return new CallbackConnection(new MQTT(this)); } public FutureConnection futureConnection() {
/** * Initializes {@code connection}. * @throws Exception if an exception during connecting to connector occurs */ public static void startPublisher() throws Exception { MQTT client = new MQTT(); client.setTracer(new MqttLogger()); client.setHost("tcp://localhost:1883"); client.setClientId("MqttBrokerPublisher"); connection = client.blockingConnection(); Runtime.getRuntime().addShutdownHook(new Thread() { @Override public void run() { try { LOG.info("Shutting down MQTT client..."); connection.disconnect(); } catch (Exception e) { e.printStackTrace(); } } }); connection.connect(); }
@Before public void setUp() throws Exception { startServer(); m_mqtt = new MQTT(); m_mqtt.setHost("localhost", 1883); }
public static MQTT configureClient(MqttOptions options, String clientId, KeyStoreLoader keyStoreLoader) throws Exception { MQTT client = new MQTT(); URI uri = URI.create(options.getUrl());
String willTestamentMsg = "Bye bye"; MQTT mqtt = new MQTT(); mqtt.setHost("localhost", 1883); mqtt.setClientId("WillTestamentPublisher");
public CallbackConnection callbackConnection() { if( !isCleanSession() && ( getClientId()==null || getClientId().length==0 )) { throw new IllegalArgumentException("The client id MUST be configured when clean session is set to false"); } return new CallbackConnection(new MQTT(this)); } public FutureConnection futureConnection() {
private FutureConnection createConnection(String host,String clientId) { try { MQTT client = new MQTT(); client.setHost(host); client.setClientId(clientId); return client.futureConnection(); } catch (URISyntaxException e) { getLogger().error(e.getMessage()); return null; } } private FutureConnection createConnection(String host,String clientId,String user, String password) {
public void init(String host, int port) throws Exception { mqtt = new MQTT(); mqtt.setHost(host, port); mqtt.setUserName(uid); mqtt.setPassword(pwd); mqtt.setClientId(uid); }
private CallbackConnection createConnection(String host, String clientId, boolean durable) throws URISyntaxException { MQTT client = new MQTT(); client.setHost(host); client.setClientId(clientId); client.setCleanSession(!durable); return client.callbackConnection(); }
private FutureConnection createConnection(String host,String clientId,String user, String password) { try { MQTT client = new MQTT(); client.setHost(host); client.setUserName(user); client.setPassword(password); client.setClientId(clientId); return client.futureConnection(); } catch (URISyntaxException e) { getLogger().error(e.getMessage()); return null; } }
private CallbackConnection createConnection(String host, String clientId, boolean durable, String user, String password) throws URISyntaxException { MQTT client = new MQTT(); client.setHost(host); client.setClientId(clientId); client.setUserName(user); client.setPassword(password); client.setCleanSession(!durable); return client.callbackConnection(); }
try{ MqttMessage message2 = new MqttMessage(); MQTT mqtt_connect = new MQTT(); mqtt_connect.setHost(Host_Address, Integer.parseInt(port)); String topic = "/call/MQTT_Config"; mqtt_connect.setClientId("MQTT_Config"); mqtt_connect.setWillRetain(false); mqtt_connect.isWillRetain(); mqtt_connect.setWillTopic(topic); BlockingConnection m_publisher = mqtt_connect.blockingConnection(); m_publisher.connect(); } catch(Exception e){ add message for connection not established }
private static BlockingConnection retrieveMQTTConnection(String host) throws Exception { MQTT mqtt = new MQTT(); mqtt.setHost(host); BlockingConnection connection = mqtt.blockingConnection(); connection.connect(); return connection; } }
private static BlockingConnection retrieveMQTTConnection(String host) throws Exception { MQTT mqtt = new MQTT(); mqtt.setHost(host); BlockingConnection connection = mqtt.blockingConnection(); connection.connect(); return connection; }
@Override public Void call() throws Exception { try { this.mqtt = new MQTT(); mqtt.setHost("localhost", 1883); this.connection = mqtt.blockingConnection(); connection.connect(); System.out.println("Connected to: " + mqtt.getHost()); long start = System.currentTimeMillis(); for (int i = 0; i < messageCount; i++) { sendLocationOverMqtt(); } System.out .println("Sent " + messageCount + " events in " + (System.currentTimeMillis() - start) + "ms."); connection.disconnect(); return null; } catch (URISyntaxException e) { throw new RuntimeException(e); } catch (Exception e) { throw new RuntimeException(e); } }
@Override public void run() { this.running = true; MQTT mqtt = new MQTT(); try { mqtt.setHost(broker); BlockingConnection connection = mqtt.blockingConnection(); connection.connect(); Topic[] topics = {new Topic(topic, QoS.AT_LEAST_ONCE)}; byte[] qoses = connection.subscribe(topics); while(running && ((maxElementsToReceive == -1) || (this.messageCount <= maxElementsToReceive))) { Message message = connection.receive(); byte[] payload = message.getPayload(); consumer.onEvent(payload); message.ack(); this.messageCount++; } connection.disconnect(); } catch (Exception e) { e.printStackTrace(); } }
@Override public void run() { this.running = true; MQTT mqtt = new MQTT(); try { mqtt.setHost(broker); BlockingConnection connection = mqtt.blockingConnection(); connection.connect(); Topic[] topics = {new Topic(topic, QoS.AT_LEAST_ONCE)}; byte[] qoses = connection.subscribe(topics); while(running && ((maxElementsToReceive == -1) || (this.messageCount <= maxElementsToReceive))) { Message message = connection.receive(); byte[] payload = message.getPayload(); consumer.onEvent(payload); message.ack(); this.messageCount++; } connection.disconnect(); } catch (Exception e) { e.printStackTrace(); } }
private MQTT createMQTTSslConnection(String clientId, boolean clean) throws Exception { MQTT mqtt = new MQTT(); mqtt.setConnectAttemptsMax(1); mqtt.setReconnectAttemptsMax(0); mqtt.setTracer(createTracer()); mqtt.setHost("ssl://localhost:" + port); if (clientId != null) { mqtt.setClientId(clientId); } mqtt.setCleanSession(clean); SSLContext ctx = SSLContext.getInstance("TLS"); ctx.init(new KeyManager[0], new TrustManager[]{new DefaultTrustManager()}, new SecureRandom()); mqtt.setSslContext(ctx); return mqtt; }
private MQTT createMQTTTcpConnection(String clientId, boolean clean) throws Exception { MQTT mqtt = new MQTT(); mqtt.setConnectAttemptsMax(1); mqtt.setReconnectAttemptsMax(0); mqtt.setTracer(createTracer()); mqtt.setVersion("3.1.1"); if (clientId != null) { mqtt.setClientId(clientId); } mqtt.setCleanSession(clean); mqtt.setHost("localhost", port); return mqtt; }
private BlockingConnection retrieveMQTTConnection(String host, String truststorePath, String truststorePass, String keystorePath, String keystorePass) throws Exception { MQTT mqtt = new MQTT(); mqtt.setConnectAttemptsMax(1); mqtt.setReconnectAttemptsMax(0); mqtt.setHost(host); SSLContext sslContext = new SSLSupport() .setKeystorePath(keystorePath) .setKeystorePassword(keystorePass) .setTruststorePath(truststorePath) .setTruststorePassword(truststorePass) .createContext(); mqtt.setSslContext(sslContext); BlockingConnection connection = mqtt.blockingConnection(); connection.connect(); return connection; }