@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); } }
/** * 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; }
ParcelableMqttMessage(MqttMessage original) { super(original.getPayload()); setQos(original.getQos()); setRetained(original.isRetained()); setDuplicate(original.isDuplicate()); }