Search in sources :

Example 1 with KuraTooManyInflightMessagesException

use of org.eclipse.kura.KuraTooManyInflightMessagesException in project kura by eclipse.

the class MqttDataTransport method publish.

/*
     * (non-Javadoc)
     * 
     * @see org.eclipse.kura.data.DataPublisherService#publish(java.lang.String
     * , byte[], int, boolean)
     * 
     * DataConnectException this can be easily recovered connecting the service.
     * TooManyInflightMessagesException the caller SHOULD retry publishing the
     * message at a later time. RuntimeException (unchecked) all other
     * unrecoverable faults that are not recoverable by the caller.
     */
@Override
public DataTransportToken publish(String topic, byte[] payload, int qos, boolean retain) throws KuraTooManyInflightMessagesException, KuraException, KuraNotConnectedException {
    if (this.m_mqttClient == null || !this.m_mqttClient.isConnected()) {
        throw new KuraNotConnectedException("Not connected");
    }
    topic = replaceTopicVariables(topic);
    s_logger.info("Publishing message on topic: {} with QoS: {}", topic, qos);
    MqttMessage message = new MqttMessage();
    message.setPayload(payload);
    message.setQos(qos);
    message.setRetained(retain);
    Integer messageId = null;
    try {
        IMqttDeliveryToken token = this.m_mqttClient.publish(topic, message);
        // At present Paho ALWAYS allocates (gets and increments) internally
        // a message ID,
        // even for messages published with QoS == 0.
        // Of course, for QoS == 0 this "internal" message ID will not hit
        // the wire.
        // On top of that, messages published with QoS == 0 are confirmed
        // in the deliveryComplete callback.
        // Another implementation might behave differently
        // and only allocate a message ID for messages published with QoS >
        // 0.
        // We don't want to rely on this and only return and confirm IDs
        // of messages published with QoS > 0.
        s_logger.debug("Published message with ID: {}", token.getMessageId());
        if (qos > 0) {
            messageId = Integer.valueOf(token.getMessageId());
        }
    } catch (MqttPersistenceException e) {
        // This is probably an unrecoverable internal error
        s_logger.error("Cannot publish on topic: {}", topic, e);
        throw new IllegalStateException("Cannot publish on topic: " + topic, e);
    } catch (MqttException e) {
        if (e.getReasonCode() == MqttException.REASON_CODE_MAX_INFLIGHT) {
            s_logger.info("Too many inflight messages");
            throw new KuraTooManyInflightMessagesException(e, "Too many in-fligh messages");
        } else {
            s_logger.error("Cannot publish on topic: " + topic, e);
            throw KuraException.internalError(e, "Cannot publish on topic: " + topic);
        }
    }
    DataTransportToken token = null;
    if (messageId != null) {
        token = new DataTransportToken(messageId, this.m_sessionId);
    }
    return token;
}
Also used : MqttMessage(org.eclipse.paho.client.mqttv3.MqttMessage) DataTransportToken(org.eclipse.kura.data.DataTransportToken) MqttException(org.eclipse.paho.client.mqttv3.MqttException) MqttPersistenceException(org.eclipse.paho.client.mqttv3.MqttPersistenceException) KuraNotConnectedException(org.eclipse.kura.KuraNotConnectedException) KuraTooManyInflightMessagesException(org.eclipse.kura.KuraTooManyInflightMessagesException) IMqttDeliveryToken(org.eclipse.paho.client.mqttv3.IMqttDeliveryToken)

Aggregations

KuraNotConnectedException (org.eclipse.kura.KuraNotConnectedException)1 KuraTooManyInflightMessagesException (org.eclipse.kura.KuraTooManyInflightMessagesException)1 DataTransportToken (org.eclipse.kura.data.DataTransportToken)1 IMqttDeliveryToken (org.eclipse.paho.client.mqttv3.IMqttDeliveryToken)1 MqttException (org.eclipse.paho.client.mqttv3.MqttException)1 MqttMessage (org.eclipse.paho.client.mqttv3.MqttMessage)1 MqttPersistenceException (org.eclipse.paho.client.mqttv3.MqttPersistenceException)1