This is an automated email from the ASF dual-hosted git repository. davsclaus pushed a commit to branch camel-2.23.x in repository https://gitbox.apache.org/repos/asf/camel.git
commit 28c4fdb03ef77f33b92c67b5777d9a5282ac6115 Author: Claus Ibsen <claus.ib...@gmail.com> AuthorDate: Tue Dec 11 15:20:56 2018 +0100 CAMEL-12969: OSGi service registry should unget services later during shutting down CamelContext which we can do in the start/stop service API instead. This reduces leaks if a service is get udring shutdown as now the lifecycle/osgi service registry is stopped as last action when camel context is shutting down. --- .../org/apache/camel/core/osgi/OsgiServiceRegistry.java | 17 +++++++++++------ 1 file changed, 11 insertions(+), 6 deletions(-) diff --git a/components/camel-core-osgi/src/main/java/org/apache/camel/core/osgi/OsgiServiceRegistry.java b/components/camel-core-osgi/src/main/java/org/apache/camel/core/osgi/OsgiServiceRegistry.java index 4569962..ce24bb0 100644 --- a/components/camel-core-osgi/src/main/java/org/apache/camel/core/osgi/OsgiServiceRegistry.java +++ b/components/camel-core-osgi/src/main/java/org/apache/camel/core/osgi/OsgiServiceRegistry.java @@ -23,7 +23,7 @@ import java.util.Queue; import java.util.Set; import java.util.concurrent.ConcurrentLinkedQueue; -import org.apache.camel.CamelContext; +import org.apache.camel.Service; import org.apache.camel.spi.Registry; import org.apache.camel.support.LifecycleStrategySupport; import org.apache.camel.util.ObjectHelper; @@ -37,7 +37,7 @@ import org.slf4j.LoggerFactory; /** * The OsgiServiceRegistry support to get the service object from the bundle context */ -public class OsgiServiceRegistry extends LifecycleStrategySupport implements Registry { +public class OsgiServiceRegistry extends LifecycleStrategySupport implements Registry, Service { private static final Logger LOG = LoggerFactory.getLogger(OsgiCamelContextHelper.class); private final BundleContext bundleContext; private final Queue<ServiceReference<?>> serviceReferenceQueue = new ConcurrentLinkedQueue<>(); @@ -51,7 +51,7 @@ public class OsgiServiceRegistry extends LifecycleStrategySupport implements Reg */ public <T> T lookupByNameAndType(String name, Class<T> type) { Object service = null; - ServiceReference<?> sr = null; + ServiceReference<?> sr; try { ServiceReference<?>[] refs = bundleContext.getServiceReferences(type.getName(), "(name=" + name + ")"); if (refs != null && refs.length > 0) { @@ -143,8 +143,14 @@ public class OsgiServiceRegistry extends LifecycleStrategySupport implements Reg } @Override - public void onContextStop(CamelContext context) { - // Unget the OSGi service + public void start() throws Exception { + // noop + } + + @Override + public void stop() throws Exception { + // Unget the OSGi service as OSGi uses reference counting + // and we should do this as one of the last actions when stopping Camel ServiceReference<?> sr = serviceReferenceQueue.poll(); while (sr != null) { bundleContext.ungetService(sr); @@ -153,5 +159,4 @@ public class OsgiServiceRegistry extends LifecycleStrategySupport implements Reg // Clean up the OSGi Service Cache serviceReferenceQueue.clear(); } - }