コード例 #1
0
  @Override
  public void messageArrived(String topic, MqttMessage message) throws Exception {
    LOG.log(
        Level.INFO,
        "Message arrived for topic: {0}, QoS: {1}, message: {2}",
        new Object[] {topic, message.getQos(), new String(message.getPayload())});

    for (String tPattern : listeners.keySet()) {
      if (matchesTopic(tPattern, topic)) {
        // Notify the listener of the incoming message
        listeners.get(tPattern).notify(message);
      }
    }
  }
コード例 #2
0
 /** @see MqttCallback#messageArrived(String, MqttMessage) */
 public void messageArrived(String topic, MqttMessage message) throws MqttException {
   // Called when a message arrives from the server that matches any
   // subscription made by the client
   String time = new Timestamp(System.currentTimeMillis()).toString();
   System.out.println(
       "Time:\t"
           + time
           + "  Topic:\t"
           + topic
           + "  Message:\t"
           + new String(message.getPayload())
           + "  QoS:\t"
           + message.getQos());
 }
コード例 #3
0
  @Override
  public void messageArrived(String topic, MqttMessage mqttMessage) throws Exception {
    s_logger.info(
        "mqtt message arrived. message "
            + topic
            + ":'"
            + new String(mqttMessage.getPayload())
            + "' qos="
            + mqttMessage.getQos()
            + " retained="
            + mqttMessage.isRetained());

    final String message = new String(mqttMessage.getPayload());
    final int qos = mqttMessage.getQos();
    final boolean retained = mqttMessage.isRetained();
    final Date received = new Date();

    if (m_session != null) {
      // prevent blocking of modal thread
      new ClientAsyncJob("mqtt message arrived (async wrapper)", m_session, true) {
        @Override
        protected void runVoid(IProgressMonitor monitor) throws Exception {
          // implement ui changes in a sync job
          new ClientSyncJob("mqtt message arrived", m_session) {
            @Override
            protected void runVoid(IProgressMonitor monitor1) throws Throwable {
              MessageHandlingService service = SERVICES.getService(MessageHandlingService.class);
              service.handleMessage(topic, message, qos, retained, received);
            }
          }.schedule();
        }
      }.schedule();
    } else {
      s_logger.error("client session is null");
    }
  }
コード例 #4
0
 @Override
 public Message<?> toMessage(String topic, MqttMessage mqttMessage) {
   try {
     AbstractIntegrationMessageBuilder<Object> messageBuilder =
         getMessageBuilderFactory()
             .withPayload(mqttBytesToPayload(mqttMessage))
             .setHeader(MqttHeaders.QOS, mqttMessage.getQos())
             .setHeader(MqttHeaders.DUPLICATE, mqttMessage.isDuplicate())
             .setHeader(MqttHeaders.RETAINED, mqttMessage.isRetained());
     if (topic != null) {
       messageBuilder.setHeader(MqttHeaders.TOPIC, topic);
     }
     return messageBuilder.build();
   } catch (Exception e) {
     throw new MessageConversionException("failed to convert object to Message", e);
   }
 }
コード例 #5
0
  /**
   * Store an MQTT message
   *
   * @param clientHandle identifier for the client storing the message
   * @param topic The topic on which the message was published
   * @param message the arrived MQTT message
   * @return an identifier for the message, so that it can be removed when appropriate
   */
  @Override
  public String storeArrived(String clientHandle, String topic, MqttMessage message) {

    db = mqttDb.getWritableDatabase();

    traceHandler.traceDebug(
        TAG, "storeArrived{" + clientHandle + "}, {" + message.toString() + "}");

    byte[] payload = message.getPayload();
    int qos = message.getQos();
    boolean retained = message.isRetained();
    boolean duplicate = message.isDuplicate();

    ContentValues values = new ContentValues();
    String id = java.util.UUID.randomUUID().toString();
    values.put(MqttServiceConstants.MESSAGE_ID, id);
    values.put(MqttServiceConstants.CLIENT_HANDLE, clientHandle);
    values.put(MqttServiceConstants.DESTINATION_NAME, topic);
    values.put(MqttServiceConstants.PAYLOAD, payload);
    values.put(MqttServiceConstants.QOS, qos);
    values.put(MqttServiceConstants.RETAINED, retained);
    values.put(MqttServiceConstants.DUPLICATE, duplicate);
    values.put(MTIMESTAMP, System.currentTimeMillis());
    try {
      db.insertOrThrow(ARRIVED_MESSAGE_TABLE_NAME, null, values);
    } catch (SQLException e) {
      traceHandler.traceException(TAG, "onUpgrade", e);
      throw e;
    }
    int count = getArrivedRowCount(clientHandle);
    traceHandler.traceDebug(
        TAG,
        "storeArrived: inserted message with id of {"
            + id
            + "} - Number of messages in database for this clientHandle = "
            + count);
    return id;
  }
コード例 #6
0
 ParcelableMqttMessage(MqttMessage original) {
   super(original.getPayload());
   setQos(original.getQos());
   setRetained(original.isRetained());
   setDuplicate(original.isDuplicate());
 }