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 Summary

    Constructors
    Constructor
    Description
    KafkaMessagePublisher(String bootstrapServers)
    Create a publisher connected to the given Kafka bootstrap servers using plaintext (no security).
    KafkaMessagePublisher(String bootstrapServers, KafkaSecurity security)
    Create a publisher connected to the given Kafka bootstrap servers with optional security configuration.
  • Method Summary

    Modifier and Type
    Method
    Description
    void
    Release any resources held by this publisher (producer connections, etc.).
    void
    Block until every message published so far has been acknowledged by the broker, throwing if any of them failed to be delivered.
    void
    publish(String channel, String payload)
    Publish the given payload to the specified channel with no key or headers.
    void
    publish(String channel, String key, String payload, Map<String,String> headers)
    Publish a message with a specific key and optional headers.
    void
    publish(String channel, String payload, PublishOptions options)
    Publish a message with per-message options from AsyncAPI bindings.

    Methods inherited from class java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • 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)
    • flush

      public void flush()
      Block until every message published so far has been acknowledged by the broker, throwing if any of them failed to be delivered.

      Implementations that send asynchronously (notably Kafka) would otherwise let a caller report a successful publish before the broker has accepted — or rejected — the message. Callers that report publish success to a user must call this first.

      The default implementation is a no-op, for publishers that deliver synchronously.

      Blocks until the producer's buffer has been drained and every in-flight send has been acknowledged, then rethrows the first delivery failure seen since the last flush.

      Specified by:
      flush in interface MessagePublisher
    • 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