Exemplo n.º 1
0
 /**
  * Publish the Device management response to IBm IoT Foundation
  *
  * <p>This method is used by the library to respond to each of the Device Management commands from
  * IBM IoT Foundation
  *
  * @param topic Topic where the response to be published
  * @param payload the Payload
  * @param qos The Quality Of Service
  * @throws MqttException
  */
 public void publish(DeviceTopic topic, JsonObject payload, int qos) throws MqttException {
   final String METHOD = "publish3";
   JsonObject jsonPubMsg = new JsonObject();
   jsonPubMsg.addProperty("topic", topic.getName());
   jsonPubMsg.add("qos", new JsonPrimitive(qos));
   jsonPubMsg.add("payload", payload);
   publishQueue.add(jsonPubMsg);
   LoggerUtility.log(
       Level.FINE,
       CLASS_NAME,
       METHOD,
       ": Queued Topic(" + topic + ") qos=" + qos + " payload (" + payload.toString() + ")");
 }
Exemplo n.º 2
0
 private void publish(JsonObject jsonPubMsg) throws MqttException, UnsupportedEncodingException {
   final String METHOD = "publish1";
   String topic = jsonPubMsg.get("topic").getAsString();
   int qos = jsonPubMsg.get("qos").getAsInt();
   JsonObject payload = jsonPubMsg.getAsJsonObject("payload");
   LoggerUtility.log(
       Level.FINE,
       CLASS_NAME,
       METHOD,
       ": Topic(" + topic + ") qos=" + qos + " payload (" + payload.toString() + ")");
   MqttMessage message = new MqttMessage();
   message.setPayload(payload.toString().getBytes("UTF-8"));
   message.setQos(qos);
   publish(DeviceTopic.get(topic), message);
 }
Exemplo n.º 3
0
  protected IMqttDeliveryToken publish(DeviceTopic topic, MqttMessage message)
      throws MqttException {
    final String METHOD = "publish";
    IMqttDeliveryToken token = null;
    LoggerUtility.fine(CLASS_NAME, METHOD, "Topic(" + topic + ")");
    while (true) {
      if (isConnected()) {
        try {
          if (this.mqttAsyncClient != null) {
            token = mqttAsyncClient.publish(topic.getName(), message);
          } else if (mqttClient != null) {
            mqttClient.publish(topic.getName(), message);
          }
        } catch (MqttException ex) {
          String payload = null;
          try {
            payload = new String(message.getPayload(), "UTF-8");
          } catch (UnsupportedEncodingException e1) {
          }
          if (this.mqttAsyncClient.isConnected() == false) {
            LoggerUtility.log(
                Level.WARNING,
                CLASS_NAME,
                METHOD,
                " Connection Lost retrying to publish MSG :"
                    + payload
                    + " on topic "
                    + topic
                    + " every 5 seconds");

            // 	wait for 5 seconds and retry
            try {
              Thread.sleep(5 * 1000);
              continue;
            } catch (InterruptedException e) {
            }
          } else {
            throw ex;
          }
        }

        if (isConnected() == false) {
          LoggerUtility.log(
              Level.WARNING,
              CLASS_NAME,
              METHOD,
              "MQTT got disconnected " + "after publish to Topic(" + topic + ")");
        }
        return token;
      } else {
        LoggerUtility.warn(
            CLASS_NAME,
            METHOD,
            ": Will not publish to topic(" + topic + ") because MQTT client is not connected.");
        try {
          Thread.sleep(5 * 1000);
          continue;
        } catch (InterruptedException e) {
        }
      }
    }
  }