Class MqttMessageSubscriber

java.lang.Object
org.mockserver.async.subscribe.MqttMessageSubscriber
All Implemented Interfaces:
MessageSubscriber

public class MqttMessageSubscriber extends Object implements MessageSubscriber
A MessageSubscriber that uses an MQTT MqttClient to subscribe to topics and record received messages.

Recorded messages are stored in a bounded BoundedMessageStore per channel to prevent unbounded memory growth.

  • Constructor Details

    • MqttMessageSubscriber

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

      public MqttMessageSubscriber(String brokerUrl, String clientId, int qos)
      Create a subscriber with a specific QoS level and no security.
    • MqttMessageSubscriber

      public MqttMessageSubscriber(String brokerUrl, String clientId, int qos, int maxRecordedMessages)
      Create a subscriber with a specific QoS level and recorded-message cap, no security. Backward-compatible entry point.
    • MqttMessageSubscriber

      public MqttMessageSubscriber(String brokerUrl, String clientId, int qos, int maxRecordedMessages, MqttSecurity security)
      Create a subscriber 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)
      maxRecordedMessages - maximum recorded messages per channel
      security - security configuration (may be null for plaintext)
  • Method Details

    • isHealthy

      public boolean isHealthy()
      Returns false if the MQTT callback has reported a lost connection (the subscriber will record no further messages until reconnected); true otherwise.
    • subscribe

      public void subscribe(String channel)
      Description copied from interface: MessageSubscriber
      Start subscribing to the given channel. Messages will be recorded internally and are retrievable via MessageSubscriber.getRecordedMessages(String).
      Specified by:
      subscribe in interface MessageSubscriber
      Parameters:
      channel - the channel/topic to subscribe to
    • unsubscribe

      public void unsubscribe(String channel)
      Description copied from interface: MessageSubscriber
      Stop subscribing to the given channel.
      Specified by:
      unsubscribe in interface MessageSubscriber
    • getRecordedMessages

      public List<RecordedMessage> getRecordedMessages(String channel)
      Return all messages recorded on the given channel since subscription started (or since the last reset).

      Messages are recorded under the concrete topic they arrived on, but a subscription may have used a wildcard filter. When the requested channel is a wildcard filter, every recorded topic matching it (per MQTT 3.1.1 §4.7) is returned — without this a wildcard subscription would record messages that could never be retrieved, and verification would always report zero matches.

      Specified by:
      getRecordedMessages in interface MessageSubscriber
    • getAllRecordedMessages

      public List<RecordedMessage> getAllRecordedMessages()
      Description copied from interface: MessageSubscriber
      Return all messages recorded across all channels.
      Specified by:
      getAllRecordedMessages in interface MessageSubscriber
    • close

      public void close()
      Description copied from interface: MessageSubscriber
      Clear all recorded messages and stop all subscriptions.
      Specified by:
      close in interface MessageSubscriber