/** * close iMqttClient quietly. * @param iMqttClient mqtt client interface. */ public static void closeMqttClientQuite(IMqttClient iMqttClient) { if (iMqttClient != null) { try { iMqttClient.disconnectForcibly(); } catch (MqttException e) { logger.info("Close Mqtt Client quite.", e); } } }
private IMqttClient getClient() { if (this.client == null) { this.createClient(); } else if (!this.client.isConnected()) { try { this.client.connect(this.getConnectOptions()); } catch (MqttException e) { LOG.error("Exception ({}) caught in getClient: {}", e.getClass().getName(), e.getMessage(), e); } } return this.client; }
private void connect(IMqttClient mqttClient) { MqttConnectOptions connOpts = this.getConnectOptions(); LOG.debug("Connecting to broker"); try { mqttClient.connect(connOpts); } catch (MqttException e) { LOG.error("Exception ({}) caught in connect: {}", e.getClass().getName(), e.getMessage(), e); } LOG.debug("Mqtt Client Connected"); }
/** * @param client * @throws MqttException */ public static void disconnectAndCloseClient(IMqttClient client) throws MqttException { if (client != null) { if (client.isConnected()) { client.disconnect(0); } client.close(); } }
/** * @param mqttClient * @param reportStream */ public MqttV3Receiver(IMqttClient mqttClient, PrintStream reportStream) { String methodName = Utility.getMethodName(); log.entering(className, methodName); this.reportStream = reportStream; connected = true; clientId = mqttClient.getClientId(); log.exiting(className, methodName); }
/** * Tests that a client can be constructed and that it can connect to and disconnect from the * service * @throws Exception */ @Test public void testConnect() throws Exception { final String methodName = Utility.getMethodName(); LoggingUtilities.banner(log, cclass, methodName); log.entering(className, methodName); IMqttClient mqttClient = null; try { mqttClient = clientFactory.createMqttClient(serverURI, methodName); log.info("Connecting...(serverURI:" + serverURI + ", ClientId:" + methodName); mqttClient.connect(); log.info("Disconnecting..."); mqttClient.disconnect(); log.info("Connecting...(serverURI:" + serverURI + ", ClientId:" + methodName); mqttClient.connect(); log.info("Disconnecting..."); mqttClient.disconnect(); } catch (Exception exception) { log.log(Level.SEVERE, "caught exception:", exception); Assert.fail("Failed:" + methodName + " exception=" + exception); } finally { if (mqttClient != null) { log.info("Close..."); mqttClient.close(); } } log.exiting(className, methodName); }
/** * @param context * @param eventService */ public ConnectionManager(BundleContext context, IEventService eventService) { this.eventService = eventService; this.registrations = new Registrations(); this.connections = new ConcurrentHashMap<Connection, IMqttClient>(); context.registerService(IConnectionManager.class.getName(), this, null); }
@Override public void connect(Connection connection) { try { IMqttClient client = doConnect(connection); connections.put(connection, client); } catch (Exception e) { throw new PahoException(e); } }
@Override public void disconnect(Connection connection) { try { IMqttClient client = connections.remove(connection); if (client != null && client.isConnected()) { client.disconnect(); } } catch (Exception e) { throw new PahoException(e); } }
/** * Stop */ public void stop() { registrations.unregister(); for (IMqttClient client : connections.values()) { try { if (client.isConnected()) { client.disconnect(); } } catch (MqttException e) { throw new PahoException(e); } } connections.clear(); }
/** * @throws Exception */ public void testConnect() throws Exception { IMqttClient client = null; try { String clientId = "testConnect"; client = new MqttClient(serverURI, clientId); System.out.println("Connecting...(serverURI:" + serverURI + ", ClientId:" + clientId); client.connect(); String clientId2 = client.getClientId(); System.out.println("clientId = " + clientId2); boolean isConnected = client.isConnected(); System.out.println("isConnected = " + isConnected); String id = client.getServerURI(); System.out.println("ServerURI = " + id); client.disconnect(); System.out.println("Disconnecting..."); client.connect(); System.out.println("Re-Connecting..."); client.disconnect(); System.out.println("Disconnecting..."); } catch (MqttException exception) { System.out.println("Unexpected exception: " + exception); } finally { if (client != null) { System.out.println("Close..."); client.close(); } } }
/** * @throws Exception */ public void testHAConnect() throws Exception { IMqttClient client = null; try { try { String clientId = "testHAConnect"; // If a proxy client does not support the URI list in the connect options, then this test should fail. // We ensure this happens by using a junk URI when creating the client. String junk = "tcp://junk:123"; client = new MqttClient(junk, clientId); // The first URI has a good protocol, but has a garbage hostname. // This ensures that a connect is attempted to the the second URI in the list String[] urls = new String[]{"tcp://junk", serverURI.toString()}; MqttConnectOptions options = new MqttConnectOptions(); options.setServerURIs(urls); System.out.println("Connecting..."); client.connect(options); System.out.println("Disconnecting..."); client.disconnect(); } catch (Exception e) { System.out.println(e.getClass().getName() + ": " + e.getMessage()); e.printStackTrace(); throw e; } } finally { if (client != null) { System.out.println("Close..."); client.close(); } } }
public IMqttClient getClient() { return client; }
/** * @throws Exception */ @Test public void testConnect() throws Exception { String methodName = Utility.getMethodName(); LoggingUtilities.banner(log, cclass, methodName); IMqttClient client = null; try { String clientId = methodName; client = clientFactory.createMqttClient(serverURI, clientId); log.info("Connecting...(serverURI:" + serverURI + ", ClientId:" + clientId); client.connect(); String clientId2 = client.getClientId(); log.info("clientId = " + clientId2); boolean isConnected = client.isConnected(); log.info("isConnected = " + isConnected); String id = client.getServerURI(); log.info("ServerURI = " + id); log.info("Disconnecting..."); client.disconnect(); log.info("Re-Connecting..."); client.connect(); log.info("Disconnecting..."); client.disconnect(); } catch (MqttException exception) { log.log(Level.SEVERE, "caught exception:", exception); Assert.fail("Unexpected exception: " + exception); } finally { if (client != null) { log.info("Close..."); client.close(); } } }
/** * @throws Exception */ @Test public void testHAConnect() throws Exception { String methodName = Utility.getMethodName(); LoggingUtilities.banner(log, cclass, methodName); // Some old clients do not support the new HA interface on the connect call if (clientFactory.isHighAvalabilitySupported() == false) { return; } IMqttClient client = null; try { try { String clientId = methodName; // If a client does not support the URI list in the connect options, then this test should fail. // We ensure this happens by using a junk URI when creating the client. URI junk = new URI("tcp://junk:123"); client = clientFactory.createMqttClient(junk, clientId); // The first URI has a good protocol, but has a garbage hostname. // This ensures that a connect is attempted to the the second URI in the list String[] urls = new String[]{"tcp://junk", serverURI.toString()}; MqttConnectOptions options = new MqttConnectOptions(); options.setServerURIs(urls); log.info("Connecting..."); client.connect(options); log.info("Disconnecting..."); client.disconnect(); } catch (Exception e) { // logger.info(e.getClass().getName() + ": " + e.getMessage()); e.printStackTrace(); throw e; } } finally { if (client != null) { log.info("Close..."); client.close(); } } }
/** * @throws Exception */ @Test public void testPubSub() throws Exception { String methodName = Utility.getMethodName(); LoggingUtilities.banner(log, cclass, methodName); IMqttClient client = null; try { String topicStr = "topic" + "_02"; String clientId = methodName; client = clientFactory.createMqttClient(serverURI, clientId); log.info("Assigning callback..."); MessageListener listener = new MessageListener(); client.setCallback(listener); log.info("Connecting...(serverURI:" + serverURI + ", ClientId:" + clientId); client.connect(); log.info("Subscribing to..." + topicStr); client.subscribe(topicStr); log.info("Publishing to..." + topicStr); MqttTopic topic = client.getTopic(topicStr); MqttMessage message = new MqttMessage("foo".getBytes()); topic.publish(message); log.info("Checking msg"); MqttMessage msg = listener.getNextMessage(); Assert.assertNotNull(msg); Assert.assertEquals("foo", msg.toString()); log.info("getTopic name"); String topicName = topic.getName(); log.info("topicName = " + topicName); Assert.assertEquals(topicName, topicStr); log.info("Disconnecting..."); client.disconnect(); } finally { if (client != null) { log.info("Close..."); client.close(); } } }
/** * Test connection using a remote host name for the local host. * @throws Exception */ @Test public void testRemoteConnect() throws Exception { final String methodName = Utility.getMethodName(); LoggingUtilities.banner(log, cclass, methodName); log.entering(className, methodName); IMqttClient mqttClient = null; try { mqttClient = clientFactory.createMqttClient(serverURI, methodName); log.info("Connecting...(serverURI:" + serverURI + ", ClientId:" + methodName); mqttClient.connect(); log.info("Disconnecting..."); mqttClient.disconnect(); MqttV3Receiver mqttV3Receiver = new MqttV3Receiver(mqttClient, LoggingUtilities.getPrintStream()); log.info("Assigning callback..."); mqttClient.setCallback(mqttV3Receiver); MqttConnectOptions mqttConnectOptions = new MqttConnectOptions(); mqttConnectOptions.setCleanSession(false); log.info("Connecting...(serverURI:" + serverURI + ", ClientId:" + methodName + ", cleanSession: false"); mqttClient.connect(mqttConnectOptions); String[] topicNames = new String[]{methodName + "/Topic"}; int[] topicQos = {0}; log.info("Subscribing to..." + topicNames[0]); mqttClient.subscribe(topicNames, topicQos); byte[] payload = ("Message payload " + className + "." + methodName).getBytes(); MqttTopic mqttTopic = mqttClient.getTopic(topicNames[0]); log.info("Publishing to..." + topicNames[0]); mqttTopic.publish(payload, 1, false); boolean ok = mqttV3Receiver.validateReceipt(topicNames[0], 0, payload); if (!ok) { Assert.fail("Receive failed"); } log.info("Disconnecting..."); mqttClient.disconnect(); } catch (Exception exception) { log.log(Level.SEVERE, "caught exception:", exception); Assert.fail("Failed:" + methodName + " exception=" + exception); } finally { if (mqttClient != null) { log.info("Close..."); mqttClient.close(); } } log.exiting(className, methodName); }
/** * @throws Exception */ public void testPubSub() throws Exception { IMqttClient client = null; try { String topicStr = "topic" + "_02"; String clientId = "testPubSub"; client = new MqttClient(serverURI, clientId); System.out.println("Assigning callback..."); MessageListener listener = new MessageListener(); client.setCallback(listener); System.out.println("Connecting...(serverURI:" + serverURI + ", ClientId:" + clientId); client.connect(); System.out.println("Subscribing to..." + topicStr); client.subscribe(topicStr); System.out.println("Publishing to..." + topicStr); MqttTopic topic = client.getTopic(topicStr); MqttMessage message = new MqttMessage("foo".getBytes()); topic.publish(message); System.out.println("Checking msg"); MqttMessage msg = listener.getNextMessage(); if (msg == null) throw new Exception("message should not be null"); if (!msg.toString().equals("foo")) throw new Exception("message should equal foo"); System.out.println("getTopic name"); String topicName = topic.getName(); System.out.println("topicName = " + topicName); if (!topicName.equals(topicStr)) throw new Exception ("topicName should equal topicStr"); System.out.println("Disconnecting..."); client.disconnect(); System.out.println("testPubSub completed successfully"); } finally { if (client != null) { System.out.println("Close..."); client.close(); } } }
/** * @param mqttClient * @param reportStream */ public MqttV3Receiver(IMqttClient mqttClient, PrintStream reportStream) { this.reportStream = reportStream; connected = true; clientId = mqttClient.getClientId(); }
/** * @param serverURI * @param clientId * @return MqttClient * @throws Exception */ public IMqttClient createMqttClient(URI serverURI, String clientId) throws Exception { return new MqttClientPaho(serverURI.toString(), clientId); }
/** * @param serverURI * @param clientId * @param persistence * @return MqttClient * @throws Exception */ public IMqttClient createMqttClient(URI serverURI, String clientId, MqttClientPersistence persistence) throws Exception { return new MqttClientPaho(serverURI.toString(), clientId, persistence); }