尝试将地图存储在Hazelcast缓存中时无法序列化地图

时间:2016-12-19 12:27:21

标签: java caching hazelcast hazelcast-imap

我正在尝试从数据库获取数据并放入地图并尝试将该地图存储在 hazelcast缓存服务器中。但是它给出错误

  

com.hazelcast.nio.serialization.HazelcastSerializationException:   无法序列化'java.util.TreeMap'。

以下是我的代码。

Map<Integer, List> cacheMap;
cacheMap = (Map<Integer, List>) hazelCastCache.getDataFromCache("CdnToIpConfig", key);

if(cacheMap == null)
{
  LOGGER.info("Cache memory is not allocated");  
  cacheMap = newTreeMap<>(); 
  cdnIpResDto =    configurationService.getCdnIpConfig(pageNo,sortBy,sortingOrder);
  responseDataRange = cdnIpResDto.getDtoList();  
  int lastIndexTracker =0;  
  for (int i = 1; i <= responseDataRange.size(); i++) {
       if (i % 10 == 0) {
            finalData = responseDataRange.subList(i - 10, i);
            cacheMap.put(pageNo, finalData);
            System.out.println(cacheMap);// I can see contents of cacheMap
            pageNo++;
            lastIndexTracker = i;
                   }
               }
   hazelCastCache.addDataToCache("CdnToIpConfig", key, cacheMap); 
   System.out.println("Data added in Hazelcast"); // This line is not getting executed.

由于最后一行print语句没有被执行,这意味着TreeMap没有被添加到cache.Any help Please ??

1 个答案:

答案 0 :(得分:0)

我面临的问题是在上面的代码中我正在采用一个子列表,它没有实现Serializable.So我将子列表包装在ArrayList中并且问题已解决。这是修改后的代码: -

finalData = new ArrayList(responseDataRange.subList(i - 10, i));