我已经开始使用 JCache 和 Hazelcast 实现。
当我尝试将对象放入缓存时,出现以下错误:
Caused by: java.io.NotSerializableException: com.catenic.anafee.common.type.CaBigNumber$$Lambda$131/1884551977
<pre style='text-align: left; border: 1px dashed #008DEF; line-height: 18px; padding: 15px; font-size: 13px; font-family:'Courier New', Courier, monospace; overflow: auto;'>CaLpgDataCollectionDto<**CaBigNumber**> lpgDatasource = <span style='font-weight:bold;color:#7B0052;'>new</span> CaLpgDataCollectionDto<>();</pre>
<br>
<pre style='text-align: left; border: 1px dashed #008DEF; line-height: 18px; padding: 15px; font-size: 13px; font-family:'Courier New', Courier, monospace; overflow: auto;'><span style='font-weight:bold;color:#7B0052;'>public</span> <span style='font-weight:bold;color:#7B0052;'>class</span> CaLpgDataCollectionDto<T> <span style='font-weight:bold;color:#7B0052;'>implements</span> Serializable
<span style='font-weight:bold;color:#D3171B'>{</span>
<span style='font-weight:bold;color:#7B0052;'>private</span> <span style='font-weight:bold;color:#7B0052;'>static</span> <span style='font-weight:bold;color:#7B0052;'>final</span> <span style='font-weight:bold;color:#7B0052;'>long</span> serialVersionUID = -1L;
<br>
<span style='font-weight:bold;color:#7B0052;'>private</span> List<CaLpgDataRowDto<T>> dataRows = <span style='font-weight:bold;color:#7B0052;'>new</span> ArrayList<>();</pre>
<pre style='text-align: left; border: 1px dashed #008DEF; line-height: 18px; padding: 15px; font-size: 13px; font-family:'Courier New', Courier, monospace; overflow: auto;'><span style='font-weight:bold;color:#7B0052;'>public</span> <span style='font-weight:bold;color:#7B0052;'>class</span> CaLpgDataRowDto<T> <span style='font-weight:bold;color:#7B0052;'>implements</span> Serializable
<span style='font-weight:bold;color:#D3171B'>{</span>
<span style='font-weight:bold;color:#7B0052;'>private</span> <span style='font-weight:bold;color:#7B0052;'>static</span> <span style='font-weight:bold;color:#7B0052;'>final</span> <span style='font-weight:bold;color:#7B0052;'>long</span> serialVersionUID = 1L;</pre>
<pre style='text-align: left; border: 1px dashed #008DEF; line-height: 18px; padding: 15px; font-size: 13px; font-family:'Courier New', Courier, monospace; overflow: auto;'><span style='font-weight:bold;color:#7B0052;'>public</span> <span style='font-weight:bold;color:#7B0052;'>class</span> CaBigNumber <span style='font-weight:bold;color:#7B0052;'>extends</span> Number <span style='font-weight:bold;color:#7B0052;'>implements</span> Comparable<CaBigNumber>
<span style='font-weight:bold;color:#D3171B'>{</span></pre>
<pre style='text-align: left; border: 1px dashed #008DEF; line-height: 18px; padding: 15px; font-size: 13px; font-family:'Courier New', Courier, monospace; overflow: auto;'>CachingProvider cachingProvider = Caching.getCachingProvider();
CacheManager cacheManager = cachingProvider.getCacheManager();
CompleteConfiguration<String, Object> config =
<span style='font-weight:bold;color:#7B0052;'>new</span> MutableConfiguration<String, Object>().setTypes(String.class, Object.class);
Cache<String, Object> cache = cacheManager.createCache( <span style='color:#2A00FF'>"lpgcache"</span>, config );
**cache.put**( <span style='color:#2A00FF'>"**lpgDatasource**"</span>, lpgDatasource );</pre>
你能帮我一下吗?
最佳答案
看起来您的类 CaBigNumber
引用了一个或多个不可序列化的 lambda,因此此类的实例无法序列化,因为它们引用了不可序列化的元素。例如,考虑这个类:
public class LambdaAndSerializable implements Serializable {
private String name;
private Function<String, String> toUpperCase = x -> x.toUpperCase();
}
尝试序列化此类的实例时,会引发 NotSerializedException
: 线程“main”中的异常 java.io.NotSerializedException: example.LambdaAndSerialized$$Lambda$1/401625763
.
如果包含的 lambda 确实是类状态的一部分并且应该序列化,那么您可以通过强制转换使它们可序列化:
// explicitly cast lambda to Function & Serializable
public class LambdaAndSerializable implements Serializable {
private String name;
private Function<String, String> toUpperCase =
(Function<String, String> & Serializable) x -> x.toUpperCase();
}
有专门的section about serialization in the Hazelcast reference manual ,在采用序列化格式之前,看看可能会有所帮助。
关于java - Hazelcast 实现 java.io.NotSerializedException 时出现错误 JCache,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57327482/