在spring引导应用程序中,我们使用hazelcast作为分布式缓存。有时会忘记更改缓存对象上的serialversionuid,这会导致出现大量异常,我们的警报会因为下面的错误而变得疯狂。
java.io.InvalidClassException: com.company.service.server.domain.SomeObject; local class incompatible: stream classdesc serialVersionUID = -5387655287348283785, local class serialVersionUID = -1803841624490656210
at java.io.ObjectStreamClass.initNonProxy(ObjectStreamClass.java:699)
at java.io.ObjectInputStream.readNonProxyDesc(ObjectInputStream.java:2002)
at java.io.ObjectInputStream.readClassDesc(ObjectInputStream.java:1849)
at java.io.ObjectInputStream.readOrdinaryObject(ObjectInputStream.java:2159)
at java.io.ObjectInputStream.readObject0(ObjectInputStream.java:1666)
at java.io.ObjectInputStream.readObject(ObjectInputStream.java:502)
at java.io.ObjectInputStream.readObject(ObjectInputStream.java:460)
at com.hazelcast.internal.serialization.impl.JavaDefaultSerializers$JavaSerializer.read(JavaDefaultSerializers.java:84)
at com.hazelcast.internal.serialization.impl.JavaDefaultSerializers$JavaSerializer.read(JavaDefaultSerializers.java:77)
at com.hazelcast.internal.serialization.impl.StreamSerializerAdapter.read(StreamSerializerAdapter.java:48)
at com.hazelcast.internal.serialization.impl.AbstractSerializationService.toObject(AbstractSerializationService.java:187)
at com.hazelcast.map.impl.proxy.MapProxySupport.toObject(MapProxySupport.java:1237)
at com.hazelcast.map.impl.proxy.MapProxyImpl.get(MapProxyImpl.java:120)
at com.hazelcast.spring.cache.HazelcastCache.lookup(HazelcastCache.java:162)
at com.hazelcast.spring.cache.HazelcastCache.get(HazelcastCache.java:67)
我试图找到一个更好的方法来集中处理这个问题。因此,每次发生类似的事情时,我们都会通过代码将不兼容的对象从缓存中逐出。
我发现SpringBoot为错误处理提供了一个重写接口,即org.springframework.cache.interceptor.cacheerrorhandler。
package org.springframework.cache.interceptor;
import org.springframework.cache.Cache;
import org.springframework.lang.Nullable;
public interface CacheErrorHandler {
void handleCacheGetError(RuntimeException var1, Cache var2, Object var3);
void handleCachePutError(RuntimeException var1, Cache var2, Object var3, @Nullable Object var4);
void handleCacheEvictError(RuntimeException var1, Cache var2, Object var3);
void handleCacheClearError(RuntimeException var1, Cache var2);
}
但是这个接口只用于处理运行时异常,而不是像invalidclassexception那样用于检查异常。
有没有人对如何以一种中心和干净的方式处理它有其他的想法?
暂无答案!
目前还没有任何答案,快来回答吧!