Package org.eclipse.paho.client.mqttv3

Examples of org.eclipse.paho.client.mqttv3.MqttClient


          url, clientId, tmpDir);
      client = new MqttClient(url, clientId, dataStore);
      client.setCallback(this);
    }

    MqttConnectOptions options = new MqttConnectOptions();

    if (!StringUtils.isBlank(user)) {
      options.setUserName(user);
    }
    if (!StringUtils.isBlank(password)) {
      options.setPassword(password.toCharArray());
    }   
    if (url.toLowerCase().contains("ssl")) {

      if (StringUtils.isNotBlank(System
          .getProperty("com.ibm.ssl.protocol"))) {

        // get all com.ibm.ssl properties from the system properties
        // and set them as the SSL properties to use.

        Properties sslProps = new Properties();
        addSystemProperty("com.ibm.ssl.protocol", sslProps);
        addSystemProperty("com.ibm.ssl.contextProvider", sslProps);
        addSystemProperty("com.ibm.ssl.keyStore", sslProps);
        addSystemProperty("com.ibm.ssl.keyStorePassword", sslProps);
        addSystemProperty("com.ibm.ssl.keyStoreType", sslProps);
        addSystemProperty("com.ibm.ssl.keyStoreProvider", sslProps);
        addSystemProperty("com.ibm.ssl.trustStore", sslProps);
        addSystemProperty("com.ibm.ssl.trustStorePassword", sslProps);
        addSystemProperty("com.ibm.ssl.trustStoreType", sslProps);
        addSystemProperty("com.ibm.ssl.trustStoreProvider", sslProps);
        addSystemProperty("com.ibm.ssl.enabledCipherSuites", sslProps);
        addSystemProperty("com.ibm.ssl.keyManager", sslProps);
        addSystemProperty("com.ibm.ssl.trustManager", sslProps);

        options.setSSLProperties(sslProps);

      } else {

        // use standard JSSE available in the runtime and
        // use TLSv1.2 which is the default for a secured mosquitto
        SSLContext sslContext = SSLContext.getInstance("TLSv1.2");
        sslContext.init(null,
            new TrustManager[] { getVeryTrustingTrustManager() },
            new java.security.SecureRandom());
        SSLSocketFactory socketFactory = sslContext.getSocketFactory();
        options.setSocketFactory(socketFactory);
      }
    }

    if (lastWill != null) {
      options.setWill(lastWill.getTopic(), lastWill.getPayload(),
          lastWill.getQos(), lastWill.isRetain());
    }

    options.setKeepAliveInterval(keepAliveInterval);
   
    client.connect(options);
  }
View Full Code Here


    private void publish(MqttClient client, String topicName, int qos, byte[] payload) throws MqttException {
      MqttTopic topic = client.getTopic(topicName);
       MqttMessage message = new MqttMessage(payload);
      message.setQos(qos);
      MqttDeliveryToken token = topic.publish(message);
      token.waitForCompletion();
    }
View Full Code Here

        message.setQos(qos);
        message.setRetained(retain);

        // publish message asynchronously
        MqttTopic mqttTopic = client.getTopic(topic);
        MqttDeliveryToken deliveryToken = mqttTopic.publish(message);

        logger.debug("Publishing message {} to topic '{}'",
            deliveryToken.getMessageId(), topic);
        if (!async) {
          // wait for publish confirmation
          deliveryToken.waitForCompletion(10000);
          if (!deliveryToken.isComplete()) {
            logger.error(
                "Did not receive completion message within timeout limit whilst publishing to topic '{}'",
                topic);
          }
        }
View Full Code Here

  public synchronized void connectionLost(Throwable t) {
 
    logger.error("MQTT connection to broker was lost", t);
 
    if (t instanceof MqttException) {
      MqttException e = (MqttException) t;
      logger.error("MQTT connection to '{}' was lost: {} : ReasonCode {} : Cause : {}",
          new Object[] { name, e.getMessage(), e.getReasonCode(), (e.getCause() == null ? "Unknown" : e.getCause().getMessage()) });
    } else {     
      logger.error("MQTT connection to '{}' was lost: {}", name, t.getMessage());
    }
   
    started = false;
View Full Code Here

  public void publish(String topic, JSONObject message) {
    //nothing to do if we have no MQTT event client configured
    if (mqttClient == null || !mqttClient.isConnected()) {
      return;
    }
    MqttMessage mqttMessage = new MqttMessage(message.toString().getBytes());
    try {
      mqttClient.publish(topic, mqttMessage);
    } catch (MqttException e) {
      logger.warn("Failure publishing event on topic: " + topic, ", message: " + message.toString(), e); //$NON-NLS-1$ //$NON-NLS-2$
    }
View Full Code Here

    DefaultMqttPahoClientFactory factory;
    MqttClient client = null;
    factory = new DefaultMqttPahoClientFactory();
    factory.setPassword("guest");
    factory.setUserName("foobar");
    MqttMessage mqttMessage = new MqttMessage();
    mqttMessage.setPayload(data.getBytes());
    try {
      client = factory.getClientInstance("tcp://" + host + ":" + port, "guest");
      client.connect();
      client.publish("xd.mqtt.test", mqttMessage);
      try {
View Full Code Here

        publish(client, topic, 0, payload);
        publish(client, topic, 1, payload);
        Thread.sleep(testDelay);

        Assert.assertEquals(2, receivedMessages.size());
        MqttMessage msg1 = receivedMessages.get(0);
        MqttMessage msg2 = receivedMessages.get(1);

        Assert.assertEquals(true, Arrays.equals(msg1.getPayload(), payload));
        Assert.assertEquals(0, msg1.getQos());

        Assert.assertEquals(true, Arrays.equals(msg2.getPayload(), payload));
        Assert.assertEquals(1, msg2.getQos());

        client.disconnect();
    }
View Full Code Here

        client.disconnect();
    }

    private void publish(MqttClient client, String topicName, int qos, byte[] payload) throws MqttException {
      MqttTopic topic = client.getTopic(topicName);
       MqttMessage message = new MqttMessage(payload);
      message.setQos(qos);
      MqttDeliveryToken token = topic.publish(message);
      token.waitForCompletion();
    }
View Full Code Here

              topic);
          return;
        }

        // Create and configure a message
        MqttMessage message = new MqttMessage(payload);
        message.setQos(qos);
        message.setRetained(retain);

        // publish message asynchronously
        MqttTopic mqttTopic = client.getTopic(topic);
        MqttDeliveryToken deliveryToken = mqttTopic.publish(message);
View Full Code Here

                sockets.add(sock);
                return sock;
            }
        };
        conOpt.setSocketFactory(testFactory);
        MqttTopic willTopic = client.getTopic(topic);
        conOpt.setWill(willTopic, payload, 0, false);
        conOpt.setCleanSession(false);
        client.connect(conOpt);

        Assert.assertEquals(1, sockets.size());
View Full Code Here

TOP

Related Classes of org.eclipse.paho.client.mqttv3.MqttClient

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.