当前位置: 首页 > 知识库问答 >
问题:

Ehcache在移动到Spring Boot2时引发NotSerializableException

巴英韶
2023-03-14

我正在将现有的spring Boot1.5应用程序升级到spring Boot2.0。该应用程序还使用了Spring集成xmls。Ehcache组件在尝试执行此操作时导致问题。当尝试将GET调用的响应添加到缓存时,它引发了一个异常:

NotSerializableException:org.springframework.integration.support.MessageBuilder

在Spring Boot1.5中,它工作得非常好,并且插入缓存中的值(XML响应)的类型是GenericMessage。下面是整个堆栈跟踪:

java.io.NotSerializableException: org.springframework.integration.support.MessageBuilder
at java.io.ObjectOutputStream.writeObject0(ObjectOutputStream.java:1184)
at java.io.ObjectOutputStream.writeObject(ObjectOutputStream.java:348)
at net.sf.ehcache.store.compound.ReadWriteSerializationCopyStrategy.copyForWrite(ReadWriteSerializationCopyStrategy.java:58)
at net.sf.ehcache.store.compound.ReadWriteSerializationCopyStrategy.copyForWrite(ReadWriteSerializationCopyStrategy.java:35)
at net.sf.ehcache.store.CopyStrategyHandler.copyElementForWriteIfNeeded(CopyStrategyHandler.java:84)
at net.sf.ehcache.store.AbstractCopyingCacheStore.put(AbstractCopyingCacheStore.java:78)
at net.sf.ehcache.store.CopyingCacheStore.put(CopyingCacheStore.java:29)
at net.sf.ehcache.Cache.putInternal(Cache.java:1616)
at net.sf.ehcache.Cache.put(Cache.java:1542)
at net.sf.ehcache.Cache.put(Cache.java:1507)
at org.springframework.cache.ehcache.EhCacheCache.put(EhCacheCache.java:128)
at org.springframework.cache.interceptor.AbstractCacheInvoker.doPut(AbstractCacheInvoker.java:87)
at org.springframework.cache.interceptor.CacheAspectSupport$CachePutRequest.apply(CacheAspectSupport.java:783)
at org.springframework.cache.interceptor.CacheAspectSupport.execute(CacheAspectSupport.java:400)
at org.springframework.cache.interceptor.CacheAspectSupport.execute(CacheAspectSupport.java:316)
at org.springframework.cache.interceptor.CacheInterceptor.invoke(CacheInterceptor.java:61)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:185)
at org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:212)
at com.sun.proxy.$Proxy96.handleRequestMessage(Unknown Source)
at org.springframework.integration.handler.AbstractReplyProducingMessageHandler.doInvokeAdvisedRequestHandler(AbstractReplyProducingMessageHandler.java:127)
at org.springframework.integration.handler.AbstractReplyProducingMessageHandler.handleMessageInternal(AbstractReplyProducingMessageHandler.java:112)
at org.springframework.integration.handler.AbstractMessageHandler.handleMessage(AbstractMessageHandler.java:158)
at org.springframework.integration.dispatcher.AbstractDispatcher.tryOptimizedDispatch(AbstractDispatcher.java:116)
at org.springframework.integration.dispatcher.UnicastingDispatcher.doDispatch(UnicastingDispatcher.java:132)
at org.springframework.integration.dispatcher.UnicastingDispatcher.dispatch(UnicastingDispatcher.java:105)
at org.springframework.integration.channel.AbstractSubscribableChannel.doSend(AbstractSubscribableChannel.java:73)
at org.springframework.integration.channel.AbstractMessageChannel.send(AbstractMessageChannel.java:445)
at org.springframework.integration.channel.AbstractMessageChannel.send(AbstractMessageChannel.java:394)
at org.springframework.messaging.core.GenericMessagingTemplate.doSend(GenericMessagingTemplate.java:181)
at org.springframework.messaging.core.GenericMessagingTemplate.doSend(GenericMessagingTemplate.java:160)
at org.springframework.messaging.core.GenericMessagingTemplate.doSend(GenericMessagingTemplate.java:47)
at org.springframework.messaging.core.AbstractMessageSendingTemplate.send(AbstractMessageSendingTemplate.java:108)
at org.springframework.integration.handler.AbstractMessageProducingHandler.sendOutput(AbstractMessageProducingHandler.java:426)
at org.springframework.integration.handler.AbstractMessageProducingHandler.produceOutput(AbstractMessageProducingHandler.java:336)
at org.springframework.integration.handler.AbstractMessageProducingHandler.sendOutputs(AbstractMessageProducingHandler.java:227)
at org.springframework.integration.handler.AbstractReplyProducingMessageHandler.handleMessageInternal(AbstractReplyProducingMessageHandler.java:115)
at org.springframework.integration.handler.AbstractMessageHandler.handleMessage(AbstractMessageHandler.java:158)
at org.springframework.integration.dispatcher.AbstractDispatcher.tryOptimizedDispatch(AbstractDispatcher.java:116)
at org.springframework.integration.dispatcher.UnicastingDispatcher.doDispatch(UnicastingDispatcher.java:132)
at org.springframework.integration.dispatcher.UnicastingDispatcher.dispatch(UnicastingDispatcher.java:105)
at org.springframework.integration.channel.AbstractSubscribableChannel.doSend(AbstractSubscribableChannel.java:73)
at org.springframework.integration.channel.AbstractMessageChannel.send(AbstractMessageChannel.java:445)
at org.springframework.messaging.core.GenericMessagingTemplate.doSend(GenericMessagingTemplate.java:181)
at org.springframework.messaging.core.GenericMessagingTemplate.doSendAndReceive(GenericMessagingTemplate.java:227)
at org.springframework.messaging.core.GenericMessagingTemplate.doSendAndReceive(GenericMessagingTemplate.java:47)
at org.springframework.messaging.core.AbstractMessagingTemplate.sendAndReceive(AbstractMessagingTemplate.java:45)
at org.springframework.integration.core.MessagingTemplate.sendAndReceive(MessagingTemplate.java:97)
at org.springframework.integration.gateway.MessagingGatewaySupport.doSendAndReceive(MessagingGatewaySupport.java:497)
at org.springframework.integration.gateway.MessagingGatewaySupport.sendAndReceiveMessage(MessagingGatewaySupport.java:465)
at org.springframework.integration.http.inbound.HttpRequestHandlingEndpointSupport.actualDoHandleRequest(HttpRequestHandlingEndpointSupport.java:363)
at org.springframework.integration.http.inbound.HttpRequestHandlingEndpointSupport.doHandleRequest(HttpRequestHandlingEndpointSupport.java:255)
at org.springframework.integration.http.inbound.HttpRequestHandlingMessagingGateway.handleRequest(HttpRequestHandlingMessagingGateway.java:105)
at org.springframework.web.servlet.mvc.HttpRequestHandlerAdapter.handle(HttpRequestHandlerAdapter.java:53)
at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:991)
at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:925)
at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:974)
at org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:866)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:635)
at org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:851)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:742)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:231)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)
at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)
at org.springframework.boot.actuate.web.trace.servlet.HttpTraceFilter.doFilterInternal(HttpTraceFilter.java:90)
at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)
at org.springframework.web.filter.RequestContextFilter.doFilterInternal(RequestContextFilter.java:99)
at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)
at org.springframework.web.filter.HttpPutFormContentFilter.doFilterInternal(HttpPutFormContentFilter.java:109)
at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)
at org.springframework.web.filter.HiddenHttpMethodFilter.doFilterInternal(HiddenHttpMethodFilter.java:93)
at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)
at org.springframework.boot.actuate.metrics.web.servlet.WebMvcMetricsFilter.filterAndRecordMetrics(WebMvcMetricsFilter.java:155)
at org.springframework.boot.actuate.metrics.web.servlet.WebMvcMetricsFilter.filterAndRecordMetrics(WebMvcMetricsFilter.java:123)
at org.springframework.boot.actuate.metrics.web.servlet.WebMvcMetricsFilter.doFilterInternal(WebMvcMetricsFilter.java:108)
at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)
at org.springframework.web.filter.CharacterEncodingFilter.doFilterInternal(CharacterEncodingFilter.java:200)
at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:198)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:96)
at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:493)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:140)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:81)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:87)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:342)
at org.apache.coyote.http11.Http11Processor.service(Http11Processor.java:800)
at org.apache.coyote.AbstractProcessorLight.process(AbstractProcessorLight.java:66)
at org.apache.coyote.AbstractProtocol$ConnectionHandler.process(AbstractProtocol.java:806)
at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1498)
at org.apache.tomcat.util.net.SocketProcessorBase.run(SocketProcessorBase.java:49)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624)
at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)
at java.lang.Thread.run(Thread.java:748)

