在客户端中使用Near Cache的Hazelcast ClassNotFound

时间:2018-12-03 15:27:34

标签: caching hazelcast

我尝试通过以下方式使用Hazelcast(3.9.2、3.11没什么区别): 我有Hazelcast服务器(成员)。我专门运行它们,而不是嵌入它们。 我不想教Hazelcast成员我要在其中存储的类。我使用了捆绑的hazelcast.xml文件,并执行了以下插件(3.9.2)

<replicatedmap name="default">
    <in-memory-format>BINARY</in-memory-format>
    <statistics-enabled>true</statistics-enabled>
</replicatedmap>

我还激活了TCP,而不是多播(true / false) 那就是我所做的所有更改。我从一位成员开始听127.0.0.1:5701

然后,我尝试将Hazelcast客户端附加到用于存储和检索Map的成员(主要是ReplicatedMaps,但是Map在我的情况下也不起作用)

我的客户端代码如下(缓存只是一个无属性的可序列化类):

public class Main {
  public static final String HAZELCAST_INSTANCE_NAME = "HAZI";
  public static final String REPLICATEDMAP_NAME = "REP_MAP";
  public static final String MAP_NAME = "NORMAL_MAP";

public static void main(String[] args) {
    init();
    HazelcastInstance instance = HazelcastClient.getHazelcastClientByName(HAZELCAST_INSTANCE_NAME);
    Map<String, Object> repMap = instance.getReplicatedMap(REPLICATEDMAP_NAME);
    repMap.put("MyKey", new Cache());
    System.err.println("Retrieve " + repMap.get("MyKey"));
    Map<String, Object> normalMap = instance.getReplicatedMap(MAP_NAME);
    normalMap.put("MyKey", new Cache());
    System.err.println("Retrieve " + normalMap.get("MyKey"));
    System.exit(1);
}

private static void init() {
    ClientConfig cfg = new ClientConfig();
    cfg.setInstanceName(HAZELCAST_INSTANCE_NAME);
    cfg.addNearCacheConfig(defineNearCache(REPLICATEDMAP_NAME));
    cfg.addNearCacheConfig(defineNearCache(MAP_NAME));
    // for analysis in the hazelcast management console
    cfg.getProperties().put("hazelcast.client.statistics.enabled", "true");
    cfg.getProperties().put("hazelcast.client.statistics.period.seconds", "60");
    cfg.getNetworkConfig().addAddress("127.0.0.1:5701");
    if (HazelcastClient.newHazelcastClient(cfg) == null) {
        System.err.println(" !!! ERROR in Cache Config !!!");
    }
}
private static NearCacheConfig defineNearCache(String mapName) {
    EvictionConfig evictionConfig = new EvictionConfig()
            .setMaximumSizePolicy(EvictionConfig.MaxSizePolicy.ENTRY_COUNT)
            .setSize(200);
    return new NearCacheConfig()
            .setName(mapName)
            .setInMemoryFormat(InMemoryFormat.BINARY)
            .setInvalidateOnChange(true)
            .setEvictionConfig(evictionConfig);
}

}

我现在的问题是: 使用此代码,我得到一个ClassNotFoundError尝试将内容放入复制的地图或常规地图中,但放置在专用的Hazelcast服务器(成员)中,而不是在客户端上。

    SCHWERWIEGEND: [127.0.0.1]:5701 [dev] [3.9.2] hz._hzInstance_1_dev.event-3 caught an exception while processing task:com.hazelcast.spi.impl.eventservice.impl.LocalEventDispatcher@eeed098
com.hazelcast.nio.serialization.HazelcastSerializationException: java.lang.ClassNotFoundException: de.empic.hazelwar.model.Cache
        at com.hazelcast.internal.serialization.impl.JavaDefaultSerializers$JavaSerializer.read(JavaDefaultSerializers.java:224)
        at com.hazelcast.internal.serialization.impl.StreamSerializerAdapter.read(StreamSerializerAdapter.java:48)
        at com.hazelcast.internal.serialization.impl.AbstractSerializationService.toObject(AbstractSerializationService.java:185)
        at com.hazelcast.map.impl.DataAwareEntryEvent.getValue(DataAwareEntryEvent.java:90)
        at com.hazelcast.client.impl.protocol.task.replicatedmap.AbstractReplicatedMapAddEntryListenerMessageTask.handleEvent(AbstractReplicatedMapAddEntryListenerMessageTask.java:92)
        at com.hazelcast.client.impl.protocol.task.replicatedmap.AbstractReplicatedMapAddEntryListenerMessageTask.entryAdded(AbstractReplicatedMapAddEntryListenerMessageTask.java:132)
        at com.hazelcast.replicatedmap.impl.ReplicatedMapEventPublishingService.dispatchEvent(ReplicatedMapEventPublishingService.java:82)
        at com.hazelcast.replicatedmap.impl.ReplicatedMapService.dispatchEvent(ReplicatedMapService.java:247)
        at com.hazelcast.spi.impl.eventservice.impl.LocalEventDispatcher.run(LocalEventDispatcher.java:64)
        at com.hazelcast.util.executor.StripedExecutor$Worker.process(StripedExecutor.java:225)
        at com.hazelcast.util.executor.StripedExecutor$Worker.run(StripedExecutor.java:208)
Caused by: java.lang.ClassNotFoundException: de.empic.hazelwar.model.Cache
    at java.net.URLClassLoader.findClass(URLClassLoader.java:381)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
    at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:349)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
    at com.hazelcast.nio.ClassLoaderUtil.tryLoadClass(ClassLoaderUtil.java:173)
    at com.hazelcast.nio.ClassLoaderUtil.loadClass(ClassLoaderUtil.java:147)
    at com.hazelcast.nio.IOUtil$ClassLoaderAwareObjectInputStream.resolveClass(IOUtil.java:591)
    at java.io.ObjectInputStream.readNonProxyDesc(ObjectInputStream.java:1868)
    at java.io.ObjectInputStream.readClassDesc(ObjectInputStream.java:1751)
    at java.io.ObjectInputStream.readOrdinaryObject(ObjectInputStream.java:2042)
    at java.io.ObjectInputStream.readObject0(ObjectInputStream.java:1573)
    at java.io.ObjectInputStream.readObject(ObjectInputStream.java:431)
    at com.hazelcast.internal.serialization.impl.JavaDefaultSerializers$JavaSerializer.read(JavaDefaultSerializers.java:219)
    ... 10 more

每当我从客户端配置中删除Near缓存配置时,所有工作都非常完美,除非我当然没有Near缓存。

我在这里想念什么?

2 个答案:

答案 0 :(得分:0)

@magicroomy,我在3.9.2和3.11上都运行相同的命令。我可以确认:

  • 如果您将“复制地图”更改为“地图”,则无论有无近缓存,它都可以使用。
  • 使用复制地图时,如果定义了“近缓存”,则会在服务器端引发异常。
  • 没有Near Cache,ReplicatedMap也可以工作。

我也创建了一个github问题:https://github.com/hazelcast/hazelcast/issues/14210

答案 1 :(得分:0)

使用hazelcast的3.11.1版本解决了我的问题。