4242import io .undertow .util .PathMatcher ;
4343import org .springframework .context .annotation .Lazy ;
4444import io .undertow .util .StatusCodes ;
45+ import org .springframework .context .event .ContextClosedEvent ;
46+ import org .springframework .context .event .EventListener ;
4547import org .springframework .stereotype .Component ;
4648
4749import javax .inject .Inject ;
@@ -74,7 +76,9 @@ public class ProxyMappingManager {
7476 private PathHandler pathHandler ;
7577
7678 private final Map <String , String > mappings = new HashMap <>();
77-
79+
80+ private volatile boolean isShuttingDown = false ;
81+
7882 @ Inject
7983 @ Lazy
8084 private HeartbeatService heartbeatService ;
@@ -172,6 +176,11 @@ public void dispatchAsync(String proxyId, String mapping, HttpServletRequest req
172176 request .getRequestDispatcher (targetPath ).forward (request , response );
173177 }
174178
179+ @ EventListener (ContextClosedEvent .class )
180+ public void onApplicationEvent (ContextClosedEvent event ) {
181+ isShuttingDown = true ;
182+ }
183+
175184 private final DefaultResponseListener defaultResponseListener = responseExchange -> {
176185 if (!responseExchange .isResponseChannelAvailable ()) {
177186 return false ;
@@ -182,7 +191,7 @@ public void dispatchAsync(String proxyId, String mapping, HttpServletRequest req
182191 if (proxyIdAttachment != null ) {
183192 try {
184193 proxy = proxyService .getProxy (proxyIdAttachment .proxyId );
185- if (proxy != null && !proxy .getStatus ().isUnavailable ()) {
194+ if (proxy != null && !proxy .getStatus ().isUnavailable () && ! isShuttingDown ) {
186195 logger .info (proxy , "Proxy unreachable/crashed, stopping it now" );
187196 asyncProxyService .stopProxy (proxy , true );
188197 }
0 commit comments