Package org.mockserver.async.subscribe
Class KafkaMessageSubscriber
java.lang.Object
org.mockserver.async.subscribe.KafkaMessageSubscriber
- All Implemented Interfaces:
MessageSubscriber
A
MessageSubscriber that uses a Kafka KafkaConsumer to subscribe
to topics and record received messages. Runs a background poll loop on a daemon
thread.
All KafkaConsumer access is confined to the single poll thread to avoid
ConcurrentModificationException. Subscribe and unsubscribe
requests are queued and drained at the top of each poll iteration.
Recorded messages are stored in a bounded BoundedMessageStore per channel
to prevent unbounded memory growth.
-
Constructor Summary
ConstructorsConstructorDescriptionKafkaMessageSubscriber(String bootstrapServers, String groupId) Create a subscriber connected to the given Kafka bootstrap servers using plaintext (no security).KafkaMessageSubscriber(String bootstrapServers, String groupId, int maxRecordedMessages) Create a subscriber with a custom recorded-message cap per channel.KafkaMessageSubscriber(String bootstrapServers, String groupId, int maxRecordedMessages, KafkaSecurity security) Create a subscriber with optional security configuration. -
Method Summary
Modifier and TypeMethodDescriptionvoidclose()Clear all recorded messages and stop all subscriptions.Return all messages recorded across all channels.getRecordedMessages(String channel) Return all messages recorded on the given channel since subscription started (or since the last reset).voidStart subscribing to the given channel.voidunsubscribe(String channel) Stop subscribing to the given channel.
-
Constructor Details
-
KafkaMessageSubscriber
Create a subscriber connected to the given Kafka bootstrap servers using plaintext (no security). Backward-compatible entry point.- Parameters:
bootstrapServers- comma-separated list of host:port pairsgroupId- the consumer group ID
-
KafkaMessageSubscriber
Create a subscriber with a custom recorded-message cap per channel. -
KafkaMessageSubscriber
public KafkaMessageSubscriber(String bootstrapServers, String groupId, int maxRecordedMessages, KafkaSecurity security) Create a subscriber with optional security configuration.- Parameters:
bootstrapServers- comma-separated list of host:port pairsgroupId- the consumer group IDmaxRecordedMessages- maximum recorded messages per channelsecurity- security configuration (may be null for plaintext)
-
-
Method Details
-
subscribe
Description copied from interface:MessageSubscriberStart subscribing to the given channel. Messages will be recorded internally and are retrievable viaMessageSubscriber.getRecordedMessages(String).- Specified by:
subscribein interfaceMessageSubscriber- Parameters:
channel- the channel/topic to subscribe to
-
unsubscribe
Description copied from interface:MessageSubscriberStop subscribing to the given channel.- Specified by:
unsubscribein interfaceMessageSubscriber
-
getRecordedMessages
Description copied from interface:MessageSubscriberReturn all messages recorded on the given channel since subscription started (or since the last reset).- Specified by:
getRecordedMessagesin interfaceMessageSubscriber
-
getAllRecordedMessages
Description copied from interface:MessageSubscriberReturn all messages recorded across all channels.- Specified by:
getAllRecordedMessagesin interfaceMessageSubscriber
-
close
public void close()Description copied from interface:MessageSubscriberClear all recorded messages and stop all subscriptions.- Specified by:
closein interfaceMessageSubscriber
-