【问题标题】:Error JCache with Hazelcast implementation java.io.NotSerializableException带有 Hazelcast 实现 java.io.NotSerializableException 的错误 JCache
【发布时间】:2019-08-02 13:28:02
【问题描述】:

我已经开始使用带有 Hazelcast 实现的 JCache。

当我尝试将对象放入缓存时,我收到以下错误:

引起: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&lt;**CaBigNumber**&gt; lpgDatasource = <span style='font-weight:bold;color:#7B0052;'>new</span> CaLpgDataCollectionDto&lt;&gt;();</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&lt;T&gt; <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&lt;CaLpgDataRowDto&lt;T&gt;&gt; dataRows = <span style='font-weight:bold;color:#7B0052;'>new</span> ArrayList&lt;&gt;();</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&lt;T&gt; <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&lt;CaBigNumber&gt;
<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&lt;String, Object&gt; config =
               <span style='font-weight:bold;color:#7B0052;'>new</span> MutableConfiguration&lt;String, Object&gt;().setTypes(String.class, Object.class);
Cache&lt;String, Object&gt; cache = cacheManager.createCache( <span style='color:#2A00FF'>"lpgcache"</span>, config );
**cache.put**( <span style='color:#2A00FF'>"**lpgDatasource**"</span>, lpgDatasource );</pre>

你能帮帮我吗?

【问题讨论】:

标签: java hazelcast jcache


【解决方案1】:

看起来您的类 CaBigNumber 引用了一个或多个不可序列化的 lambda,因此无法序列化此类的实例,因为它们引用了不可序列化的元素。例如,考虑这个类:

public class LambdaAndSerializable implements Serializable {
  private String name;
  private Function<String, String> toUpperCase = x -> x.toUpperCase();
}

尝试序列化此类的实例时,会抛出 NotSerializableExceptionException in thread "main" java.io.NotSerializableException: example.LambdaAndSerializable$$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,在提交序列化格式之前,看看会有所帮助。

【讨论】:

  • 嗨瓦西利斯。是的,这就是问题所在。它现在工作正常。非常感谢您的帮助,并对 html 代码表示抱歉。这是我第一次在堆栈溢出。
猜你喜欢
  • 2020-03-11
  • 2015-05-29
  • 2013-04-10
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-11-09
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多