我需要显式强制转换响应以使其工作吗?

    private MethodInterceptor createGatewayInterceptor(String cacheName) {
    CacheInterceptor interceptor = new CacheInterceptor();
    interceptor.setCacheManager(cacheManager());
    NameMatchCacheOperationSource source = new NameMatchCacheOperationSource();
    CacheableOperation.Builder operationBuilder = new CacheableOperation.Builder();
    operationBuilder.setCacheName(cacheName);
    source.addCacheMethod("handle*Message", Collections.singleton(operationBuilder.build()));
    interceptor.setCacheOperationSources(source);
    interceptor.setKeyGenerator((target, method, params) -> ((Message) params[0]).getHeaders().get(HEADER_CACHEKEY));
    return interceptor;
}

DSL缓存实现:

    adviceChain.add(eternalCacheAdvice);
    adviceChain.add(genericMessageAdvice);
    handler.setAdviceChain(adviceChain);
    return handler;

@Bean
public MethodInterceptor cacheAdvice() {
    return createGatewayInterceptor(TWENTY_FOUR_HOUR_CACHE);
}

@Bean
public MethodInterceptor eternalCacheAdvice() {
    return createGatewayInterceptor(ETERNAL_CACHE);
}

@Bean
public MethodInterceptor genericMessageAdvice(){
    return invocation -> ((MessageBuilder<?>) invocation.proceed()).build();
}
@Bean
public MethodInterceptor genericXMLMessageAdvice(){
    return invocation -> ((MessageBuilder<?>) invocation.proceed()).build();
}

