0

I have start to use JCache with Hazelcast implementation.

When I try to put in cache an object I get the following error:

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&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>

Could you help me please?

Andronicus
  • 25,419
  • 17
  • 47
  • 88
Hossain
  • 31
  • 6
  • Please take a look at [code formatting guide](https://stackoverflow.com/editing-help#code), you don't have to format it in html snippet like you did. You can also set proper [syntax-highlighting](https://stackoverflow.com/editing-help#syntax-highlighting) if it's different than your tag or if you have multiple language tags – barbsan Aug 02 '19 at 13:42

1 Answers1

1

Looks like your class CaBigNumber references one or more lambdas which are not serializable, therefore instances of this class cannot be serialized because they reference non-serializable elements. For example, consider this class:

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

When attempting to serialize an instance of this class, a NotSerializableException is thrown: Exception in thread "main" java.io.NotSerializableException: example.LambdaAndSerializable$$Lambda$1/401625763.

If the included lambdas are indeed part of your class' state and should be serialized, then you can make them serializable by casting:

// 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();
}

There is a dedicated section about serialization in the Hazelcast reference manual, before committing to a serialization format it could be helpful to have a look.

  • Hi Vassilis. Yes, this was the problem. It works fine now. Thank you so much for your help and sorry for the html code. It is my first time in Stack overflow. – Hossain Aug 05 '19 at 08:15