Skip to content

Commit

Permalink
spring-projectsGH-2055: Containers Must Implement DisposableBean
Browse files Browse the repository at this point in the history
Resolves spring-projects#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**
  • Loading branch information
garyrussell committed Jan 4, 2022
1 parent 5e4e6b4 commit 1b9f93a
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -253,13 +253,11 @@ 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");
}
try {
listenerContainer.destroy();
}
catch (Exception ex) {
this.logger.warn(ex, "Failed to destroy message listener container");
}
}
}
Expand Down
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 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 @@ -35,7 +36,7 @@
* @author Vladimir Tsanev
* @author Tomaz Fernandes
*/
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 @@ -225,4 +226,9 @@ default void stopAbnormally(Runnable callback) {
stop(callback);
}

@Override
default void destroy() throws Exception {
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 @@ -650,8 +650,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();
}

@Test
Expand Down

0 comments on commit 1b9f93a

Please sign in to comment.