Class PlccomMqttClientBuilder
- java.lang.Object
-
- com.plccom.mqtt.client.PlccomMqttClientBuilder
-
public final class PlccomMqttClientBuilder extends Object
High-level builder for creatingPlccomMqttClientinstances.This builder offers short, readable setup calls for the most common MQTT client configurations: endpoint, TLS, WebSocket transport, authentication, Last Will, reconnect behavior and lifecycle callbacks. Applications that already create
PlccomMqttClientOptionsdirectly can continue to use that immutable options model for advanced setup.
-
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description PlccomMqttClientBuilderautomaticReconnect()Enables bounded automatic reconnect with the existing client defaults.PlccomMqttClientBuilderautoProtocol()Selects automatic MQTT protocol negotiation.PlccomMqttClientbuild()Builds a synchronous MQTT client.PlccomMqttClientOptionsbuildOptions()Builds validated immutable client options.PlccomMqttClientBuildercleanStart(boolean value)Sets whether the client requests a clean session.PlccomMqttClientBuilderconnectTimeoutMillis(long value)Sets the socket connect timeout.PlccomMqttClientBuilderdiagnosticLevel(PlccomMqttDiagnosticLevel value)Sets the minimum diagnostic level delivered to the diagnostic listener.PlccomMqttClientBuilderendpoint(String uri)Sets the endpoint from a URI such asmqtt://localhost:1883orws://localhost:8080/mqtt.PlccomMqttClientBuilderenhancedAuthentication(String method, byte[] initialData, PlccomMqttClientEnhancedAuthenticator authenticator)Configures MQTT 5 enhanced authentication for this client.PlccomMqttClientBuilderenhancedAuthentication(String method, PlccomMqttClientEnhancedAuthenticator authenticator)Configures MQTT 5 enhanced authentication without initial Authentication Data.PlccomMqttClientBuilderidentifier(String value)Sets the MQTT Client Identifier.PlccomMqttClientBuilderkeepAliveSeconds(int value)Sets the MQTT Keep Alive interval.PlccomMqttClientBuilderlastWill(PlccomMqttLastWill value)Sets the optional Last Will configuration directly.PlccomMqttClientBuilderlicense(String user, String serial)Sets PLCcom license information for the high-level MQTT client API.PlccomMqttClientBuilderlimits(PlccomMqttLimits value)Sets packet and queue safety limits.PlccomMqttClientBuilderlocalAddress(InetAddress value)Sets the local address the outgoing connection binds to.PlccomMqttClientBuildermqtt311()Selects MQTT 3.1.1.PlccomMqttClientBuildermqtt5()Selects MQTT 5.0.PlccomMqttClientBuildernoEnhancedAuthentication()Disables MQTT 5 enhanced authentication.PlccomMqttClientBuilderonConnected(MqttConnectedListener listener)Registers a callback for successful MQTT connections on clients created by this builder.PlccomMqttClientBuilderonDiagnostic(PlccomMqttDiagnosticListener listener)Registers a diagnostic listener for clients created by this builder.PlccomMqttClientBuilderonDisconnected(MqttDisconnectedListener listener)Registers a callback for MQTT disconnect events on clients created by this builder.PlccomMqttClientBuilderonReconnectFailed(MqttReconnectFailedListener listener)Registers a callback for failed bounded automatic reconnect on clients created by this builder.PlccomMqttClientBuilderoperationTimeoutMillis(long value)Sets the timeout for blocking MQTT operations.PlccomMqttClientBuilderreconnectDelaysMillis(long initialDelayMillis, long maxDelayMillis)Sets the reconnect backoff range.PlccomMqttClientBuilderserver(String serverHost, int serverPort)Sets the TCP server endpoint.PlccomMqttClientBuildersessionExpirySeconds(Long value)Sets the MQTT 5 Session Expiry Interval.PlccomMqttClientBuildersocketReadTimeoutMillis(int value)Sets the socket read timeout used while waiting for data from the broker.PlccomMqttClientBuildertls()Enables TLS using default TLS options.PlccomMqttClientBuildertls(PlccomMqttTlsOptions options)Enables TLS using the supplied TLS options.PlccomMqttClientBuildertopicAliasMaximum(int value)Enables or disables inbound MQTT 5 Topic Alias support for this client.PlccomMqttClientBuilderusernamePassword(String userName, byte[] passwordBytes)Sets the optional MQTT username and password bytes.PlccomMqttClientBuilderwebSocket()Switches the configured server endpoint to MQTT over WebSocket.PlccomMqttClientBuilderwebSocket(String webSocketPath)Switches the configured server endpoint to MQTT over WebSocket using the supplied HTTP request path.MqttClientWillBuilderwill(String topic, byte[] payload)Starts high-level Last Will configuration for a binary payload.MqttClientWillBuilderwill(String topic, String payload)Starts high-level Last Will configuration for a UTF-8 text payload.
-
-
-
Method Detail
-
license
public PlccomMqttClientBuilder license(String user, String serial)
Sets PLCcom license information for the high-level MQTT client API.The license is checked when
build()creates the client and again when high-level convenience operations are used.Evaluation mode: pass two empty strings and the library runs for 15 minutes during a debug session, which is enough to open a connection and read values. For uninterrupted work generate a free trial license (14 days): https://www.indi-an.com/en/plccom/mqtt/mqtt-download/
- Parameters:
user- user name from the PLCcom license informationserial- serial code from the PLCcom license information- Returns:
- this builder
- Throws:
NullPointerException- ifuserorserialisnull
-
identifier
public PlccomMqttClientBuilder identifier(String value)
Sets the MQTT Client Identifier.- Parameters:
value- client identifier sent in CONNECT- Returns:
- this builder
-
server
public PlccomMqttClientBuilder server(String serverHost, int serverPort)
Sets the TCP server endpoint.- Parameters:
serverHost- host name or IP addressserverPort- TCP port- Returns:
- this builder
- Throws:
NullPointerException- ifserverHostisnull
-
endpoint
public PlccomMqttClientBuilder endpoint(String uri)
Sets the endpoint from a URI such asmqtt://localhost:1883orws://localhost:8080/mqtt.- Parameters:
uri- endpoint URI- Returns:
- this builder
- Throws:
NullPointerException- ifuriisnullIllegalArgumentException- if the endpoint URI is invalid
-
webSocket
public PlccomMqttClientBuilder webSocket()
Switches the configured server endpoint to MQTT over WebSocket.This is a high-level comfort method for the common case where the application already configured the host and port with
server(String, int)and only wants to select the WebSocket transport. The default WebSocket path is/mqtt. Calltls()afterwards, or before this method, to usewss://instead ofws://.- Returns:
- this builder
-
webSocket
public PlccomMqttClientBuilder webSocket(String webSocketPath)
Switches the configured server endpoint to MQTT over WebSocket using the supplied HTTP request path.The method keeps the currently configured host and port. If TLS is already enabled the resulting endpoint uses
wss://; otherwise it usesws://. The path must start with/because it is sent as the HTTP request target during the WebSocket upgrade.- Parameters:
webSocketPath- HTTP path used for the WebSocket upgrade, for example/mqtt- Returns:
- this builder
- Throws:
NullPointerException- ifwebSocketPathisnullIllegalArgumentException- ifwebSocketPathdoes not start with/
-
mqtt311
public PlccomMqttClientBuilder mqtt311()
Selects MQTT 3.1.1.- Returns:
- this builder
-
mqtt5
public PlccomMqttClientBuilder mqtt5()
Selects MQTT 5.0.- Returns:
- this builder
-
autoProtocol
public PlccomMqttClientBuilder autoProtocol()
Selects automatic MQTT protocol negotiation.- Returns:
- this builder
-
usernamePassword
public PlccomMqttClientBuilder usernamePassword(String userName, byte[] passwordBytes)
Sets the optional MQTT username and password bytes.- Parameters:
userName- username, ornullto omit itpasswordBytes- password bytes, ornullto omit the password- Returns:
- this builder
-
enhancedAuthentication
public PlccomMqttClientBuilder enhancedAuthentication(String method, byte[] initialData, PlccomMqttClientEnhancedAuthenticator authenticator)
Configures MQTT 5 enhanced authentication for this client.The method and optional initial data are sent in CONNECT. The authenticator answers broker AUTH challenges synchronously before CONNACK is accepted. Slow implementations can increase connect and reconnect latency, so long-running checks should be used deliberately.
- Parameters:
method- MQTT 5 Authentication Method to send in CONNECTinitialData- MQTT 5 Authentication Data to send in CONNECT, ornullto omit itauthenticator- handler for broker AUTH challenges, ornullwhen no challenge response is needed- Returns:
- this builder
- Throws:
NullPointerException- ifmethodisnull
-
enhancedAuthentication
public PlccomMqttClientBuilder enhancedAuthentication(String method, PlccomMqttClientEnhancedAuthenticator authenticator)
Configures MQTT 5 enhanced authentication without initial Authentication Data.- Parameters:
method- MQTT 5 Authentication Method to send in CONNECTauthenticator- handler for broker AUTH challenges, ornullwhen no challenge response is needed- Returns:
- this builder
- Throws:
NullPointerException- ifmethodisnull
-
noEnhancedAuthentication
public PlccomMqttClientBuilder noEnhancedAuthentication()
Disables MQTT 5 enhanced authentication.- Returns:
- this builder
-
will
public MqttClientWillBuilder will(String topic, String payload)
Starts high-level Last Will configuration for a UTF-8 text payload.The returned builder is completed with
MqttClientWillBuilder.done(). Untildone()is called, the Will is not stored in this client builder.- Parameters:
topic- MQTT Topic Name used by the broker when it publishes the Willpayload- Last Will payload text, ornullfor an empty payload- Returns:
- Last Will builder linked to this client builder
- Throws:
NullPointerException- iftopicisnullwhen the Will builder is completedIllegalArgumentException- iftopicis empty when the Will builder is completed
-
will
public MqttClientWillBuilder will(String topic, byte[] payload)
Starts high-level Last Will configuration for a binary payload.The payload bytes are defensively copied by the underlying immutable Last Will value object when the nested builder is completed.
- Parameters:
topic- MQTT Topic Name used by the broker when it publishes the Willpayload- Last Will payload bytes, ornullfor an empty payload- Returns:
- Last Will builder linked to this client builder
- Throws:
NullPointerException- iftopicisnullwhen the Will builder is completedIllegalArgumentException- iftopicis empty when the Will builder is completed
-
lastWill
public PlccomMqttClientBuilder lastWill(PlccomMqttLastWill value)
Sets the optional Last Will configuration directly.This method keeps the immutable
PlccomMqttLastWillvalue object available for callers that already have explicit Will configuration. Usewill(String, String)orwill(String, byte[])when a fluent high-level setup reads better.- Parameters:
value- Last Will configuration, ornullto clear the Will- Returns:
- this builder
-
tls
public PlccomMqttClientBuilder tls()
Enables TLS using default TLS options.For TCP endpoints this switches
mqtt://tomqtts://. For WebSocket endpoints it switchesws://towss://and preserves the configured WebSocket path.The broker certificate is then checked against the PLCcom PKI store in the
pkifolder of the working directory, which is created at the first TLS connection: an unknown broker certificate is rejected and written to its rejected folder. To trust a broker with an own certificate authority, place that authority intopki/issuer/certs; for a public certificate authority usetls(PlccomMqttTlsOptions)with a TrustStore, which also checks the host name.- Returns:
- this builder
-
tls
public PlccomMqttClientBuilder tls(PlccomMqttTlsOptions options)
Enables TLS using the supplied TLS options.For TCP endpoints this switches
mqtt://tomqtts://. For WebSocket endpoints it switchesws://towss://and preserves the configured WebSocket path.- Parameters:
options- TLS options- Returns:
- this builder
- Throws:
NullPointerException- ifoptionsisnull
-
automaticReconnect
public PlccomMqttClientBuilder automaticReconnect()
Enables bounded automatic reconnect with the existing client defaults.- Returns:
- this builder
-
onConnected
public PlccomMqttClientBuilder onConnected(MqttConnectedListener listener)
Registers a callback for successful MQTT connections on clients created by this builder.The callback is registered on the built client before it connects. The client catches runtime exceptions thrown by application callbacks so they do not close the MQTT connection.
- Parameters:
listener- callback invoked after CONNACK has been accepted- Returns:
- this builder
- Throws:
NullPointerException- iflistenerisnull
-
onDisconnected
public PlccomMqttClientBuilder onDisconnected(MqttDisconnectedListener listener)
Registers a callback for MQTT disconnect events on clients created by this builder.- Parameters:
listener- callback invoked when the connection closes- Returns:
- this builder
- Throws:
NullPointerException- iflistenerisnull
-
onReconnectFailed
public PlccomMqttClientBuilder onReconnectFailed(MqttReconnectFailedListener listener)
Registers a callback for failed bounded automatic reconnect on clients created by this builder.- Parameters:
listener- callback invoked when automatic reconnect gives up- Returns:
- this builder
- Throws:
NullPointerException- iflistenerisnull
-
onDiagnostic
public PlccomMqttClientBuilder onDiagnostic(PlccomMqttDiagnosticListener listener)
Registers a diagnostic listener for clients created by this builder.Diagnostics report background failures and protected callback exceptions without forcing the SDK to write to a fixed log destination. Use this hook to forward events to an application logger, monitoring system or user-interface notification layer.
- Parameters:
listener- diagnostic listener, ornullto ignore diagnostic events- Returns:
- this builder
-
diagnosticLevel
public PlccomMqttClientBuilder diagnosticLevel(PlccomMqttDiagnosticLevel value)
Sets the minimum diagnostic level delivered to the diagnostic listener.Use
PlccomMqttDiagnosticLevel.DEBUGwhile investigating a specific issue,PlccomMqttDiagnosticLevel.WARNINGorPlccomMqttDiagnosticLevel.ERRORfor quieter production logging, andPlccomMqttDiagnosticLevel.OFFwhen diagnostic callbacks should be suppressed completely.- Parameters:
value- minimum diagnostic level to deliver- Returns:
- this builder
- Throws:
NullPointerException- ifvalueisnull
-
cleanStart
public PlccomMqttClientBuilder cleanStart(boolean value)
Sets whether the client requests a clean session.- Parameters:
value-truefor clean session semantics- Returns:
- this builder
-
sessionExpirySeconds
public PlccomMqttClientBuilder sessionExpirySeconds(Long value)
Sets the MQTT 5 Session Expiry Interval.- Parameters:
value- session expiry in seconds, ornullto omit it- Returns:
- this builder
-
keepAliveSeconds
public PlccomMqttClientBuilder keepAliveSeconds(int value)
Sets the MQTT Keep Alive interval.- Parameters:
value- keep-alive interval in seconds- Returns:
- this builder
-
topicAliasMaximum
public PlccomMqttClientBuilder topicAliasMaximum(int value)
Enables or disables inbound MQTT 5 Topic Alias support for this client.The value is advertised to the broker as Topic Alias Maximum in the MQTT 5 CONNECT properties. Leave the value at
0when the client should reject broker-to-client Topic Aliases. Set a positive maximum when the client should accept spec-compliant aliases from any broker on this connection.- Parameters:
value- maximum accepted Topic Alias value, or0to disable inbound Topic Aliases- Returns:
- this builder
- Throws:
IllegalArgumentException- ifvalueis outside0..65535duringbuildOptions()
-
limits
public PlccomMqttClientBuilder limits(PlccomMqttLimits value)
Sets packet and queue safety limits.- Parameters:
value- limits- Returns:
- this builder
- Throws:
NullPointerException- ifvalueisnull
-
connectTimeoutMillis
public PlccomMqttClientBuilder connectTimeoutMillis(long value)
Sets the socket connect timeout.- Parameters:
value- timeout in milliseconds- Returns:
- this builder
-
operationTimeoutMillis
public PlccomMqttClientBuilder operationTimeoutMillis(long value)
Sets the timeout for blocking MQTT operations.- Parameters:
value- timeout in milliseconds- Returns:
- this builder
-
socketReadTimeoutMillis
public PlccomMqttClientBuilder socketReadTimeoutMillis(int value)
Sets the socket read timeout used while waiting for data from the broker.- Parameters:
value- timeout in milliseconds- Returns:
- this builder
-
localAddress
public PlccomMqttClientBuilder localAddress(InetAddress value)
Sets the local address the outgoing connection binds to.Useful on machines with several network interfaces when the connection has to leave through a specific one, for example a dedicated control network.
- Parameters:
value- local bind address, ornullto let the operating system choose- Returns:
- this builder
-
reconnectDelaysMillis
public PlccomMqttClientBuilder reconnectDelaysMillis(long initialDelayMillis, long maxDelayMillis)
Sets the reconnect backoff range.- Parameters:
initialDelayMillis- initial reconnect delay in millisecondsmaxDelayMillis- maximum reconnect delay in milliseconds- Returns:
- this builder
-
buildOptions
public PlccomMqttClientOptions buildOptions()
Builds validated immutable client options.- Returns:
- immutable client options
- Throws:
NullPointerException- if a required value isnullIllegalArgumentException- if a configured value is invalid
-
build
public PlccomMqttClient build()
Builds a synchronous MQTT client.License information is required. Building without any license information fails; there is no implicit unlicensed mode. To use evaluation mode, pass two empty strings to
license(String, String)or toPlccomMqttClientFactory.setLicenseInformation(String, String).- Returns:
- client instance
- Throws:
NullPointerException- if a required value isnullIllegalArgumentException- if a configured value is invalidPlccomMqttLicenseException- if no license information was supplied at all, or if the supplied information is invalid or expired
-
-