Skip to content

Commit

Permalink
CAMEL-12969: OSGi service registry should unget services later during…
Browse files Browse the repository at this point in the history
… 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.

Conflicts:
	components/camel-core-osgi/src/main/java/org/apache/camel/core/osgi/OsgiServiceRegistry.java
  • Loading branch information
davsclaus authored and oscerd committed Dec 11, 2018
1 parent 695b877 commit 6e717d0
Showing 1 changed file with 11 additions and 5 deletions.
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@


import org.apache.camel.CamelContext; import org.apache.camel.CamelContext;
import org.apache.camel.RuntimeCamelException; import org.apache.camel.RuntimeCamelException;
import org.apache.camel.Service;
import org.apache.camel.spi.Registry; import org.apache.camel.spi.Registry;
import org.apache.camel.support.LifecycleStrategySupport; import org.apache.camel.support.LifecycleStrategySupport;
import org.osgi.framework.BundleContext; import org.osgi.framework.BundleContext;
Expand All @@ -37,7 +38,7 @@
/** /**
* The OsgiServiceRegistry support to get the service object from the bundle context * 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 static final Logger LOG = LoggerFactory.getLogger(OsgiCamelContextHelper.class);
private final BundleContext bundleContext; private final BundleContext bundleContext;
private final Queue<ServiceReference<?>> serviceReferenceQueue = new ConcurrentLinkedQueue<>(); private final Queue<ServiceReference<?>> serviceReferenceQueue = new ConcurrentLinkedQueue<>();
Expand All @@ -51,7 +52,7 @@ public OsgiServiceRegistry(BundleContext bc) {
*/ */
public <T> T lookupByNameAndType(String name, Class<T> type) { public <T> T lookupByNameAndType(String name, Class<T> type) {
Object service = null; Object service = null;
ServiceReference<?> sr = null; ServiceReference<?> sr;
try { try {
ServiceReference<?>[] refs = bundleContext.getServiceReferences(type.getName(), "(name=" + name + ")"); ServiceReference<?>[] refs = bundleContext.getServiceReferences(type.getName(), "(name=" + name + ")");
if (refs != null && refs.length > 0) { if (refs != null && refs.length > 0) {
Expand Down Expand Up @@ -131,8 +132,14 @@ public <T> Set<T> findByType(Class<T> type) {
} }


@Override @Override
public void onContextStop(CamelContext context) { public void start() throws Exception {
// Unget the OSGi service // 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(); ServiceReference<?> sr = serviceReferenceQueue.poll();
while (sr != null) { while (sr != null) {
bundleContext.ungetService(sr); bundleContext.ungetService(sr);
Expand All @@ -141,5 +148,4 @@ public void onContextStop(CamelContext context) {
// Clean up the OSGi Service Cache // Clean up the OSGi Service Cache
serviceReferenceQueue.clear(); serviceReferenceQueue.clear();
} }

} }

0 comments on commit 6e717d0

Please sign in to comment.