Skip to content

Commit

Permalink
Bean leak (#11818)
Browse files Browse the repository at this point in the history
* CAMEL-20035: Added javadoc

* CAMEL-20035: camel-bean - Fix bean info cache gets populated per instance instead of per class. Only in special use-case use per instance.
  • Loading branch information
davsclaus committed Oct 24, 2023
1 parent ad9b93e commit 0eb0e21
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,9 @@ BeanInfo getBeanInfoFromCache(BeanInfoCacheKey key) {
}

void addBeanInfoToCache(BeanInfoCacheKey key, BeanInfo beanInfo) {
beanInfoCache.put(key, beanInfo);
if (beanInfo != null) {
beanInfoCache.put(key, beanInfo);
}
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -111,9 +111,13 @@ public BeanInfo(CamelContext camelContext, Class<?> type, Object instance, Metho
this.component = beanComponent;

final BeanInfoCacheKey key = new BeanInfoCacheKey(type, instance, explicitMethod);
final BeanInfoCacheKey key2 = instance != null ? new BeanInfoCacheKey(type, null, explicitMethod) : null;

// lookup if we have a bean info cache
BeanInfo beanInfo = component.getBeanInfoFromCache(key);
if (key2 != null && beanInfo == null) {
beanInfo = component.getBeanInfoFromCache(key2);
}
if (beanInfo != null) {
// copy the values from the cache we need
defaultMethod = beanInfo.defaultMethod;
Expand Down Expand Up @@ -158,8 +162,16 @@ public BeanInfo(CamelContext camelContext, Class<?> type, Object instance, Metho
operationsWithHandlerAnnotation = Collections.unmodifiableList(operationsWithHandlerAnnotation);
methodMap = Collections.unmodifiableMap(methodMap);

// add new bean info to cache
component.addBeanInfoToCache(key, this);
// key must be instance based for custom/handler annotations
boolean instanceBased = !operationsWithCustomAnnotation.isEmpty() || !operationsWithHandlerAnnotation.isEmpty();
if (instanceBased) {
// add new bean info to cache (instance based)
component.addBeanInfoToCache(key, this);
} else {
// add new bean info to cache (not instance based, favour key2 if possible)
BeanInfoCacheKey k = key2 != null ? key2 : key;
component.addBeanInfoToCache(k, this);
}
}

public Class<?> getType() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
import org.apache.camel.spi.BeanProcessorFactory;
import org.apache.camel.spi.annotations.JdkService;
import org.apache.camel.support.CamelContextHelper;
import org.apache.camel.support.service.ServiceHelper;
import org.apache.camel.support.service.ServiceSupport;
import org.apache.camel.util.ObjectHelper;
import org.slf4j.Logger;
Expand Down Expand Up @@ -205,6 +206,8 @@ public Processor createBeanProcessor(
@Override
protected void doInit() throws Exception {
parameterMappingStrategy = ParameterMappingStrategyHelper.createParameterMappingStrategy(getCamelContext());

beanComponent = getCamelContext().getComponent("bean", BeanComponent.class);
ServiceHelper.initService(beanComponent);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,18 +18,39 @@

import java.util.Map;

/**
* A least-recently-used cache.
*/
public interface LRUCache<K, V> extends Map<K, V> {

/**
* Clears the cache
*/
void cleanUp();

/**
* Reset usage statistics
*/
void resetStatistics();

/**
* Gets the number of evicted elements
*/
long getEvicted();

/**
* Gets the number of cache misses.
*/
long getMisses();

/**
* Gets the number of cache hits.
*/
long getHits();

/**
* Maximum cache capacity.
*/
int getMaxCacheSize();

}

0 comments on commit 0eb0e21

Please sign in to comment.