Hazelcast 优雅的处理方式 com.hazelcast.nio.serialization.HazelcastSerializationException java.io.InvalidClassException

Hazelcast graceful way to handle com.hazelcast.nio.serialization.HazelcastSerializationException java.io.InvalidClassException

在我们的 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)

我试图找到一种更好、更集中地处理这个问题的方法。所以每次类似的事情发生时,我们都会通过代码从缓存中驱逐不兼容的对象。

我发现 spring 引导提供了一个接口来覆盖错误处理,它是: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 那样处理检查异常。

有没有人对如何以集中和干净的方式处理它有任何其他想法?

基于此线程,Hazelcast 将所有 InvalidClassException 包装到运行时 HazelcastSerializationException 中,并且可以处理它们。有关详细信息,请参阅此线程 https://github.com/spring-projects/spring-framework/issues/26587