gpt4 book ai didi

java - 将数据放入 map 时 Hazelcast、HazelcastSerializationException

转载 作者:太空宇宙 更新时间:2023-11-04 12:21:09 28 4
gpt4 key购买 nike

我的应用程序基于 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);
}

请告诉我如何解决这个问题。

更新:

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

最佳答案

这更多的是猜测而不是答案,但我发现了类似的 issue在 hazelcast github 上。

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

关于java - 将数据放入 map 时 Hazelcast、HazelcastSerializationException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38849249/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com