Apache Camel缓存配置不起作用

时间:2016-05-27 16:50:43

标签: java caching apache-camel objectcache

我想将非序列化对象放入camel缓存并获取。 在缓存的路由配置中,我将objectCache参数设置为true,如下所示。

 from("cache://MyCache"
           + "?maxElementsInMemory=5" +
            "&memoryStoreEvictionPolicy=" +
            "MemoryStoreEvictionPolicy.FIFO" +
            "&overflowToDisk=false" +
            "&timeToLiveSeconds=300" +
            "&objectCache=true" +
            "&diskExpiryThreadIntervalSeconds=30").to("mock:endpoint");

使用以下代码插入缓存没有问题。

from("direct:addToCache").process(new Processor() {
            @Override
            public void process(Exchange exchange) throws Exception {
                MyObject ad = (MyObject) exchange.getIn().getBody();
                String key= ad.getId();
                WebServiceResponse value= ad.getWebServiceResponse();
                exchange.getIn().setHeader(CacheConstants.CACHE_OPERATION, CacheConstants.CACHE_OPERATION_ADD);
                exchange.getIn().setHeader(CacheConstants.CACHE_KEY, key);
                exchange.getIn().setBody(value);
            }
        }).to("cache://MyCache");

但是在抛出异常后从缓存中获取元素。

org.apache.camel.NoTypeConversionAvailableException: No type converter available to convert from type: com.aaa.model.WebServiceResponse to the required type: java.io.InputStream with value com.aaa.model.WebServiceResponse@5781f3d4
 at org.apache.camel.impl.converter.BaseTypeConverterRegistry.mandatoryConvertTo(BaseTypeConverterRegistry.java:185)
 at org.apache.camel.impl.converter.BaseTypeConverterRegistry.mandatoryConvertTo(BaseTypeConverterRegistry.java:156)
 at org.apache.camel.component.cache.CacheProducer.createElementFromBody(CacheProducer.java:132)
 at org.apache.camel.component.cache.CacheProducer.performCacheOperation(CacheProducer.java:82)
 at org.apache.camel.component.cache.CacheProducer.process(CacheProducer.java:71)
 at org.apache.camel.util.AsyncProcessorConverterHelper$ProcessorToAsyncProcessorBridge.process(AsyncProcessorConverterHelper.java:61)
 at org.apache.camel.processor.SendProcessor.process(SendProcessor.java:141)
 at org.apache.camel.management.InstrumentationProcessor.process(InstrumentationProcessor.java:77)
 at org.apache.camel.processor.RedeliveryErrorHandler.process(RedeliveryErrorHandler.java:460)
 at org.apache.camel.processor.CamelInternalProcessor.process(CamelInternalProcessor.java:190)
 at org.apache.camel.processor.Pipeline.process(Pipeline.java:121)
 at org.apache.camel.processor.Pipeline.process(Pipeline.java:83)
 at org.apache.camel.processor.CamelInternalProcessor.process(CamelInternalProcessor.java:190)
 at org.apache.camel.component.direct.DirectProducer.process(DirectProducer.java:62)
 at org.apache.camel.processor.CamelInternalProcessor.process(CamelInternalProcessor.java:190)
 at org.apache.camel.util.AsyncProcessorHelper.process(AsyncProcessorHelper.java:109)
 at org.apache.camel.processor.UnitOfWorkProducer.process(UnitOfWorkProducer.java:68)
 at org.apache.camel.impl.ProducerCache$2.doInProducer(ProducerCache.java:412)
 at org.apache.camel.impl.ProducerCache$2.doInProducer(ProducerCache.java:380)
 at org.apache.camel.impl.ProducerCache.doInProducer(ProducerCache.java:270)
 at org.apache.camel.impl.ProducerCache.sendExchange(ProducerCache.java:380)

在调试CacheProducerClass.createElementFromBody方法时,我注意到objectCache参数设置为false。所有其他参数都设置为defult值。(maxElementsInMemory = 1000和memoryStoreEvictionPolicy = MemoryStoreEvictionPolicy.LFU)

com.aaa.model.WebServiceResponse类不可序列化。

你对这个问题有什么看法吗? 为什么缓存参数不会被缓存定义中的值覆盖?

1 个答案:

答案 0 :(得分:1)

我有同样的问题,不知道为什么,当一个元素被添加到缓存时,所有参数都被重置为默认值。

我发现作为变通方法的解决方案包括在添加元素时再次指定所有参数及其值。

例如,如果您将代码更改为以下代码,则应该可以正常工作。

from("direct:addToCache").process(new Processor() {
            @Override
            public void process(Exchange exchange) throws Exception {
                MyObject ad = (MyObject) exchange.getIn().getBody();
                String key= ad.getId();
                WebServiceResponse value= ad.getWebServiceResponse();
                exchange.getIn().setHeader(CacheConstants.CACHE_OPERATION, CacheConstants.CACHE_OPERATION_ADD);
                exchange.getIn().setHeader(CacheConstants.CACHE_KEY, key);
                exchange.getIn().setBody(value);
            }
        }).to("cache://MyCache"
           + "?maxElementsInMemory=5" +
            "&memoryStoreEvictionPolicy=" +
            "MemoryStoreEvictionPolicy.FIFO" +
            "&overflowToDisk=false" +
            "&timeToLiveSeconds=300" +
            "&objectCache=true" +
            "&diskExpiryThreadIntervalSeconds=30").to("mock:endpoint");