Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Gh 3340 #3347

Closed
wants to merge 17 commits into from
Closed

Gh 3340 #3347

Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
/*
* Copyright 2018-2020 the original author or authors.
artembilan marked this conversation as resolved.
Show resolved Hide resolved
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package org.springframework.kafka.event;

/**
* An event published when a container is stopped.
*
* @author Gary Russell
* @since 2.2
artembilan marked this conversation as resolved.
Show resolved Hide resolved
*
*/
public class ConcurrentContainerStoppedEvent extends KafkaEvent {

private static final long serialVersionUID = 1L;

/**
* Construct an instance with the provided source and container.
* @param source the container instance that generated the event.
* @param container the container or the parent container if the container is a child.
* @since 2.2.1
*/
public ConcurrentContainerStoppedEvent(Object source, Object container) {
artembilan marked this conversation as resolved.
Show resolved Hide resolved
super(source, container);
}

@Override
public String toString() {
return "ContainerStoppedEvent [source=" + getSource() + "]";
artembilan marked this conversation as resolved.
Show resolved Hide resolved
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@
import org.springframework.core.log.LogAccessor;
import org.springframework.kafka.core.ConsumerFactory;
import org.springframework.kafka.core.KafkaAdmin;
import org.springframework.kafka.event.ConcurrentContainerStoppedEvent;
import org.springframework.kafka.event.ContainerStoppedEvent;
import org.springframework.kafka.support.KafkaHeaders;
import org.springframework.kafka.support.TopicPartitionOffset;
Expand Down Expand Up @@ -652,7 +653,13 @@ public void stop(Runnable callback) {

@Override
public void stopAbnormally(Runnable callback) {
doStop(callback, false);
this.lifecycleLock.lock();
try {
doStop(callback, false);
}
finally {
this.lifecycleLock.unlock();
}
publishContainerStoppedEvent();
}

Expand Down Expand Up @@ -704,6 +711,13 @@ protected void publishContainerStoppedEvent() {
}
}

protected void publishConcurrentContainerStoppedEvent() {
artembilan marked this conversation as resolved.
Show resolved Hide resolved
ApplicationEventPublisher eventPublisher = getApplicationEventPublisher();
if (eventPublisher != null) {
eventPublisher.publishEvent(new ConcurrentContainerStoppedEvent(this, parentOrThis()));
}
}

/**
* Return this or a parent container if this has a parent.
* @return the parent or this.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -202,15 +202,24 @@ public boolean isContainerPaused() {

@Override
public boolean isChildRunning() {
if (!isRunning()) {
return false;
}
for (MessageListenerContainer container : this.containers) {
if (container.isRunning()) {
this.lifecycleLock.lock();
try {
if(isRunning()) {
return true;
}
if(this.containers.isEmpty() && this.stoppedContainers.get() < this.concurrency) {
artembilan marked this conversation as resolved.
Show resolved Hide resolved
return true;
}
for (MessageListenerContainer container : this.containers) {
if (container.isRunning()) {
return true;
}
}
return false;
}
finally {
this.lifecycleLock.unlock();
}
return false;
}

@Override
Expand Down Expand Up @@ -379,9 +388,10 @@ public void childStopped(MessageListenerContainer child, Reason reason) {
if (this.reason == null || reason.equals(Reason.AUTH)) {
this.reason = reason;
}
int stoppedContainersCount = this.stoppedContainers.incrementAndGet();
if (Reason.AUTH.equals(this.reason)
&& getContainerProperties().isRestartAfterAuthExceptions()
&& this.concurrency == this.stoppedContainers.incrementAndGet()) {
&& this.concurrency == stoppedContainersCount) {

this.reason = null;
this.stoppedContainers.set(0);
Expand All @@ -392,6 +402,8 @@ && getContainerProperties().isRestartAfterAuthExceptions()
exec = new SimpleAsyncTaskExecutor(getListenerId() + ".authRestart");
}
exec.execute(this::start);
} else if (this.concurrency == stoppedContainersCount) {
artembilan marked this conversation as resolved.
Show resolved Hide resolved
publishConcurrentContainerStoppedEvent();
}
}

Expand Down
Loading