Class KafkaMessagePublisher

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

public class KafkaMessagePublisher extends Object implements MessagePublisher
A MessagePublisher that delegates to a Kafka KafkaProducer. The channel name maps directly to a Kafka topic.

Supports configurable record keys and message headers in addition to the basic payload publishing.

  • Constructor Details

    • KafkaMessagePublisher

      public KafkaMessagePublisher(String bootstrapServers)
      Create a publisher connected to the given Kafka bootstrap servers using plaintext (no security). Backward-compatible entry point.
      Parameters:
      bootstrapServers - comma-separated list of host:port pairs
    • KafkaMessagePublisher

      public KafkaMessagePublisher(String bootstrapServers, KafkaSecurity security)
      Create a publisher connected to the given Kafka bootstrap servers with optional security configuration.
      Parameters:
      bootstrapServers - comma-separated list of host:port pairs
      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. The PublishOptions.getKey() sets the Kafka record key and PublishOptions.getHeaders() are added as Kafka record headers. MQTT-specific fields (QoS, retain) are ignored.
      Specified by:
      publish in interface MessagePublisher
      Parameters:
      channel - the Kafka topic name
      payload - the message payload (typically JSON)
      options - per-message publish options (may be null)
    • publish

      public void publish(String channel, String key, String payload, Map<String,String> headers)
      Publish a message with a specific key and optional headers.
      Specified by:
      publish in interface MessagePublisher
      Parameters:
      channel - the Kafka topic name
      key - the record key (may be null)
      payload - the message payload (typically JSON)
      headers - optional headers (may be null or empty)
    • 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