Client Configuration
A MQTT client can be created and configured by using a fluent builder pattern.
MqttClient.builder()
.identifier(UUID.randomUUID().toString())
.serverHost("broker.hivemq.com")
.serverPort(1883)
...;
General configuration
Method name | Description | Default value |
---|---|---|
identifier | The unique identifier of the MQTT client | The server creates an identifier for the client |
executorConfig | Configuration of used Threads | Default Netty event loop and Schedulers.comutation() for callbacks |
Transport configuration
Method name | Description | Default value |
---|---|---|
serverAddress | The address (host + port) of the MQTT server | See serverHost and serverPort |
serverHost | The host name or IP address of the MQTT server | localhost |
serverPort | The port of the MQTT server | - 1883 - 8883 (SSL/TLS) - 80 (WebSocket) - 443 (Secure WebSocket) |
sslConfig sslWithDefaultConfig | Secure transport configuration (SSL/TLS) | none |
webSocketConfig webSocketWithDefaultConfig | WebSocket transport configuration | none |
transportConfig | Transport configuration which combines: - server address (host + port) - secure transport configuration - WebSocket transport configuration | See - serverAddress /Host /Port - sslConfig - webSocketConfig |
Lifecycle configuration
Method name | Description | Default value |
---|---|---|
automaticReconnect automaticReconnectWithDefaultConfig | Automatic reconnect configuration | none |
addConnectedListener | Adds a listener that is notified when the client is connected | none |
addDisconnectedListener | Adds a listener that is notified when the client is disconnected | none |
You can not build an instance of MqttClient
directly, but a version specific Mqtt5Client
or Mqtt3Client
.
You can use the generic
MqttClientBuilder
to set the above properties and then switch to the builder for theMqtt5Client
by callinguseMqttVersion5()
:MqttClientBuilder clientBuilder = MqttClient.builder() .identifier(UUID.randomUUID().toString()) .serverHost("broker.hivemq.com") Mqtt5Client client = clientBuilder.useMqttVersion5().build();
Alternatively you can directly use a
Mqtt5ClientBuilder
:Mqtt5Client client = Mqtt5Client.builder() .identifier(UUID.randomUUID().toString()) .serverHost("broker.hivemq.com") .build();
The
Mqtt5ClientBuilder
has the following additional configuration methods:Method name Description Default value advancedConfig
Advanced configuration options for special requirements Sensible default values, you do not have to care about these if you do not require any of the advanced options You can use the generic
MqttClientBuilder
to set the above properties and then switch to the builder for theMqtt3Client
by callinguseMqttVersion3()
:MqttClientBuilder clientBuilder = MqttClient.builder() .identifier(UUID.randomUUID().toString()) .serverHost("broker.hivemq.com") Mqtt3Client client = clientBuilder.useMqttVersion3().build();
Alternatively you can directly use a
Mqtt3ClientBuilder
:Mqtt3Client client = Mqtt3Client.builder() .identifier(UUID.randomUUID().toString()) .serverHost("broker.hivemq.com") .build();