将数据放入映射时,Hazelcast,HazelcastSerializationException

时间:2016-08-09 11:17:38

标签: java serialization hazelcast hazelcast-imap

我的应用程序基于spring boot + hazelcast。

我正在尝试将简单实体保存为hazelcast:

public class ExampleMeeting implements Serializable {

  private static final long serialVersionUID = 1L;

  private String id;
  private String name;

  public ExampleMeeting(String id, String name) {
    this.id = id;
    this.name = name;
  }

  public ExampleMeeting() {
  }

// getters and setters
}

我的服务方法如下:

@CachePut(value = MEETING_CACHE_NAME, key = "#meeting.id")
  public ExampleMeeting saveMeeting(ExampleMeeting meeting) {
    LOGGER.info("Save meeting to cache {}", meeting);
    return meeting;
  }

当我尝试存储我收到com.hazelcast.nio.serialization.HazelcastSerializationException的实体时,跟踪:

com.hazelcast.nio.serialization.HazelcastSerializationException: There is no suitable de-serializer for type 2. This exception is likely to be caused by differences in the serialization configuration between members or between clients and members.
    at com.hazelcast.internal.serialization.impl.AbstractSerializationService.newHazelcastSerializationException(AbstractSerializationService.java:173)
    at com.hazelcast.internal.serialization.impl.AbstractSerializationService.readObject(AbstractSerializationService.java:200)
    at com.hazelcast.internal.serialization.impl.ByteArrayObjectDataInput.readObject(ByteArrayObjectDataInput.java:600)
    at com.hazelcast.cluster.impl.ConfigCheck.readData(ConfigCheck.java:215)
    at com.hazelcast.cluster.impl.JoinMessage.readData(JoinMessage.java:98)
    at com.hazelcast.cluster.impl.JoinRequest.readData(JoinRequest.java:68)
    at com.hazelcast.internal.serialization.impl.DataSerializer.read(DataSerializer.java:121)
    at com.hazelcast.internal.serialization.impl.DataSerializer.read(DataSerializer.java:47)
    at com.hazelcast.internal.serialization.impl.StreamSerializerAdapter.read(StreamSerializerAdapter.java:46)
    at com.hazelcast.internal.serialization.impl.AbstractSerializationService.readObject(AbstractSerializationService.java:204)
    at com.hazelcast.internal.serialization.impl.ByteArrayObjectDataInput.readObject(ByteArrayObjectDataInput.java:600)
    at com.hazelcast.cluster.impl.MulticastService.receive(MulticastService.java:201)
    at com.hazelcast.cluster.impl.MulticastService.run(MulticastService.java:159)
    at java.lang.Thread.run(Thread.java:745)

这是我的hazelcast配置:

@Bean
  HazelcastInstance hazelcastInstance() {

    Config config = new ClasspathXmlConfig(hazelcatsConfig);

    Map<String, MapConfig> mapConfigMap = new HashMap<String, MapConfig>();

    ClassPathBeanDefinitionScanner scanner = new ClassPathBeanDefinitionScanner(applicationContext);
    for (BeanDefinition bd : scanner.findCandidateComponents("com.egalacoral.spark")) {
      String className = bd.getBeanClassName();
      try {
        Class<?> classObj = Class.forName(className);
        Method[] methods = classObj.getDeclaredMethods();
        for (Method method2 : methods) {
          Cacheable annotation = AnnotationUtils.getAnnotation(method2, Cacheable.class);
          if (annotation != null && annotation.value().length > 0) {
            addMap(mapConfigMap, method2, annotation);
          }
        }
      } catch (ClassNotFoundException e) {
        LOGGER.error("Error while creating maps for caches", e);
      }
    }
    config.setMapConfigs(mapConfigMap);
    return Hazelcast.newHazelcastInstance(config);
  }

请告诉我如何解决此问题。

UPADATED

 protected void addMap(Map<String, MapConfig> mapConfigMap, Method method2, Cacheable annotation) {
    MapConfig mapConfig = new MapConfig();
    HazelcastMapConfig cacheConfig = AnnotationUtils.getAnnotation(method2, HazelcastMapConfig.class);
    mapConfig.setEvictionPolicy(cacheConfig.evictionPolicy());
    String timeToLiveSeconds = cacheConfig.timeToLiveSeconds();
    if (StringUtils.hasText(timeToLiveSeconds)) {
      timeToLiveSeconds = this.embeddedValueResolver.resolvePlaceholders(timeToLiveSeconds);
    }
    mapConfig.setTimeToLiveSeconds(Integer.parseInt(timeToLiveSeconds));
    String key = annotation.value()[0];
    mapConfigMap.put(key, mapConfig);
    LOGGER.info("Created map for cache {} : {} ", key, mapConfig);
  }

2 个答案:

答案 0 :(得分:0)

这更像是一个答案的猜测,但我在hazelcast github上找到了类似的issue

当从注释创建hazelcast conf时,可能会检测到一个tird-party seriliazers(你使用hazelcast作为hibernate缓存吗?)。如果是这种情况,您应该包含jar,其中包含所有hazelcast实例(客户端和服务器)中的序列化类。

答案 1 :(得分:0)

I.Domshchikov,

你有没有机会在SO中查看关于在Spring(Boot)环境中使用Hazelcast的答案?

Hazelcast Caching for Clusterd Spring Application

谢谢

相关问题