Author: ningjiang
Date: Mon Aug 22 05:45:04 2011
New Revision: 1160113
URL: http://svn.apache.org/viewvc?rev=1160113&view=rev
Log:
CAMEL-4355 Should use LRUSoftCache and start/stop the cache according to the
producers lifecycle
Modified:
camel/trunk/components/camel-cxf/src/main/java/org/apache/camel/component/cxf/jaxrs/CxfRsProducer.java
Modified:
camel/trunk/components/camel-cxf/src/main/java/org/apache/camel/component/cxf/jaxrs/CxfRsProducer.java
URL:
http://svn.apache.org/viewvc/camel/trunk/components/camel-cxf/src/main/java/org/apache/camel/component/cxf/jaxrs/CxfRsProducer.java?rev=1160113&r1=1160112&r2=1160113&view=diff
==============================================================================
---
camel/trunk/components/camel-cxf/src/main/java/org/apache/camel/component/cxf/jaxrs/CxfRsProducer.java
(original)
+++
camel/trunk/components/camel-cxf/src/main/java/org/apache/camel/component/cxf/jaxrs/CxfRsProducer.java
Mon Aug 22 05:45:04 2011
@@ -39,6 +39,7 @@ import org.apache.camel.component.cxf.co
import org.apache.camel.converter.IOConverter;
import org.apache.camel.impl.DefaultProducer;
import org.apache.camel.util.LRUCache;
+import org.apache.camel.util.LRUSoftCache;
import org.apache.cxf.jaxrs.JAXRSServiceFactoryBean;
import org.apache.cxf.jaxrs.client.Client;
import org.apache.cxf.jaxrs.client.JAXRSClientFactoryBean;
@@ -66,6 +67,16 @@ public class CxfRsProducer extends Defau
this.throwException = endpoint.isThrowExceptionOnFailure();
clientFactoryBeanCache = new
ClientFactoryBeanCache(endpoint.getMaxClientCacheSize());
}
+
+ protected void doStart() throws Exception {
+ clientFactoryBeanCache.start();
+ super.doStart();
+ }
+
+ protected void doStop() throws Exception {
+ super.doStop();
+ clientFactoryBeanCache.stop();
+ }
public void process(Exchange exchange) throws Exception {
Message inMessage = exchange.getIn();
@@ -322,25 +333,29 @@ public class CxfRsProducer extends Defau
* Cache contains {@link
org.apache.cxf.jaxrs.client.JAXRSClientFactoryBean}
*/
private class ClientFactoryBeanCache {
- private LRUCache<String, SoftReference<JAXRSClientFactoryBean>> cache;
+ private LRUSoftCache<String, JAXRSClientFactoryBean> cache;
public ClientFactoryBeanCache(final int maxCacheSize) {
- this.cache = new LRUCache<String,
SoftReference<JAXRSClientFactoryBean>>(maxCacheSize);
+ this.cache = new LRUSoftCache<String,
JAXRSClientFactoryBean>(maxCacheSize);
+ }
+
+ public void start() throws Exception {
+ this.cache.start();
+ }
+
+ public void stop() throws Exception {
+ this.cache.stop();
}
public JAXRSClientFactoryBean get(String address) throws Exception {
- JAXRSClientFactoryBean retval = null;
+ JAXRSClientFactoryBean retVal = null;
synchronized (cache) {
- SoftReference<JAXRSClientFactoryBean> ref = cache.get(address);
+ retVal = cache.get(address);
- if (ref != null) {
- retval = ref.get();
- }
-
- if (retval == null) {
- retval =
((CxfRsEndpoint)getEndpoint()).createJAXRSClientFactoryBean(address);
+ if (retVal == null) {
+ retVal =
((CxfRsEndpoint)getEndpoint()).createJAXRSClientFactoryBean(address);
- cache.put(address, new
SoftReference<JAXRSClientFactoryBean>(retval));
+ cache.put(address, retVal);
LOG.trace("Created client factory bean and add to cache
for address '{}'", address);
@@ -348,7 +363,7 @@ public class CxfRsProducer extends Defau
LOG.trace("Retrieved client factory bean from cache for
address '{}'", address);
}
}
- return retval;
+ return retVal;
}
}
}