Class MqttClient
- java.lang.Object
-
- de.iip_ecosphere.platform.transport.spring.binder.mqttv5.MqttClient
-
@Component public class MqttClient extends java.lang.ObjectA MQTT client for a single binder instance. Typically, different binders subscribe to different topics. Partially public for testing. Initial implementation, not optimized.- Author:
- Holger Eichelberger, SSE
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description static interfaceMqttClient.ArrivedCallbackCalled when a message for a topic arrives.private static classMqttClient.CallbackThe internal MQTT reception callback.
-
Field Summary
Fields Modifier and Type Field Description private MqttClient.Callbackcallbackprivate org.eclipse.paho.mqttv5.client.MqttAsyncClientclientprivate MqttConfigurationconfigurationprivate static MqttClientlastInstanceprivate static org.slf4j.LoggerLOGGERprivate de.iip_ecosphere.platform.transport.connectors.basics.MqttQoSqos
-
Constructor Summary
Constructors Constructor Description MqttClient()Creates and registers an instance.
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description voidcreateClient(MqttConfiguration config)Creates the client based on a given MQTT client configuration.MqttConfigurationgetConfiguration()Returns the actual configuration.static MqttClientgetLastInstance()Returns the last instance created for this class.(package private) voidsend(java.lang.String topic, byte[] payload)Sendspayloadtotopic.voidstopClient()Stops the client.(package private) booleansubscribeTo(java.lang.String topic, MqttClient.ArrivedCallback arrivedCallback)(package private) booleanunsubscribeFrom(java.lang.String topic)(package private) voidwaitForCompletion(org.eclipse.paho.mqttv5.client.IMqttToken token)Waits for completion until thetokenis processed.
-
-
-
Field Detail
-
LOGGER
private static final org.slf4j.Logger LOGGER
-
lastInstance
private static MqttClient lastInstance
-
client
private org.eclipse.paho.mqttv5.client.MqttAsyncClient client
-
configuration
private MqttConfiguration configuration
-
callback
private MqttClient.Callback callback
-
qos
private de.iip_ecosphere.platform.transport.connectors.basics.MqttQoS qos
-
-
Method Detail
-
getLastInstance
public static MqttClient getLastInstance()
Returns the last instance created for this class. [testing]- Returns:
- the last instance
-
getConfiguration
public MqttConfiguration getConfiguration()
Returns the actual configuration. [for testing]- Returns:
- the configuration, may be null
-
createClient
public void createClient(MqttConfiguration config)
Creates the client based on a given MQTT client configuration.- Parameters:
config- the MQTT configuration to take the connection information from
-
stopClient
public void stopClient()
Stops the client.
-
subscribeTo
boolean subscribeTo(java.lang.String topic, MqttClient.ArrivedCallback arrivedCallback)- Parameters:
topic- the topic to unsubscribe fromarrivedCallback- the callback to be called when a message arrived- Returns:
trueif done/successful,falseelse
-
unsubscribeFrom
boolean unsubscribeFrom(java.lang.String topic)
- Parameters:
topic- the topic to unsubscribe from- Returns:
trueif done/successful,falseelse
-
send
void send(java.lang.String topic, byte[] payload)Sendspayloadtotopic.- Parameters:
topic- the topic to send topayload- the payload to send
-
waitForCompletion
void waitForCompletion(org.eclipse.paho.mqttv5.client.IMqttToken token) throws org.eclipse.paho.mqttv5.common.MqttExceptionWaits for completion until thetokenis processed.- Parameters:
token- the token- Throws:
org.eclipse.paho.mqttv5.common.MqttException- in case that processing of the token fails
-
-