Apache Camel Cache Configuration Is Not Working

1.5k Views Asked by At

I want to put non-serializable object to camel cache and get. In route configuration of cache I set objectCache parameter to true as seen below.

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

There is no problem in inserting to cache with following code.

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");

But while getting element from cache following exception thrown.

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)

While debugging CacheProducerClass.createElementFromBody method,I notice that objectCache parameter is set as false. All the other parameters are set as the defult values.(maxElementsInMemory=1000 and memoryStoreEvictionPolicy = MemoryStoreEvictionPolicy.LFU)

com.aaa.model.WebServiceResponse class is not Serializable.

Do you have any idea about this problem? Why cache parameters are not overridden with the values in cache definition?

1

There are 1 best solutions below

0
Sebastián Laborde On

I have the same issue, dont know why, when an element is added to cache, all parameters are reset to default values.

The solution i found as a workaround, consists to specify again all the parameters and its values, when you add an element.

For example if you change your code to the following, it should work.

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");