Package org.springframework.kafka.core
Class KafkaAdmin
java.lang.Object
org.springframework.kafka.core.KafkaResourceFactory
org.springframework.kafka.core.KafkaAdmin
- All Implemented Interfaces:
Aware,SmartInitializingSingleton,ApplicationContextAware,KafkaAdminOperations
public class KafkaAdmin
extends KafkaResourceFactory
implements ApplicationContextAware, SmartInitializingSingleton, KafkaAdminOperations
An admin that delegates to an
AdminClient to create topics defined
in the application context.- Since:
- 1.3
- Author:
- Gary Russell, Artem Bilan
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionstatic classWrapper for a collection ofNewTopicto facilitate declaring multiple topics as a single bean. -
Field Summary
FieldsModifier and TypeFieldDescriptionstatic final DurationThe default close timeout duration as 10 seconds. -
Constructor Summary
ConstructorsConstructorDescriptionKafkaAdmin(Map<String, Object> config) Create an instance with anAdminClientbased on the supplied configuration. -
Method Summary
Modifier and TypeMethodDescriptionvoidvoidcreateOrModifyTopics(org.apache.kafka.clients.admin.NewTopic... topics) Create topics if they don't exist or increase the number of partitions if needed.describeTopics(String... topicNames) ObtainTopicDescriptions for these topics.Get an unmodifiable copy of this admin's configuration.final booleanCall this method to check/add topics; this might be needed if the broker was not available when the application context was initialized, andfatalIfBrokerNotAvailableis false, orautoCreatewas set to false.voidsetApplicationContext(ApplicationContext applicationContext) voidsetAutoCreate(boolean autoCreate) Set to false to suppress auto creation of topics during context initialization.voidsetCloseTimeout(int closeTimeout) Set the close timeout in seconds.voidsetFatalIfBrokerNotAvailable(boolean fatalIfBrokerNotAvailable) Set to true if you want the application context to fail to load if we are unable to connect to the broker during initialization, to check/add topics.voidsetOperationTimeout(int operationTimeout) Set the operation timeout in seconds.Methods inherited from class org.springframework.kafka.core.KafkaResourceFactory
checkBootstrap, getBootstrapServers, setBootstrapServersSupplier
-
Field Details
-
DEFAULT_CLOSE_TIMEOUT
The default close timeout duration as 10 seconds.
-
-
Constructor Details
-
KafkaAdmin
Create an instance with anAdminClientbased on the supplied configuration.- Parameters:
config- the configuration for theAdminClient.
-
-
Method Details
-
setApplicationContext
- Specified by:
setApplicationContextin interfaceApplicationContextAware- Throws:
BeansException
-
setCloseTimeout
public void setCloseTimeout(int closeTimeout) Set the close timeout in seconds. Defaults toDEFAULT_CLOSE_TIMEOUTseconds.- Parameters:
closeTimeout- the timeout.
-
setOperationTimeout
public void setOperationTimeout(int operationTimeout) Set the operation timeout in seconds. Defaults to 30 seconds.- Parameters:
operationTimeout- the timeout.
-
setFatalIfBrokerNotAvailable
public void setFatalIfBrokerNotAvailable(boolean fatalIfBrokerNotAvailable) Set to true if you want the application context to fail to load if we are unable to connect to the broker during initialization, to check/add topics.- Parameters:
fatalIfBrokerNotAvailable- true to fail.
-
setAutoCreate
public void setAutoCreate(boolean autoCreate) Set to false to suppress auto creation of topics during context initialization.- Parameters:
autoCreate- boolean flag to indicate creating topics or not during context initialization- See Also:
-
getConfigurationProperties
Description copied from interface:KafkaAdminOperationsGet an unmodifiable copy of this admin's configuration.- Specified by:
getConfigurationPropertiesin interfaceKafkaAdminOperations- Returns:
- the configuration map.
-
afterSingletonsInstantiated
public void afterSingletonsInstantiated()- Specified by:
afterSingletonsInstantiatedin interfaceSmartInitializingSingleton
-
initialize
public final boolean initialize()Call this method to check/add topics; this might be needed if the broker was not available when the application context was initialized, andfatalIfBrokerNotAvailableis false, orautoCreatewas set to false.- Returns:
- true if successful.
- See Also:
-
createOrModifyTopics
public void createOrModifyTopics(org.apache.kafka.clients.admin.NewTopic... topics) Description copied from interface:KafkaAdminOperationsCreate topics if they don't exist or increase the number of partitions if needed.- Specified by:
createOrModifyTopicsin interfaceKafkaAdminOperations- Parameters:
topics- the topics.
-
describeTopics
public Map<String,org.apache.kafka.clients.admin.TopicDescription> describeTopics(String... topicNames) Description copied from interface:KafkaAdminOperationsObtainTopicDescriptions for these topics.- Specified by:
describeTopicsin interfaceKafkaAdminOperations- Parameters:
topicNames- the topic names.- Returns:
- a map of name:topicDescription.
-