Package org.springframework.kafka.test
Interface EmbeddedKafkaBroker
- All Superinterfaces:
DisposableBean
,InitializingBean
- All Known Implementing Classes:
EmbeddedKafkaKraftBroker
,EmbeddedKafkaZKBroker
- Since:
- 3.1
- Author:
- Gary Russell
-
Field Summary
Modifier and TypeFieldDescriptionstatic final String
static final String
Set the value of this property to a property name that should be set to the list of embedded broker addresses instead of "spring.embedded.kafka.brokers".static final String
static final int
static final String
static final String
-
Method Summary
Modifier and TypeMethodDescriptionvoid
Add topics to the existing broker(s) using the configured number of partitions.void
addTopics
(org.apache.kafka.clients.admin.NewTopic... topicsToAdd) Add topics to the existing broker(s).addTopicsWithResults
(String... topicsToAdd) Add topics to the existing broker(s) using the configured number of partitions.addTopicsWithResults
(org.apache.kafka.clients.admin.NewTopic... topicsToAdd) Add topics to the existing broker(s) and returning a map of results.adminTimeout
(int adminTimeout) Set the timeout in seconds for admin operations (e.g.default void
brokerListProperty
(String brokerListProperty) Set the system property with this name to the list of broker addresses.brokerProperties
(Map<String, String> properties) Specify the properties to configure Kafka Broker before start, e.g.void
consumeFromAllEmbeddedTopics
(org.apache.kafka.clients.consumer.Consumer<?, ?> consumer) Subscribe a consumer to all the embedded topics.void
consumeFromAllEmbeddedTopics
(org.apache.kafka.clients.consumer.Consumer<?, ?> consumer, boolean seekToEnd) Subscribe a consumer to all the embedded topics.void
consumeFromAnEmbeddedTopic
(org.apache.kafka.clients.consumer.Consumer<?, ?> consumer, boolean seekToEnd, String topic) Subscribe a consumer to one of the embedded topics.void
consumeFromAnEmbeddedTopic
(org.apache.kafka.clients.consumer.Consumer<?, ?> consumer, String topic) Subscribe a consumer to one of the embedded topics.void
consumeFromEmbeddedTopics
(org.apache.kafka.clients.consumer.Consumer<?, ?> consumer, boolean seekToEnd, String... topicsToConsume) Subscribe a consumer to one or more of the embedded topics.void
consumeFromEmbeddedTopics
(org.apache.kafka.clients.consumer.Consumer<?, ?> consumer, String... topicsToConsume) Subscribe a consumer to one or more of the embedded topics.default void
destroy()
Get the bootstrap server addresses as a String.int
Get the configured number of partitions per topic.Get the topics.kafkaPorts
(int... ports) Set explicit ports on which the kafka brokers will listen.
-
Field Details
-
DEFAULT_ADMIN_TIMEOUT
static final int DEFAULT_ADMIN_TIMEOUT- See Also:
-
BEAN_NAME
- See Also:
-
BROKER_LIST_PROPERTY
Set the value of this property to a property name that should be set to the list of embedded broker addresses instead of "spring.embedded.kafka.brokers".- See Also:
-
SPRING_EMBEDDED_KAFKA_BROKERS
- See Also:
-
BROKER_NEEDED
- See Also:
-
LOOPBACK
- See Also:
-
-
Method Details
-
kafkaPorts
Set explicit ports on which the kafka brokers will listen. Useful when running an embedded broker that you want to access from other processes.- Parameters:
ports
- the ports.- Returns:
- the
EmbeddedKafkaBroker
.
-
getTopics
Get the topics.- Returns:
- the topics.
-
destroy
default void destroy()- Specified by:
destroy
in interfaceDisposableBean
-
afterPropertiesSet
default void afterPropertiesSet()- Specified by:
afterPropertiesSet
in interfaceInitializingBean
-
brokerProperties
Specify the properties to configure Kafka Broker before start, e.g.auto.create.topics.enable
,transaction.state.log.replication.factor
etc.- Parameters:
properties
- the properties to use for configuring Kafka Broker(s).- Returns:
- this for chaining configuration.
- See Also:
-
KafkaConfig
-
brokerListProperty
Set the system property with this name to the list of broker addresses. Defaults tospring.kafka.bootstrap-servers
for Spring Boot compatibility.- Parameters:
brokerListProperty
- the brokerListProperty to set- Returns:
- this broker.
-
adminTimeout
Set the timeout in seconds for admin operations (e.g. topic creation, close).- Parameters:
adminTimeout
- the timeout.- Returns:
- the
EmbeddedKafkaBroker
- Since:
- 2.8.5
-
getBrokersAsString
String getBrokersAsString()Get the bootstrap server addresses as a String.- Returns:
- the bootstrap servers.
-
addTopics
Add topics to the existing broker(s) using the configured number of partitions. The broker(s) must be running.- Parameters:
topicsToAdd
- the topics.
-
addTopics
void addTopics(org.apache.kafka.clients.admin.NewTopic... topicsToAdd) Add topics to the existing broker(s). The broker(s) must be running.- Parameters:
topicsToAdd
- the topics.
-
addTopicsWithResults
Add topics to the existing broker(s) and returning a map of results. The broker(s) must be running.- Parameters:
topicsToAdd
- the topics.- Returns:
- the results; null values indicate success.
-
addTopicsWithResults
Add topics to the existing broker(s) using the configured number of partitions. The broker(s) must be running.- Parameters:
topicsToAdd
- the topics.- Returns:
- the results; null values indicate success.
-
consumeFromEmbeddedTopics
void consumeFromEmbeddedTopics(org.apache.kafka.clients.consumer.Consumer<?, ?> consumer, boolean seekToEnd, String... topicsToConsume) Subscribe a consumer to one or more of the embedded topics.- Parameters:
consumer
- the consumer.topicsToConsume
- the topics.seekToEnd
- true to seek to the end instead of the beginning.- Throws:
IllegalStateException
- if you attempt to consume from a topic that is not in the list of embedded topics.
-
consumeFromEmbeddedTopics
void consumeFromEmbeddedTopics(org.apache.kafka.clients.consumer.Consumer<?, ?> consumer, String... topicsToConsume) Subscribe a consumer to one or more of the embedded topics.- Parameters:
consumer
- the consumer.topicsToConsume
- the topics.- Throws:
IllegalStateException
- if you attempt to consume from a topic that is not in the list of embedded topics.
-
consumeFromAnEmbeddedTopic
void consumeFromAnEmbeddedTopic(org.apache.kafka.clients.consumer.Consumer<?, ?> consumer, boolean seekToEnd, String topic) Subscribe a consumer to one of the embedded topics.- Parameters:
consumer
- the consumer.seekToEnd
- true to seek to the end instead of the beginning.topic
- the topic.
-
consumeFromAnEmbeddedTopic
void consumeFromAnEmbeddedTopic(org.apache.kafka.clients.consumer.Consumer<?, ?> consumer, String topic) Subscribe a consumer to one of the embedded topics.- Parameters:
consumer
- the consumer.topic
- the topic.
-
consumeFromAllEmbeddedTopics
void consumeFromAllEmbeddedTopics(org.apache.kafka.clients.consumer.Consumer<?, ?> consumer, boolean seekToEnd) Subscribe a consumer to all the embedded topics.- Parameters:
seekToEnd
- true to seek to the end instead of the beginning.consumer
- the consumer.
-
consumeFromAllEmbeddedTopics
void consumeFromAllEmbeddedTopics(org.apache.kafka.clients.consumer.Consumer<?, ?> consumer) Subscribe a consumer to all the embedded topics.- Parameters:
consumer
- the consumer.
-
getPartitionsPerTopic
int getPartitionsPerTopic()Get the configured number of partitions per topic.- Returns:
- the partition count.
-