1

My app is based on spring boot + hazelcast.

I am trying to save simple entity into 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
}

My service method look like this:

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

When I am trying to store the entity I received com.hazelcast.nio.serialization.HazelcastSerializationException with trace:

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)

Here is my hazelcast config:

@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);
  }

Please, tell me how can I resolve this issue.

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);
  }
Hutsul
  • 1,535
  • 4
  • 31
  • 51
  • Are you using custom serialization? Something like `.addSerializerConfig(new SerializerConfig().setImplementation(serializer).setTypeClass(ExampleMeeting.class)))`? – noscreenname Aug 09 '16 at 12:45
  • @noscreenname Thanks for response. No I have not. Is it always need to add custom serializer for hazelcast? – Hutsul Aug 09 '16 at 13:47
  • @I.Domshchikon no, but the stacktrace suggested it could be caused by incorrect serialization conf. Can you add code of `addMap(mapConfigMap, method2, annotation)`? – noscreenname Aug 09 '16 at 13:52
  • @noscreenname. I have updated the question. – Hutsul Aug 09 '16 at 19:40
  • @I.Domshchikon one more question: are you using hazelcast as a hibernated level 2 cache? – noscreenname Aug 10 '16 at 07:37
  • @I.Domshchikon you don't need to register any custom serializers if you use java `Serializable` (as in your example). It should work out of the box with `Serializable` and `Externalizable`. Could you post a reproducible example on GH some there? Thank you – Vik Gamov Aug 12 '16 at 16:08
  • BTW, I wrote a blog about how yo can use Spring Boot with Hazelcast http://next.javaheadbrain.com/posts/2015/12/31/caching-made-bootiful.html. TL;DR, spring boot has support of jcache and Hazelcast out of the box with – Vik Gamov Aug 12 '16 at 16:09
  • Did you have a chance to read my answer? Thanks – Vik Gamov Aug 15 '16 at 19:20
  • @VikGamov. Sorry, I have't chance to look for that, but I'll do that – Hutsul Aug 16 '16 at 16:12

2 Answers2

0

This is more of a guess that an answer, but I found a similar issue on hazelcast github.

It is possible that when hazelcast conf is created from annotions, a tird-party seriliazers are detected (are you using hazelcast as hibernate cache?). If this is the case, you should include the jars, containing the serialization classes in all hazelcast instances (client and server).

noscreenname
  • 3,314
  • 22
  • 30
0

I.Domshchikov,

Did you have a chance to check this answer in SO about using Hazelcast in Spring (Boot) environment?

Hazelcast Caching for Clusterd Spring Application

Thank you

Community
  • 1
  • 1
Vik Gamov
  • 5,446
  • 1
  • 26
  • 46