Class KafkaListenerEndpointRegistry
- All Implemented Interfaces:
EventListener
,Aware
,DisposableBean
,ApplicationContextAware
,ApplicationListener<ContextRefreshedEvent>
,Lifecycle
,Phased
,SmartLifecycle
,ListenerContainerRegistry
MessageListenerContainer
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. Use getListenerContainers()
if
you need to access this registry's listener containers for management purposes.
If you need to access to a specific message listener container, use
getListenerContainer(String)
with the id of the endpoint.
- Author:
- Stephane Nicoll, Juergen Hoeller, Artem Bilan, Gary Russell, Asi Bross, Wang Zhiyang, Joo Hyuk Kim
- See Also:
-
Field Summary
FieldsFields inherited from interface org.springframework.context.SmartLifecycle
DEFAULT_PHASE
-
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionprotected MessageListenerContainer
createListenerContainer
(KafkaListenerEndpoint endpoint, KafkaListenerContainerFactory<?> factory) Create and start a newMessageListenerContainer
using the specified factory.void
destroy()
Return allMessageListenerContainer
instances including those managed by this registry and those declared as beans in the application context.Return theMessageListenerContainer
with the specified id ornull
if no such container exists.Return the ids of the managedMessageListenerContainer
instance(s).Return the managedMessageListenerContainer
instance(s).getListenerContainersMatching
(BiPredicate<String, MessageListenerContainer> idAndContainerMatcher) Return allMessageListenerContainer
instances that satisfy the given bi-predicate.getListenerContainersMatching
(Predicate<String> idMatcher) Return allMessageListenerContainer
instances with id matching the predicate or emptyCollection
if no such container exists.int
getPhase()
Return theMessageListenerContainer
with the specified id ornull
if no such container exists.boolean
void
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
(ApplicationContext applicationContext) void
start()
void
stop()
void
Unregister the listener container with the provided id.Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
Methods inherited from interface org.springframework.context.ApplicationListener
supportsAsyncExecution
Methods inherited from interface org.springframework.context.SmartLifecycle
isAutoStartup
-
Field Details
-
logger
-
-
Constructor Details
-
KafkaListenerEndpointRegistry
public KafkaListenerEndpointRegistry()
-
-
Method Details
-
setApplicationContext
- Specified by:
setApplicationContext
in interfaceApplicationContextAware
- Throws:
BeansException
-
getListenerContainer
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:
-
getListenerContainersMatching
public Collection<MessageListenerContainer> getListenerContainersMatching(Predicate<String> idMatcher) Return allMessageListenerContainer
instances with id matching the predicate or emptyCollection
if no such container exists.- Specified by:
getListenerContainersMatching
in interfaceListenerContainerRegistry
- Parameters:
idMatcher
- the predicate to match container id with- Returns:
- the containers or empty
Collection
if no container with that id exists - Since:
- 3.2
- See Also:
-
getListenerContainersMatching
public Collection<MessageListenerContainer> getListenerContainersMatching(BiPredicate<String, MessageListenerContainer> idAndContainerMatcher) Return allMessageListenerContainer
instances that satisfy the given bi-predicate. TheBiPredicate<String, MessageListenerContainer>
takes the container id and the container itself as arguments. This allows for more sophisticated filtering, including properties or state of the container itself.- Specified by:
getListenerContainersMatching
in interfaceListenerContainerRegistry
- Parameters:
idAndContainerMatcher
- the bi-predicate to match the container id and the container- Returns:
- the containers that match the bi-predicate criteria or an empty
Collection
if no matching containers exist - Since:
- 3.2
- See Also:
-
getUnregisteredListenerContainer
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:
-
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
Return the ids of the managedMessageListenerContainer
instance(s).- Specified by:
getListenerContainerIds
in interfaceListenerContainerRegistry
- Returns:
- the ids.
- See Also:
-
getListenerContainers
Return the managedMessageListenerContainer
instance(s).- Specified by:
getListenerContainers
in interfaceListenerContainerRegistry
- Returns:
- the managed
MessageListenerContainer
instance(s). - See Also:
-
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:
-
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
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:
-
unregisterListenerContainer
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 interfaceDisposableBean
-
getPhase
public int getPhase()- Specified by:
getPhase
in interfacePhased
- Specified by:
getPhase
in interfaceSmartLifecycle
-
start
public void start() -
stop
public void stop() -
stop
- Specified by:
stop
in interfaceSmartLifecycle
-
isRunning
public boolean isRunning() -
onApplicationEvent
- Specified by:
onApplicationEvent
in interfaceApplicationListener<ContextRefreshedEvent>
-