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)
    • 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