Class MqttMessagePublisher

java.lang.Object
org.mockserver.async.publish.MqttMessagePublisher
All Implemented Interfaces:
MessagePublisher

public class MqttMessagePublisher extends Object implements MessagePublisher
A MessagePublisher that delegates to an MQTT MqttClient. The channel name maps directly to an MQTT topic.

Supports configurable QoS (0, 1, or 2) and both text and binary payloads.

  • Constructor Details

    • MqttMessagePublisher

      public MqttMessagePublisher(String brokerUrl, String clientId)
      Create a publisher connected to the given MQTT broker with default QoS (1) and no security. Backward-compatible entry point.
      Parameters:
      brokerUrl - the MQTT broker URL (e.g. tcp://localhost:1883)
      clientId - the client identifier
    • MqttMessagePublisher

      public MqttMessagePublisher(String brokerUrl, String clientId, int qos)
      Create a publisher connected to the given MQTT broker with a specific QoS and no security. Backward-compatible entry point.
      Parameters:
      brokerUrl - the MQTT broker URL (e.g. tcp://localhost:1883)
      clientId - the client identifier
      qos - the MQTT QoS level (0, 1, or 2)
    • MqttMessagePublisher

      public MqttMessagePublisher(String brokerUrl, String clientId, int qos, MqttSecurity security)
      Create a publisher connected to the given MQTT broker with optional security configuration.
      Parameters:
      brokerUrl - the MQTT broker URL (e.g. ssl://localhost:8883)
      clientId - the client identifier
      qos - the MQTT QoS level (0, 1, or 2)
      security - security configuration (may be null for plaintext)
  • Method Details

    • publish

      public void publish(String channel, String payload)
      Description copied from interface: MessagePublisher
      Publish the given payload to the specified channel with no key or headers.
      Specified by:
      publish in interface MessagePublisher
      Parameters:
      channel - the channel / topic name
      payload - the message payload (typically JSON)
    • publish

      public void publish(String channel, String payload, PublishOptions options)
      Publish a message with per-message options from AsyncAPI bindings. Applies PublishOptions.getQos() and PublishOptions.getRetain() when non-null; falls back to the instance-level QoS and no-retain defaults. The Kafka key field is ignored for MQTT.

      MQTT does not support message-level headers; when PublishOptions.getHeaders() is non-empty, a DEBUG log is emitted noting that header-location correlation IDs are not delivered over MQTT. Payload-location correlation IDs are unaffected (they are injected into the payload before publishing).

      Specified by:
      publish in interface MessagePublisher
      Parameters:
      channel - the MQTT topic name
      payload - the message payload (typically JSON)
      options - per-message publish options (may be null)
    • publishBytes

      public void publishBytes(String channel, byte[] payload)
      Publish a binary payload to the given channel. Supports binary message formats as specified in AsyncAPI channel bindings.
      Parameters:
      channel - the MQTT topic name
      payload - the raw binary payload
    • getQos

      public int getQos()
      Returns:
      the configured QoS level for this publisher
    • close

      public void close()
      Description copied from interface: MessagePublisher
      Release any resources held by this publisher (producer connections, etc.).
      Specified by:
      close in interface MessagePublisher