Skip to content

Commit 0ee2d49

Browse files
committed
GH-1827: Fix New Sonar Issues
1 parent 2d43d6b commit 0ee2d49

File tree

4 files changed

+19
-20
lines changed

4 files changed

+19
-20
lines changed

spring-kafka/src/main/java/org/springframework/kafka/annotation/KafkaListener.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,6 @@
2323
import java.lang.annotation.RetentionPolicy;
2424
import java.lang.annotation.Target;
2525

26-
import org.springframework.kafka.listener.ContainerGroup;
2726
import org.springframework.messaging.handler.annotation.MessageMapping;
2827

2928
/**
@@ -154,7 +153,8 @@
154153
* allows, for example, iteration over the collection to start/stop a subset of
155154
* containers. The {@code Collection} beans are deprecated as of version 2.7.3 and
156155
* will be removed in 2.8. Instead, a bean with name {@code containerGroup + ".group"}
157-
* and type {@link ContainerGroup} should be used instead.
156+
* and type {@link org.springframework.kafka.listener.ContainerGroup} should be used
157+
* instead.
158158
* <p>
159159
* SpEL {@code #{...}} and property place holders {@code ${...}} are supported.
160160
* @return the bean name for the group.

spring-kafka/src/main/java/org/springframework/kafka/listener/ContainerGroupSequencer.java

Lines changed: 12 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,7 @@ public void setApplicationContext(ApplicationContext applicationContext) throws
9494
* default, the containers in the final group remain running.
9595
* @param stopLastGroupWhenIdle true to stop containers in the final group.
9696
*/
97-
public void setStopLastGroupWhenIdle(boolean stopLastGroupWhenIdle) {
97+
public synchronized void setStopLastGroupWhenIdle(boolean stopLastGroupWhenIdle) {
9898
this.stopLastGroupWhenIdle = stopLastGroupWhenIdle;
9999
}
100100

@@ -103,21 +103,18 @@ public synchronized void onApplicationEvent(ListenerContainerIdleEvent event) {
103103
LOGGER.debug(() -> event.toString());
104104
MessageListenerContainer parent = event.getContainer(MessageListenerContainer.class);
105105
MessageListenerContainer container = (MessageListenerContainer) event.getSource();
106-
if (this.running) {
107-
if (this.currentGroup != null && this.currentGroup.contains(parent)) {
108-
if (this.iterator.hasNext() || this.stopLastGroupWhenIdle) {
109-
this.executor.execute(() -> {
110-
LOGGER.debug(() -> "Stopping: " + container);
111-
container.stop(() -> {
112-
if (!parent.isChildRunning()) {
113-
this.executor.execute(() -> {
114-
stopParentAndCheckGroup(parent);
115-
});
116-
}
106+
boolean inCurrentGroup = this.currentGroup != null && this.currentGroup.contains(parent);
107+
if (this.running && inCurrentGroup && (this.iterator.hasNext() || this.stopLastGroupWhenIdle)) {
108+
this.executor.execute(() -> {
109+
LOGGER.debug(() -> "Stopping: " + container);
110+
container.stop(() -> {
111+
if (!parent.isChildRunning()) {
112+
this.executor.execute(() -> {
113+
stopParentAndCheckGroup(parent);
117114
});
118-
});
119-
}
120-
}
115+
}
116+
});
117+
});
121118
}
122119
}
123120

spring-kafka/src/main/java/org/springframework/kafka/listener/ContainerProperties.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -459,6 +459,7 @@ public long getShutdownTimeout() {
459459
return this.shutdownTimeout;
460460
}
461461

462+
@Nullable
462463
public Long getIdleEventInterval() {
463464
return this.idleEventInterval;
464465
}

spring-kafka/src/main/java/org/springframework/kafka/listener/KafkaMessageListenerContainer.java

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1506,10 +1506,11 @@ private void resumePartitionsIfNecessary() {
15061506
}
15071507

15081508
private void checkIdle() {
1509-
if (this.containerProperties.getIdleEventInterval() != null) {
1509+
Long idleEventInterval = this.containerProperties.getIdleEventInterval();
1510+
if (idleEventInterval != null) {
15101511
long now = System.currentTimeMillis();
1511-
if (now > this.lastReceive + this.containerProperties.getIdleEventInterval()
1512-
&& now > this.lastAlertAt + this.containerProperties.getIdleEventInterval()) {
1512+
if (now > this.lastReceive + idleEventInterval
1513+
&& now > this.lastAlertAt + idleEventInterval) {
15131514
this.wasIdle = true;
15141515
publishIdleContainerEvent(now - this.lastReceive, this.consumer, this.consumerPaused);
15151516
this.lastAlertAt = now;

0 commit comments

Comments
 (0)