Class KafkaAvroMessagePublisher

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

public class KafkaAvroMessagePublisher extends Object implements MessagePublisher
A MessagePublisher that publishes Avro messages to Kafka in the Confluent Schema Registry wire format (magic byte + schema id + Avro binary), so real Confluent Avro consumers can read them.

MockServer's example payloads are JSON; this publisher encodes each JSON payload to Avro binary (AvroPayloadCodec), resolves the schema id, and frames it (ConfluentWireFormat). Two modes:

  • Registry-backed (a SchemaRegistryClient is supplied): the schema is registered under <topic>-value and its returned id is embedded — exactly what the Confluent serializers do.
  • Registry-less (no client): a fixed, configured schema id is embedded. Useful for closed round-trips where the same inline schema is used on both ends.
  • Constructor Details

    • KafkaAvroMessagePublisher

      public KafkaAvroMessagePublisher(String bootstrapServers, KafkaSecurity security, String schemaJson, SchemaRegistryClient registryClient, int fixedSchemaId)
      Parameters:
      bootstrapServers - comma-separated host:port pairs
      security - Kafka security (may be null for plaintext)
      schemaJson - the Avro schema JSON used to encode payloads (required)
      registryClient - the Schema Registry client, or null for registry-less mode
      fixedSchemaId - the schema id to embed in registry-less mode
  • 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)
      Description copied from interface: MessagePublisher
      Publish the given payload with per-message options derived from AsyncAPI bindings. Default implementation delegates to MessagePublisher.publish(String, String), ignoring the options — implementations that support them override this.
      Specified by:
      publish in interface MessagePublisher
      Parameters:
      channel - the channel / topic name
      payload - the message payload (typically JSON)
      options - per-message publish options (may be null)
    • 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 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