XML通知链:

        <int-http:request-handler-advice-chain>
            <ref bean="genericXMLMessageAdvice" />
            <ref bean="cacheAdvice" />
        </int-http:request-handler-advice-chain>

共有1个答案

费承载
2023-03-14

对于某些内部优化,许多开箱即用的Spring集成组件现在在生成回复之前返回messageBuilder。这是用额外的头来丰富目标回复消息所需要的。

类似于CacheInterceptor执行调用。proceed()ad尝试将结果原样用于缓存。

考虑在已配置的CacheInterceptor之前再添加一个MethodInterceptor,其简单实现如下所示:

invocation -> ((MessageBuilder<?>) invocation.proceed()).build()

因此,您的CacheInterceptor将像以前一样获得预期的GenericMessage<?>

顺便提一下,我们有一个关于缓存这个Spring集成的PR:https://github.com/spring-projects/spring-integration/pull/2105。请随意在那里分享您的经验,这样可能会推动一个开箱即用的支持。

 类似资料:
  • 我正在尝试将我的项目的Ehcache从2.6升级到3.0版本。 任何net的替代品。旧金山。ehcache。元素和CacheExceptionHandler。 关于Ehcache 3的文档更少,有谁能给我们一些将Ehcache升级到版本3的提示吗。

  • 我想开发一个带有div和一些样式的文件拖放组件: dragenter:更改div边框颜色 dragleave:还原为原始div边框颜色 在Firefox 35(Ubuntu)上,当div有一个滚动条(水平或垂直)并且在滚动条中移动文件时,我会发生以下事件: < li>dragenter:移过div边框 < li>dragenter:在滚动条上移动 < li>dragleave:离开滚动条 所以我在

  • 我想在我的Spring Boot应用程序启动时加载缓存中的数据。我知道在Ehcache2中使用BootstrapCacheLoader有一种集成的方法。如何在应用程序启动时将数据从数据库加载到Ehcache,但我在Ehcache3中没有看到这一点。我仍然可以在@postConstruct方法中手动完成。但我想知道是否有集成解决方案(Spring 5,Ehcache 3) 谢谢你。

  • 以前一切正常,现在当我运行我的应用程序时,它开始产生这个错误,我的应用程序一直在崩溃。 此错误不断出现 android.content.Intentandroid.content.ntent.putExtra(java.lang.String, int)'对空对象引用 这是我的飞溅活动代码。 Oncreate方法代码如下 @重写受保护的void onCreate(Bundle savedInsta

  • 当我们在视频通话时,我有问题建立屏幕录制功能。我是说我想同时开始录屏和视频通话。尝试将https://www.simplifiedcoding.net/video-call-android-tutorial/和http://www.truiton.com/2015/05/capture-record-android-screen-using-mediaprojection-apis/but应用程序

  • 我想在应用程序启动时使用Spring ehCache将数据从数据库加载到缓存中,即在调用任何其他方法之前服务器启动时。我不想使用构造函数。请帮帮我。