Class KafkaListenerEndpointRegistry
- java.lang.Object
-
- org.springframework.kafka.config.KafkaListenerEndpointRegistry
-
- All Implemented Interfaces:
java.util.EventListener
,org.springframework.beans.factory.Aware
,org.springframework.beans.factory.DisposableBean
,org.springframework.context.ApplicationContextAware
,org.springframework.context.ApplicationListener<org.springframework.context.event.ContextRefreshedEvent>
,org.springframework.context.Lifecycle
,org.springframework.context.Phased
,org.springframework.context.SmartLifecycle
,ListenerContainerRegistry
public class KafkaListenerEndpointRegistry extends java.lang.Object implements ListenerContainerRegistry, org.springframework.beans.factory.DisposableBean, org.springframework.context.SmartLifecycle, org.springframework.context.ApplicationContextAware, org.springframework.context.ApplicationListener<org.springframework.context.event.ContextRefreshedEvent>
Creates the necessaryMessageListenerContainer
instances for the registered endpoints. Also manages the lifecycle of the listener containers, in particular within the lifecycle of the application context.Contrary to
MessageListenerContainer
s created manually, listener containers managed by registry are not beans in the application context and are not candidates for autowiring. UsegetListenerContainers()
if you need to access this registry's listener containers for management purposes. If you need to access to a specific message listener container, usegetListenerContainer(String)
with the id of the endpoint.
-
-
Field Summary
Fields Modifier and Type Field Description protected org.springframework.core.log.LogAccessor
logger
-
Constructor Summary
Constructors Constructor Description KafkaListenerEndpointRegistry()
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description protected MessageListenerContainer
createListenerContainer(KafkaListenerEndpoint endpoint, KafkaListenerContainerFactory<?> factory)
Create and start a newMessageListenerContainer
using the specified factory.void
destroy()
java.util.Collection<MessageListenerContainer>
getAllListenerContainers()
Return allMessageListenerContainer
instances including those managed by this registry and those declared as beans in the application context.MessageListenerContainer
getListenerContainer(java.lang.String id)
Return theMessageListenerContainer
with the specified id ornull
if no such container exists.java.util.Set<java.lang.String>
getListenerContainerIds()
Return the ids of the managedMessageListenerContainer
instance(s).java.util.Collection<MessageListenerContainer>
getListenerContainers()
Return the managedMessageListenerContainer
instance(s).int
getPhase()
MessageListenerContainer
getUnregisteredListenerContainer(java.lang.String id)
Return theMessageListenerContainer
with the specified id ornull
if no such container exists.boolean
isAutoStartup()
boolean
isRunning()
void
onApplicationEvent(org.springframework.context.event.ContextRefreshedEvent event)
void
registerListenerContainer(KafkaListenerEndpoint endpoint, KafkaListenerContainerFactory<?> factory)
Create a message listener container for the givenKafkaListenerEndpoint
.void
registerListenerContainer(KafkaListenerEndpoint endpoint, KafkaListenerContainerFactory<?> factory, boolean startImmediately)
Create a message listener container for the givenKafkaListenerEndpoint
.void
setAlwaysStartAfterRefresh(boolean alwaysStartAfterRefresh)
By default, containers registered for endpoints after the context is refreshed are immediately started, regardless of their autoStartup property, to comply with theSmartLifecycle
contract, where autoStartup is only considered during context initialization.void
setApplicationContext(org.springframework.context.ApplicationContext applicationContext)
void
start()
void
stop()
void
stop(java.lang.Runnable callback)
MessageListenerContainer
unregisterListenerContainer(java.lang.String id)
Unregister the listener container with the provided id.
-
-
-
Method Detail
-
setApplicationContext
public void setApplicationContext(org.springframework.context.ApplicationContext applicationContext) throws org.springframework.beans.BeansException
- Specified by:
setApplicationContext
in interfaceorg.springframework.context.ApplicationContextAware
- Throws:
org.springframework.beans.BeansException
-
getListenerContainer
@Nullable public MessageListenerContainer getListenerContainer(java.lang.String id)
Return theMessageListenerContainer
with the specified id ornull
if no such container exists.- Specified by:
getListenerContainer
in interfaceListenerContainerRegistry
- Parameters:
id
- the id of the container- Returns:
- the container or
null
if no container with that id exists - See Also:
KafkaListenerEndpoint.getId()
,getListenerContainerIds()
-
getUnregisteredListenerContainer
@Nullable public MessageListenerContainer getUnregisteredListenerContainer(java.lang.String id)
Description copied from interface:ListenerContainerRegistry
Return theMessageListenerContainer
with the specified id ornull
if no such container exists. Returns containers that are not registered with the registry, but exist in the application context.- Specified by:
getUnregisteredListenerContainer
in interfaceListenerContainerRegistry
- Parameters:
id
- the id of the container- Returns:
- the container or
null
if no container with that id exists - See Also:
ListenerContainerRegistry.getListenerContainerIds()
-
setAlwaysStartAfterRefresh
public void setAlwaysStartAfterRefresh(boolean alwaysStartAfterRefresh)
By default, containers registered for endpoints after the context is refreshed are immediately started, regardless of their autoStartup property, to comply with theSmartLifecycle
contract, where autoStartup is only considered during context initialization. Set to false to apply the autoStartup property, even for late endpoint binding. If this is called after the context is refreshed, it will apply to any endpoints registered after that call.- Parameters:
alwaysStartAfterRefresh
- false to apply the property.- Since:
- 2.8.7
-
getListenerContainerIds
public java.util.Set<java.lang.String> getListenerContainerIds()
Return the ids of the managedMessageListenerContainer
instance(s).- Specified by:
getListenerContainerIds
in interfaceListenerContainerRegistry
- Returns:
- the ids.
- See Also:
getListenerContainer(String)
-
getListenerContainers
public java.util.Collection<MessageListenerContainer> getListenerContainers()
Return the managedMessageListenerContainer
instance(s).- Specified by:
getListenerContainers
in interfaceListenerContainerRegistry
- Returns:
- the managed
MessageListenerContainer
instance(s). - See Also:
getAllListenerContainers()
-
getAllListenerContainers
public java.util.Collection<MessageListenerContainer> getAllListenerContainers()
Return allMessageListenerContainer
instances including those managed by this registry and those declared as beans in the application context. Prototype-scoped containers will be included. Lazy beans that have not yet been created will not be initialized by a call to this method.- Specified by:
getAllListenerContainers
in interfaceListenerContainerRegistry
- Returns:
- the
MessageListenerContainer
instance(s). - Since:
- 2.2.5
- See Also:
getListenerContainers()
-
registerListenerContainer
public void registerListenerContainer(KafkaListenerEndpoint endpoint, KafkaListenerContainerFactory<?> factory)
Create a message listener container for the givenKafkaListenerEndpoint
.This create the necessary infrastructure to honor that endpoint with regards to its configuration.
- Parameters:
endpoint
- the endpoint to addfactory
- the listener factory to use- See Also:
registerListenerContainer(KafkaListenerEndpoint, KafkaListenerContainerFactory, boolean)
-
registerListenerContainer
public void registerListenerContainer(KafkaListenerEndpoint endpoint, KafkaListenerContainerFactory<?> factory, boolean startImmediately)
Create a message listener container for the givenKafkaListenerEndpoint
.This create the necessary infrastructure to honor that endpoint with regards to its configuration.
The
startImmediately
flag determines if the container should be started immediately.- Parameters:
endpoint
- the endpoint to add.factory
- theKafkaListenerContainerFactory
to use.startImmediately
- start the container immediately if necessary- See Also:
getListenerContainers()
,getListenerContainer(String)
-
unregisterListenerContainer
@Nullable public MessageListenerContainer unregisterListenerContainer(java.lang.String id)
Unregister the listener container with the provided id.IMPORTANT: this method simply removes the container from the registry. It does NOT call any
Lifecycle
orDisposableBean
methods; you need to call them before or after calling this method to shut down the container.- Parameters:
id
- the id.- Returns:
- the container, if it was registered; null otherwise.
- Since:
- 2.8.9
-
createListenerContainer
protected MessageListenerContainer createListenerContainer(KafkaListenerEndpoint endpoint, KafkaListenerContainerFactory<?> factory)
Create and start a newMessageListenerContainer
using the specified factory.- Parameters:
endpoint
- the endpoint to create aMessageListenerContainer
.factory
- theKafkaListenerContainerFactory
to use.- Returns:
- the
MessageListenerContainer
.
-
destroy
public void destroy()
- Specified by:
destroy
in interfaceorg.springframework.beans.factory.DisposableBean
-
getPhase
public int getPhase()
- Specified by:
getPhase
in interfaceorg.springframework.context.Phased
- Specified by:
getPhase
in interfaceorg.springframework.context.SmartLifecycle
-
isAutoStartup
public boolean isAutoStartup()
- Specified by:
isAutoStartup
in interfaceorg.springframework.context.SmartLifecycle
-
start
public void start()
- Specified by:
start
in interfaceorg.springframework.context.Lifecycle
-
stop
public void stop()
- Specified by:
stop
in interfaceorg.springframework.context.Lifecycle
-
stop
public void stop(java.lang.Runnable callback)
- Specified by:
stop
in interfaceorg.springframework.context.SmartLifecycle
-
isRunning
public boolean isRunning()
- Specified by:
isRunning
in interfaceorg.springframework.context.Lifecycle
-
onApplicationEvent
public void onApplicationEvent(org.springframework.context.event.ContextRefreshedEvent event)
- Specified by:
onApplicationEvent
in interfaceorg.springframework.context.ApplicationListener<org.springframework.context.event.ContextRefreshedEvent>
-
-