-
Notifications
You must be signed in to change notification settings - Fork 59
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Support api graceful shutdown (#2684)
- Loading branch information
Showing
4 changed files
with
105 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
100 changes: 100 additions & 0 deletions
100
api/src/main/java/com/epam/pipeline/app/GracefulShutdownConfiguration.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,100 @@ | ||
package com.epam.pipeline.app; | ||
|
||
import lombok.RequiredArgsConstructor; | ||
import lombok.extern.slf4j.Slf4j; | ||
import org.apache.catalina.connector.Connector; | ||
import org.apache.commons.lang3.StringUtils; | ||
import org.apache.commons.lang3.math.NumberUtils; | ||
import org.apache.coyote.ProtocolHandler; | ||
import org.apache.tomcat.util.threads.ThreadPoolExecutor; | ||
import org.springframework.beans.factory.annotation.Value; | ||
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty; | ||
import org.springframework.boot.context.embedded.EmbeddedServletContainerCustomizer; | ||
import org.springframework.boot.context.embedded.tomcat.TomcatConnectorCustomizer; | ||
import org.springframework.boot.context.embedded.tomcat.TomcatEmbeddedServletContainerFactory; | ||
import org.springframework.context.ApplicationListener; | ||
import org.springframework.context.annotation.Bean; | ||
import org.springframework.context.annotation.Configuration; | ||
import org.springframework.context.event.ContextClosedEvent; | ||
|
||
import java.util.Optional; | ||
import java.util.concurrent.TimeUnit; | ||
|
||
@Configuration | ||
@ConditionalOnProperty(value = "server.shutdown", havingValue = "graceful") | ||
public class GracefulShutdownConfiguration { | ||
|
||
@Bean | ||
public GracefulShutdownListener gracefulShutdownListener( | ||
@Value("${spring.lifecycle.timeout-per-shutdown-phase:30s}") | ||
final String gracefulShutdownTimeoutString) { | ||
final long gracefulShutdownTimeoutInSeconds = toSeconds(gracefulShutdownTimeoutString); | ||
return new GracefulShutdownListener(gracefulShutdownTimeoutInSeconds); | ||
} | ||
|
||
private int toSeconds(final String timeoutString) { | ||
return NumberUtils.toInt(StringUtils.removeEnd(timeoutString, "s")); | ||
} | ||
|
||
@Bean | ||
public EmbeddedServletContainerCustomizer embeddedServletContainerCustomizer( | ||
final GracefulShutdownListener gracefulShutdownListener) { | ||
return container -> Optional.of(container) | ||
.filter(TomcatEmbeddedServletContainerFactory.class::isInstance) | ||
.map(TomcatEmbeddedServletContainerFactory.class::cast) | ||
.ifPresent(factory -> factory.addConnectorCustomizers(gracefulShutdownListener)); | ||
} | ||
|
||
@Slf4j | ||
@RequiredArgsConstructor | ||
private static class GracefulShutdownListener implements TomcatConnectorCustomizer, | ||
ApplicationListener<ContextClosedEvent> { | ||
|
||
private final long timeoutSeconds; | ||
|
||
private volatile Connector connector; | ||
|
||
@Override | ||
public void customize(final Connector connector) { | ||
this.connector = connector; | ||
} | ||
|
||
@Override | ||
public void onApplicationEvent(final ContextClosedEvent event) { | ||
log.info("Gracefully shutting down application..."); | ||
|
||
log.debug("Resetting new connections..."); | ||
connector.pause(); | ||
|
||
log.debug("Terminating connections pool..."); | ||
getThreadPoolExecutor(connector).ifPresent(this::terminate); | ||
|
||
log.debug("Proceeding with application shutting down..."); | ||
} | ||
|
||
private Optional<ThreadPoolExecutor> getThreadPoolExecutor(final Connector connector) { | ||
return Optional.of(connector) | ||
.map(Connector::getProtocolHandler) | ||
.map(ProtocolHandler::getExecutor) | ||
.filter(ThreadPoolExecutor.class::isInstance) | ||
.map(ThreadPoolExecutor.class::cast); | ||
} | ||
|
||
private void terminate(final ThreadPoolExecutor executor) { | ||
try { | ||
executor.shutdown(); | ||
log.debug("Waiting for connections pool to terminate..."); | ||
if (executor.awaitTermination(timeoutSeconds, TimeUnit.SECONDS)) { | ||
log.debug("Connections pool has been terminated successfully."); | ||
} else { | ||
log.warn("Connections pool has not been terminated after %s seconds. " + | ||
"Proceeding with forceful connections shutting down..."); | ||
} | ||
} catch (InterruptedException e) { | ||
log.warn("Connections pool termination has been interrupted. " + | ||
"Proceeding with forceful connections shutting down..."); | ||
Thread.currentThread().interrupt(); | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters