gpt4 book ai didi

java - Apache Camel 缓存配置不起作用

转载 作者:行者123 更新时间:2023-12-02 03:35:35 25 4
gpt4 key购买 nike

我想将不可序列化的对象放入 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。所有其他参数均设置为默认值。(maxElementsInMemory=1000 且 memoryStoreEvictionPolicy = MemoryStoreEvictionPolicy.LFU)

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

您对这个问题有什么想法吗?为什么缓存参数没有被缓存定义中的值覆盖?

最佳答案

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

我找到的解决方法是在添加元素时再次指定所有参数及其值。

例如,如果您将代码更改为以下内容,它应该可以工作。

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

关于java - Apache Camel 缓存配置不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37488705/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com