Package net.solarnetwork.central.support
Class BaseMqttConnectionObserver
java.lang.Object
net.solarnetwork.service.support.BasicIdentifiable
net.solarnetwork.central.support.BaseMqttConnectionObserver
- All Implemented Interfaces:
net.solarnetwork.common.mqtt.MqttConnectionObserver,net.solarnetwork.service.Identifiable
- Direct Known Subclasses:
MqttJsonPublisher
public abstract class BaseMqttConnectionObserver
extends net.solarnetwork.service.support.BasicIdentifiable
implements net.solarnetwork.common.mqtt.MqttConnectionObserver
Base implementation of
MqttConnectionObserver to help with connection
observer-based MQTT services.-
Field Summary
FieldsModifier and TypeFieldDescriptionstatic final net.solarnetwork.common.mqtt.MqttQosThepublishQosproperty default value.static final intTheconnectTimeoutSecondsproperty default value.static final net.solarnetwork.common.mqtt.MqttQosThesubscribeQosproperty default value.static final intThesubscribeTimeoutSecondsproperty default value.static final intThetransientErrorTriesproperty default value.protected final org.slf4j.LoggerA class-level logger.protected final AtomicReference<net.solarnetwork.common.mqtt.MqttConnection> Fields inherited from interface net.solarnetwork.service.Identifiable
GROUP_UID_PROPERTY, UID_PROPERTY -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionnet.solarnetwork.util.StatTrackerGet the statistics tracker.net.solarnetwork.common.mqtt.MqttQosGet the publish QoS.intGet a publish timeout, in seconds.net.solarnetwork.common.mqtt.MqttQosGet the subscribe QoS.intGet the subscribe timeout seconds.intGet the number of times to try operations that support retry, when transient exceptions occur.booleanTest if the MQTT connection is established.booleanGet the retained flag.voidonMqttServerConnectionEstablished(net.solarnetwork.common.mqtt.MqttConnection connection, boolean reconnected) Callback when the MQTT connection has been established.voidonMqttServerConnectionLost(net.solarnetwork.common.mqtt.MqttConnection connection, boolean willReconnect, Throwable cause) Callback when the MQTT connection has been lost.voidsetMqttStats(net.solarnetwork.util.StatTracker mqttStats) Set the statistics tracker.voidsetPublishQos(net.solarnetwork.common.mqtt.MqttQos publishQos) Set the publish QoS.voidsetPublishQosLevel(int level) Set the publish QoS as a level value.voidsetPublishTimeoutSeconds(int publishTimeoutSeconds) Set a publish timeout, in seconds.voidsetRetained(boolean retained) Set the retained flagvoidsetSubscribeQos(net.solarnetwork.common.mqtt.MqttQos subscribeQos) Set the subscribe QoS.voidsetSubscribeQosLevel(int level) Set the subscribe QoS as a level value.voidsetSubscribeTimeoutSeconds(int subscribeTimeoutSeconds) Set the subscribe timeout seconds.voidsetTransientErrorTries(int transientErrorTries) Set the number of times to try operations that support retry, when transient exceptions occur.toString()Methods inherited from class net.solarnetwork.service.support.BasicIdentifiable
basicIdentifiableMetadataSettings, basicIdentifiableSettings, basicIdentifiableSettings, basicIdentifiableSettings, doubleMetadataValue, getDisplayName, getGroupUid, getGroupUID, getMessageSource, getMetadata, getMetadataCount, getUid, getUID, integerMetadataValue, metadataValue, numberMetadataValue, saveMetadataValue, saveMetadataValue, setDisplayName, setGroupUid, setGroupUID, setMessageSource, setMetadata, setMetadataCount, setUid, setUID, smartMetadataValue
-
Field Details
-
DEFAULT_PUBLISH_TIMEOUT_SECONDS
public static final int DEFAULT_PUBLISH_TIMEOUT_SECONDSTheconnectTimeoutSecondsproperty default value.- See Also:
-
DEFAULT_SUBSCRIBE_TIMEOUT_SECONDS
public static final int DEFAULT_SUBSCRIBE_TIMEOUT_SECONDSThesubscribeTimeoutSecondsproperty default value.- See Also:
-
DEFAULT_TRANSIENT_ERROR_TRIES
public static final int DEFAULT_TRANSIENT_ERROR_TRIESThetransientErrorTriesproperty default value.- See Also:
-
DEFAULT_PUBLISH_QOS
public static final net.solarnetwork.common.mqtt.MqttQos DEFAULT_PUBLISH_QOSThepublishQosproperty default value. -
DEFAULT_SUBSCRIBE_QOS
public static final net.solarnetwork.common.mqtt.MqttQos DEFAULT_SUBSCRIBE_QOSThesubscribeQosproperty default value. -
log
protected final org.slf4j.Logger logA class-level logger. -
mqttConnection
-
-
Constructor Details
-
BaseMqttConnectionObserver
public BaseMqttConnectionObserver()
-
-
Method Details
-
onMqttServerConnectionEstablished
public void onMqttServerConnectionEstablished(net.solarnetwork.common.mqtt.MqttConnection connection, boolean reconnected) Callback when the MQTT connection has been established.Extending classes that override this can call this implementation to store the connection in the
hmqttConnectionreference.- Specified by:
onMqttServerConnectionEstablishedin interfacenet.solarnetwork.common.mqtt.MqttConnectionObserver
-
onMqttServerConnectionLost
public void onMqttServerConnectionLost(net.solarnetwork.common.mqtt.MqttConnection connection, boolean willReconnect, Throwable cause) Callback when the MQTT connection has been lost.Extending classes that override this can call this implementation to clear the connection from the
hmqttConnectionreference.- Specified by:
onMqttServerConnectionLostin interfacenet.solarnetwork.common.mqtt.MqttConnectionObserver
-
isConnected
public boolean isConnected()Test if the MQTT connection is established.- Returns:
- true if the MQTT connection is established
-
toString
-
getTransientErrorTries
public int getTransientErrorTries()Get the number of times to try operations that support retry, when transient exceptions occur.- Returns:
- the number of attempts to try operations that support retry;
defaults to
DEFAULT_TRANSIENT_ERROR_TRIES
-
setTransientErrorTries
public void setTransientErrorTries(int transientErrorTries) Set the number of times to try operations that support retry, when transient exceptions occur.- Parameters:
transientErrorTries- the number of times to attempt operations that support retry; must be greater than 0
-
getMqttStats
public net.solarnetwork.util.StatTracker getMqttStats()Get the statistics tracker.- Returns:
- the statistics tracker
-
setMqttStats
public void setMqttStats(net.solarnetwork.util.StatTracker mqttStats) Set the statistics tracker.- Parameters:
mqttStats- the statistics tracker to set
-
getSubscribeQos
public net.solarnetwork.common.mqtt.MqttQos getSubscribeQos()Get the subscribe QoS.- Returns:
- the QoS; defaults to
DEFAULT_SUBSCRIBE_QOS
-
setSubscribeQos
public void setSubscribeQos(net.solarnetwork.common.mqtt.MqttQos subscribeQos) Set the subscribe QoS.- Parameters:
subscribeQos- the subscribe QoS to set- Throws:
IllegalArgumentException- if the argument is null
-
setSubscribeQosLevel
public void setSubscribeQosLevel(int level) Set the subscribe QoS as a level value.- Parameters:
level- the level to set- Throws:
IllegalArgumentException- if the level is not supported
-
getSubscribeTimeoutSeconds
public int getSubscribeTimeoutSeconds()Get the subscribe timeout seconds.- Returns:
- the timeout seconds; defaults to
DEFAULT_SUBSCRIBE_TIMEOUT_SECONDS
-
setSubscribeTimeoutSeconds
public void setSubscribeTimeoutSeconds(int subscribeTimeoutSeconds) Set the subscribe timeout seconds.- Parameters:
subscribeTimeoutSeconds- the timeout seconds to set
-
isRetained
public boolean isRetained()Get the retained flag.- Returns:
- the retained; defaults to false
-
setRetained
public void setRetained(boolean retained) Set the retained flag- Parameters:
retained- the retained to set
-
getPublishQos
public net.solarnetwork.common.mqtt.MqttQos getPublishQos()Get the publish QoS.- Returns:
- the QoS; defaults to
DEFAULT_PUBLISH_QOS
-
setPublishQos
public void setPublishQos(net.solarnetwork.common.mqtt.MqttQos publishQos) Set the publish QoS.- Parameters:
publishQos- the QoS to set- Throws:
IllegalArgumentException- if the argument is null
-
setPublishQosLevel
public void setPublishQosLevel(int level) Set the publish QoS as a level value.- Parameters:
level- the QoS level to set- Throws:
IllegalArgumentException- if the level is not supported
-
getPublishTimeoutSeconds
public int getPublishTimeoutSeconds()Get a publish timeout, in seconds.- Returns:
- the timeout seconds; defaults to
DEFAULT_PUBLISH_TIMEOUT_SECONDS
-
setPublishTimeoutSeconds
public void setPublishTimeoutSeconds(int publishTimeoutSeconds) Set a publish timeout, in seconds.- Parameters:
publishTimeoutSeconds- the timeout to set
-