您是否可以像其他类节点一样在Hazelcast中与其他节点共享DataSerializationFactory?

时间:2019-05-22 10:19:12

标签: hazelcast

我希望能够动态地将成员附加到hazelcast群集,而不必显式提供类路径和数据序列化工厂。

如果我将自定义的hazelcast键/值定义为Serializable,则可以动态附加新成员,并使用getUserCodeDeploymentConfig()。setEnabled(true);向这些新成员提供这些类。另一方面,如果我重新定义这些值并且不使用Serializable,而是使用IdenfiedDataSerializable并创建我的自定义DataSerializableFactory,则会遇到该工厂无法传播的问题。

为了使测试有意义,以下类不能共享类路径(因此它们必须位于不同的项目中):

集群炒作:

package test;

import org.junit.Test;

import com.hazelcast.config.Config;
import com.hazelcast.core.HazelcastInstance;
import com.hazelcast.instance.HazelcastInstanceFactory;

public class ClusterSharingTest
{

  @Test
  public void test() throws Exception
  {
    Config config = new Config();
    config.getGroupConfig().setName("cluster-sharing-test");
    config.getUserCodeDeploymentConfig().setEnabled(true);

    HazelcastInstance hzInstance = HazelcastInstanceFactory.newHazelcastInstance(config);

    while(true) {
      Thread.sleep(1000);
      System.out.println("Object: " + hzInstance.getMap("M").get("k1"));
    }
  }

}

具有可序列化的集群:

package test;

import java.io.Serializable;

import org.junit.Test;

import com.hazelcast.config.Config;
import com.hazelcast.core.HazelcastInstance;
import com.hazelcast.instance.HazelcastInstanceFactory;

public class ClusterSharingTestSerializable
{
  public static class CustomObject implements Serializable {
    public String text = "A custom object";

    @Override
    public String toString() {
      return text;
    }
  }

  @Test
  public void test() throws Exception
  {
    Config config = new Config();
    config.getGroupConfig().setName("cluster-sharing-test");
    config.getUserCodeDeploymentConfig().setEnabled(true);

    HazelcastInstance hzInstance =     HazelcastInstanceFactory.newHazelcastInstance(config);

    hzInstance.getMap("M").put("k1", new CustomObject());

    while(true) {
      Thread.sleep(1000);
      System.out.println("Object: " + hzInstance.getMap("M").get("k1"));
    }
  }

}

具有IdentifiedDataSerializable的集群:

package test;

import java.io.IOException;
import java.io.Serializable;

import org.junit.Test;

import com.hazelcast.config.Config;
import com.hazelcast.core.HazelcastInstance;
import com.hazelcast.instance.HazelcastInstanceFactory;
import com.hazelcast.nio.ObjectDataInput;
import com.hazelcast.nio.ObjectDataOutput;
import com.hazelcast.nio.serialization.DataSerializableFactory;
import com.hazelcast.nio.serialization.IdentifiedDataSerializable;

public class ClusterSharingTestIdentifiedData
{
  public static class CustomObject implements IdentifiedDataSerializable {
    public String text = "A custom object";

    @Override
    public void writeData(ObjectDataOutput out) throws IOException {
      out.writeUTF(text);
    }

    @Override
    public void readData(ObjectDataInput in) throws IOException {
      text = in.readUTF();
    }

    @Override
    public int getFactoryId() {
      return 10;
    }

    @Override
    public int getId() {
      return 1;
    }

    @Override
    public String toString() {
      return text;
    }
  }

  public static class CustomObjectFactory implements DataSerializableFactory {

    @Override
    public IdentifiedDataSerializable create(int typeId) {
      if(typeId != 1) {
        throw new RuntimeException("unsupported type id: " + typeId);
      }

      return new CustomObject();
    }

  }

  @Test
  public void test() throws Exception
  {
    Config config = new Config();
    config.getGroupConfig().setName("cluster-sharing-test");
    config.getUserCodeDeploymentConfig().setEnabled(true);
    config.getSerializationConfig().addDataSerializableFactory(10, new CustomObjectFactory());

    HazelcastInstance hzInstance = HazelcastInstanceFactory.newHazelcastInstance(config);

    hzInstance.getMap("M").put("k1", new CustomObject());

    while(true) {
      Thread.sleep(1000);
      System.out.println("Object: " + hzInstance.getMap("M").get("k1"));
    }
  }

}

如果我启动一个炒集群和一个带有可序列化对象的集群,则将共享类路径,并且一切正常。

如果我启动一个炒集群和一个具有DataIdentifiedSerializable的集群,则会出现异常:

com.hazelcast.nio.serialization.HazelcastSerializationException:没有为名称空间注册10 DataSerializerFactory

我希望这两种情况(尤其是第二种情况)都能正常工作。

0 个答案:

没有答案