我正在使用Karaf(实际上是Aetos)OSGi容器。我想要两个使用ehcache的软件包。显然,当第二个包启动其缓存时,第一个包的缓存就会停止。
安装第一个时,没有问题。它运行良好。
当我安装第二个时,我得到一个像这样的堆栈跟踪:
2014-03-25 20:38:18,758 | ERROR | rint Extender: 2 | BlueprintContainerImpl | 9 - org.apache.aries.blueprint - 0.3.2 | Unable to start blueprint container for bundle DataProviderLoadFX
org.osgi.service.blueprint.container.ComponentDefinitionException: Unable to instantiate components
at org.apache.aries.blueprint.container.BlueprintContainerImpl.instantiateEagerComponents(BlueprintContainerImpl.java:635)[9:org.apache.aries.blueprint:0.3.2]
at org.apache.aries.blueprint.container.BlueprintContainerImpl.doRun(BlueprintContainerImpl.java:337)[9:org.apache.aries.blueprint:0.3.2]
at org.apache.aries.blueprint.container.BlueprintContainerImpl.run(BlueprintContainerImpl.java:230)[9:org.apache.aries.blueprint:0.3.2]
at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:439)[:1.6.0_41]
at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:303)[:1.6.0_41]
at java.util.concurrent.FutureTask.run(FutureTask.java:138)[:1.6.0_41]
at java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask.access$301(ScheduledThreadPoolExecutor.java:98)[:1.6.0_41]
at java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask.run(ScheduledThreadPoolExecutor.java:206)[:1.6.0_41]
at java.util.concurrent.ThreadPoolExecutor$Worker.runTask(ThreadPoolExecutor.java:895)[:1.6.0_41]
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:918)[:1.6.0_41]
at java.lang.Thread.run(Thread.java:662)[:1.6.0_41]
Caused by: java.lang.NullPointerException
at org.apache.aries.blueprint.container.BeanRecipe.setProperties(BeanRecipe.java:768)[9:org.apache.aries.blueprint:0.3.2]
at org.apache.aries.blueprint.container.BeanRecipe.internalCreate(BeanRecipe.java:722)[9:org.apache.aries.blueprint:0.3.2]
at org.apache.aries.blueprint.di.AbstractRecipe.create(AbstractRecipe.java:64)[9:org.apache.aries.blueprint:0.3.2]
at org.apache.aries.blueprint.container.BeanRecipe.setProperty(BeanRecipe.java:813)[9:org.apache.aries.blueprint:0.3.2]
at org.apache.aries.blueprint.container.BeanRecipe.setProperties(BeanRecipe.java:787)[9:org.apache.aries.blueprint:0.3.2]
at org.apache.aries.blueprint.container.BeanRecipe.setProperties(BeanRecipe.java:768)[9:org.apache.aries.blueprint:0.3.2]
at org.apache.aries.blueprint.container.BeanRecipe.internalCreate(BeanRecipe.java:722)[9:org.apache.aries.blueprint:0.3.2]
at org.apache.aries.blueprint.di.AbstractRecipe.create(AbstractRecipe.java:64)[9:org.apache.aries.blueprint:0.3.2]
at org.apache.aries.blueprint.container.BlueprintRepository.createInstances(BlueprintRepository.java:219)[9:org.apache.aries.blueprint:0.3.2]
at org.apache.aries.blueprint.container.BlueprintRepository.createAll(BlueprintRepository.java:147)[9:org.apache.aries.blueprint:0.3.2]
at org.apache.aries.blueprint.container.BlueprintContainerImpl.instantiateEagerComponents(BlueprintContainerImpl.java:631)[9:org.apache.aries.blueprint:0.3.2]
... 10 more
然后我更新第二个包,它启动就好了。但是,当我在第一个包上运行缓存时,缓存开始失败并显示此堆栈跟踪:
Caused by: org.apache.camel.language.bean.RuntimeBeanExpressionException: Failed to invoke method: handleFTPDeliveryRequest(${property.requestId},${property.duplicate}) on null due to: java.lang.IllegalStateException: The DRS_RequestServiceLoadSensitivity_jdbcOneHourToLiveCache Cache is not alive (STATUS_SHUTDOWN)
at org.apache.camel.language.bean.BeanExpression$OgnlInvokeProcessor.process(BeanExpression.java:285)
at org.apache.camel.language.bean.BeanExpression.evaluate(BeanExpression.java:114)
... 92 more
Caused by: java.lang.IllegalStateException: The DRS_RequestServiceLoadSensitivity_jdbcOneHourToLiveCache Cache is not alive (STATUS_SHUTDOWN)
at net.sf.ehcache.Cache$CacheStatus.checkAlive(Cache.java:4290)
at net.sf.ehcache.Cache.checkStatus(Cache.java:2731)
at net.sf.ehcache.Cache.get(Cache.java:1610)
at net.sf.ehcache.Cache.get(Cache.java:1591)
at com.windlogics.tsunami.drs.requestservice.LdfxDAO.checkDuplicaeFTPRequest(LdfxDAO.java:843)
at com.windlogics.tsunami.drs.requestservice.RequestProcessor.handleFTPDeliveryRequest(RequestProcessor.java:284)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:597)
at org.apache.camel.component.bean.MethodInfo.invoke(MethodInfo.java:341)
at org.apache.camel.component.bean.MethodInfo$1.proceed(MethodInfo.java:238)
at org.apache.camel.component.bean.BeanProcessor.process(BeanProcessor.java:166)
at org.apache.camel.util.AsyncProcessorHelper.process(AsyncProcessorHelper.java:99)
at org.apache.camel.component.bean.BeanProcessor.process(BeanProcessor.java:73)
at org.apache.camel.language.bean.BeanExpression$InvokeProcessor.process(BeanExpression.java:189)
at org.apache.camel.language.bean.BeanExpression$OgnlInvokeProcessor.process(BeanExpression.java:281)
... 93 more
ehcache配置文件位于捆绑包中。它们是相同的,除了ehcache和缓存的名称,文件名称本身甚至是不同的。
<ehcache xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="ehcache.xsd" updateCheck="true"
name="DRS_RequestServiceLoadSensitivity" monitoring="autodetect" dynamicConfig="true">
<diskStore path="java.io.tmpdir" />
<transactionManagerLookup class="net.sf.ehcache.transaction.manager.DefaultTransactionManagerLookup" properties="jndiName=java:/TransactionManager"
propertySeparator=";" />
<cacheManagerEventListenerFactory class="" properties="" />
<defaultCache maxEntriesLocalHeap="0" eternal="false" timeToIdleSeconds="1200" timeToLiveSeconds="1200">
</defaultCache>
<cache name="DRS_RequestServiceLoadSensitivity_jdbcOneHourToLiveCache" maxBytesLocalHeap="100M" maxBytesLocalDisk="200M" eternal="false"
diskSpoolBufferSizeMB="20" timeToLiveSeconds="3600" memoryStoreEvictionPolicy="LFU" transactionalMode="off">
</ehcache>
我使用以下bean作为工厂:
public class CacheManagerFactory {
private static Logger logger = Logger.getLogger(CacheManagerFactory.class);
private CacheManager cacheManager;
private String configFilename = "ehcache.xml";
private Bundle bundle;
public void afterPropertiesSet() {
InputStream in = null;
try {
URL url = bundle.getEntry(configFilename);
in = url.openStream();
cacheManager = CacheManager.create(in);
} catch (RuntimeException e) {
throw e;
} catch (Exception e) {
throw new RuntimeException(e);
} finally {
if (in != null) {
try {
in.close();
} catch (Exception e) {
logger.warn("", e);
}
}
}
}
public void dispose() {
cacheManager.shutdown();
}
public Cache getCache(String name) {
return cacheManager.getCache(name);
}
public CacheManager getCacheManager() {
return cacheManager;
}
public void setBundle(Bundle bundle) {
this.bundle = bundle;
}
public void setConfigFilename(String configFilename) {
this.configFilename = configFilename;
}
}
以下是我使用工厂的blueprint.xml文件的摘录:
<bean id="cacheManagerFactory" class="com.windlogics.tsunami.drs.dataprovider.CacheManagerFactory" init-method="afterPropertiesSet" destroy-method="dispose">
<property name="bundle" ref="blueprintBundle" />
<property name="configFilename" value="DRS_RequestServiceLoadSensitivity_ehcache.xml" />
</bean>
<bean id="dao" class="com.windlogics.tsunami.drs.dataprovider.LdfxDAO">
<property name="dataSource" ref="loadFXDataSource" />
<property name="cache">
<bean factory-ref="cacheManagerFactory" factory-method="getCache">
<argument value="DRS_RequestServiceLoadSensitivity_jdbcOneHourToLiveCache" />
</bean>
</property>
</bean>
我在pom.xml文件中有这个:
<dependency>
<groupId>org.apache.camel</groupId>
<artifactId>camel-cache</artifactId>
</dependency>