Skip to content

Commit

Permalink
GH-2055: Containers Must Implement DisposableBean
Browse files Browse the repository at this point in the history
Resolves #2055

If context initialization fails, `Lifecycle.stop()` is not called.
Containers must be stopped from `DisposableBean` in this case.

**cherry-pick to 2.7.x, 2.6.x, 2.5.x**

# Conflicts:
#	spring-kafka/src/main/java/org/springframework/kafka/listener/MessageListenerContainer.java

# Conflicts:
#	spring-kafka/src/main/java/org/springframework/kafka/config/KafkaListenerEndpointRegistry.java
#	spring-kafka/src/main/java/org/springframework/kafka/listener/MessageListenerContainer.java
  • Loading branch information
garyrussell authored and artembilan committed Jan 4, 2022
1 parent 4cc23d4 commit 129b2bf
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 13 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2014-2019 the original author or authors.
* Copyright 2014-2022 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -227,14 +227,7 @@ protected MessageListenerContainer createListenerContainer(KafkaListenerEndpoint
@Override
public void destroy() {
for (MessageListenerContainer listenerContainer : getListenerContainers()) {
if (listenerContainer instanceof DisposableBean) {
try {
((DisposableBean) listenerContainer).destroy();
}
catch (Exception ex) {
this.logger.warn(ex, "Failed to destroy message listener container");
}
}
listenerContainer.destroy();
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2016-2019 the original author or authors.
* Copyright 2016-2022 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand All @@ -23,6 +23,7 @@
import org.apache.kafka.common.MetricName;
import org.apache.kafka.common.TopicPartition;

import org.springframework.beans.factory.DisposableBean;
import org.springframework.context.SmartLifecycle;
import org.springframework.lang.Nullable;

Expand All @@ -34,7 +35,7 @@
* @author Gary Russell
* @author Vladimir Tsanev
*/
public interface MessageListenerContainer extends SmartLifecycle {
public interface MessageListenerContainer extends SmartLifecycle, DisposableBean {

/**
* Setup the message listener to use. Throws an {@link IllegalArgumentException}
Expand Down Expand Up @@ -151,4 +152,9 @@ default String getListenerId() {
throw new UnsupportedOperationException("This container does not support retrieving the listener id");
}

@Override
default void destroy() {
stop();
}

}
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2016-2021 the original author or authors.
* Copyright 2016-2022 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -643,8 +643,9 @@ public void onMessage(ConsumerRecord<Integer, String> data) {
inOrder.verify(consumer).commitSync(anyMap(), any());
inOrder.verify(messageListener).onMessage(any(ConsumerRecord.class));
inOrder.verify(consumer).commitSync(anyMap(), any());
container.stop();
container.destroy();
assertThat(advised).containsExactly("one", "two", "one", "two");
assertThat(container.isRunning()).isFalse();
}

@SuppressWarnings("unchecked")
Expand Down

0 comments on commit 129b2bf

Please sign in to comment.