This is an automated email from the ASF dual-hosted git repository. ffang pushed a commit to branch camel-3.18.x in repository https://gitbox.apache.org/repos/asf/camel.git
The following commit(s) were added to refs/heads/camel-3.18.x by this push: new 4e6f618932a [CAMEL-18528]ensure CXF SpringBus honor camel graceful shutdown 4e6f618932a is described below commit 4e6f618932a5fd386454f7b712ed5f2f88baa42f Author: Freeman Fang <freeman.f...@gmail.com> AuthorDate: Mon Sep 19 13:13:18 2022 -0400 [CAMEL-18528]ensure CXF SpringBus honor camel graceful shutdown (cherry picked from commit d8318bd5337788a923d340716d4b1eda326f6fd9) --- .../cxf/spring/jaxws/CxfSpringEndpoint.java | 65 ++++++++++++++++++++++ 1 file changed, 65 insertions(+) diff --git a/components/camel-cxf/camel-cxf-spring-soap/src/main/java/org/apache/camel/component/cxf/spring/jaxws/CxfSpringEndpoint.java b/components/camel-cxf/camel-cxf-spring-soap/src/main/java/org/apache/camel/component/cxf/spring/jaxws/CxfSpringEndpoint.java index 2430a97fc04..ea169aa8422 100644 --- a/components/camel-cxf/camel-cxf-spring-soap/src/main/java/org/apache/camel/component/cxf/spring/jaxws/CxfSpringEndpoint.java +++ b/components/camel-cxf/camel-cxf-spring-soap/src/main/java/org/apache/camel/component/cxf/spring/jaxws/CxfSpringEndpoint.java @@ -25,11 +25,15 @@ import org.apache.camel.component.cxf.common.message.CxfConstants; import org.apache.camel.component.cxf.jaxws.CxfComponent; import org.apache.camel.component.cxf.jaxws.CxfEndpoint; import org.apache.camel.component.cxf.jaxws.WSDLServiceFactoryBean; +import org.apache.camel.impl.DefaultCamelContext; +import org.apache.camel.spi.ShutdownStrategy; import org.apache.camel.spring.SpringCamelContext; import org.apache.camel.util.ObjectHelper; import org.apache.cxf.Bus; +import org.apache.cxf.Bus.BusState; import org.apache.cxf.BusFactory; import org.apache.cxf.bus.spring.BusWiringBeanFactoryPostProcessor; +import org.apache.cxf.bus.spring.SpringBus; import org.apache.cxf.bus.spring.SpringBusFactory; import org.apache.cxf.endpoint.Client; import org.apache.cxf.frontend.ClientFactoryBean; @@ -40,6 +44,11 @@ import org.slf4j.LoggerFactory; import org.springframework.beans.BeansException; import org.springframework.context.ApplicationContext; import org.springframework.context.ApplicationContextAware; +import org.springframework.context.ApplicationEvent; +import org.springframework.context.ApplicationListener; +import org.springframework.context.event.ApplicationEventMulticaster; +import org.springframework.context.event.ContextClosedEvent; +import org.springframework.context.support.AbstractApplicationContext; /** * Defines the <a href="http://camel.apache.org/cxf.html">CXF Endpoint</a> @@ -273,6 +282,7 @@ public class CxfSpringEndpoint extends CxfEndpoint implements ApplicationContext public Bus getBus() { if (bus == null) { bus = createBus(getCamelContext()); + enableSpringBusShutdownGracefully(bus); this.createBus = true; LOG.debug("Using DefaultBus {}", bus); } @@ -295,4 +305,59 @@ public class CxfSpringEndpoint extends CxfEndpoint implements ApplicationContext return busFactory.createBus(); } + @SuppressWarnings("rawtypes") + private void enableSpringBusShutdownGracefully(Bus bus) { + if (bus instanceof SpringBus + && applicationContext instanceof AbstractApplicationContext) { + SpringBus springBus = (SpringBus) bus; + AbstractApplicationContext abstractApplicationContext + = (AbstractApplicationContext) applicationContext; + ApplicationListener cxfSpringBusListener = null; + for (ApplicationListener listener : abstractApplicationContext.getApplicationListeners()) { + + if (listener.getClass().getName().indexOf("org.apache.cxf.bus.spring.SpringBus") >= 0) { + cxfSpringBusListener = listener; + } + } + ApplicationEventMulticaster aem = applicationContext + .getBean(AbstractApplicationContext.APPLICATION_EVENT_MULTICASTER_BEAN_NAME, + ApplicationEventMulticaster.class); + aem.removeApplicationListener(cxfSpringBusListener); + ApplicationListener listener = new ApplicationListener() { + public void onApplicationEvent(final ApplicationEvent event) { + new Thread() { + public void run() { + if (event instanceof ContextClosedEvent && bus.getState() == BusState.RUNNING) { + + try { + boolean done = false; + ShutdownStrategy shutdownStrategy = ((DefaultCamelContext) getCamelContext()) + .getShutdownStrategy(); + while (!done && !shutdownStrategy.hasTimeoutOccurred()) { + int inflight = getCamelContext().getInflightRepository().size(); + if (inflight != 0) { + Thread.sleep(1000); + } else { + done = true; + } + } + + } catch (Exception e) { + + LOG.debug("Error when enabling SpringBus shutdown gracefully", e); + } + springBus.onApplicationEvent(event); + } else { + springBus.onApplicationEvent(event); + } + } + }.start(); + + } + }; + abstractApplicationContext.addApplicationListener(listener); + } + + } + }