Class PlccomMqttClientBuilder


  • public final class PlccomMqttClientBuilder
    extends Object
    High-level builder for creating PlccomMqttClient instances.

    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 PlccomMqttClientOptions directly can continue to use that immutable options model for advanced setup.

    • 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 information
        serial - serial code from the PLCcom license information
        Returns:
        this builder
        Throws:
        NullPointerException - if user or serial is null
      • 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 address
        serverPort - TCP port
        Returns:
        this builder
        Throws:
        NullPointerException - if serverHost is null
      • 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. Call tls() afterwards, or before this method, to use wss:// instead of ws://.

        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 uses ws://. 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 - if webSocketPath is null
        IllegalArgumentException - if webSocketPath does not start with /
      • 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, or null to omit it
        passwordBytes - password bytes, or null to 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 CONNECT
        initialData - MQTT 5 Authentication Data to send in CONNECT, or null to omit it
        authenticator - handler for broker AUTH challenges, or null when no challenge response is needed
        Returns:
        this builder
        Throws:
        NullPointerException - if method is null
      • 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 CONNECT
        authenticator - handler for broker AUTH challenges, or null when no challenge response is needed
        Returns:
        this builder
        Throws:
        NullPointerException - if method is null
      • 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(). Until done() is called, the Will is not stored in this client builder.

        Parameters:
        topic - MQTT Topic Name used by the broker when it publishes the Will
        payload - Last Will payload text, or null for an empty payload
        Returns:
        Last Will builder linked to this client builder
        Throws:
        NullPointerException - if topic is null when the Will builder is completed
        IllegalArgumentException - if topic is 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 Will
        payload - Last Will payload bytes, or null for an empty payload
        Returns:
        Last Will builder linked to this client builder
        Throws:
        NullPointerException - if topic is null when the Will builder is completed
        IllegalArgumentException - if topic is empty when the Will builder is completed
      • tls

        public PlccomMqttClientBuilder tls()
        Enables TLS using default TLS options.

        For TCP endpoints this switches mqtt:// to mqtts://. For WebSocket endpoints it switches ws:// to wss:// and preserves the configured WebSocket path.

        The broker certificate is then checked against the PLCcom PKI store in the pki folder 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 into pki/issuer/certs; for a public certificate authority use tls(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:// to mqtts://. For WebSocket endpoints it switches ws:// to wss:// and preserves the configured WebSocket path.

        Parameters:
        options - TLS options
        Returns:
        this builder
        Throws:
        NullPointerException - if options is null
      • 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 - if listener is null
      • 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 - if listener is null
      • 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, or null to ignore diagnostic events
        Returns:
        this builder
      • cleanStart

        public PlccomMqttClientBuilder cleanStart​(boolean value)
        Sets whether the client requests a clean session.
        Parameters:
        value - true for 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, or null to 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 0 when 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, or 0 to disable inbound Topic Aliases
        Returns:
        this builder
        Throws:
        IllegalArgumentException - if value is outside 0..65535 during buildOptions()
      • 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, or null to 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 milliseconds
        maxDelayMillis - maximum reconnect delay in milliseconds
        Returns:
        this builder