Package org.mockserver.async.publish
Class KafkaMessagePublisher
java.lang.Object
org.mockserver.async.publish.KafkaMessagePublisher
- All Implemented Interfaces:
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
ConstructorsConstructorDescriptionKafkaMessagePublisher(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 TypeMethodDescriptionvoidclose()Release any resources held by this publisher (producer connections, etc.).voidflush()Block until every message published so far has been acknowledged by the broker, throwing if any of them failed to be delivered.voidPublish the given payload to the specified channel with no key or headers.voidPublish a message with a specific key and optional headers.voidpublish(String channel, String payload, PublishOptions options) Publish a message with per-message options from AsyncAPI bindings.
-
Constructor Details
-
KafkaMessagePublisher
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
Create a publisher connected to the given Kafka bootstrap servers with optional security configuration.- Parameters:
bootstrapServers- comma-separated list of host:port pairssecurity- security configuration (may be null for plaintext)
-
-
Method Details
-
publish
Description copied from interface:MessagePublisherPublish the given payload to the specified channel with no key or headers.- Specified by:
publishin interfaceMessagePublisher- Parameters:
channel- the channel / topic namepayload- the message payload (typically JSON)
-
publish
Publish a message with per-message options from AsyncAPI bindings. ThePublishOptions.getKey()sets the Kafka record key andPublishOptions.getHeaders()are added as Kafka record headers. MQTT-specific fields (QoS, retain) are ignored.- Specified by:
publishin interfaceMessagePublisher- Parameters:
channel- the Kafka topic namepayload- the message payload (typically JSON)options- per-message publish options (may be null)
-
publish
Publish a message with a specific key and optional headers.- Specified by:
publishin interfaceMessagePublisher- Parameters:
channel- the Kafka topic namekey- 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:
flushin interfaceMessagePublisher
-
close
public void close()Description copied from interface:MessagePublisherRelease any resources held by this publisher (producer connections, etc.).- Specified by:
closein interfaceMessagePublisher
-