用的Geode C#本地客户端注册PDX型类型注册表(registering PDX type in

2019-10-29 07:27发布

我想实现的Geode一个非常简单的PDX autoserialization。 我创建了一个域类我自己有一个零参数的构造函数:

public class TestPdx
{
    public string Test1 { get; set; }
    public string Test2 { get; set; }
    public string Test3 { get; set; }

    public TestPdx() { }
}

现在我想这个类来自动序列化。 我先从以下cache.xml,我试图注册该类型汽车PDX服务器缓存:

<?xml version="1.0" encoding="UTF-8"?>
<cache xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
xmlns="http://geode.apache.org/schema/cache" 
xsi:schemaLocation="http://geode.apache.org/schema/cache
http://geode.apache.org/schema/cache/cache-1.0.xsd" 
version="1.0">
<cache-server/>
<pdx>
<pdx-serializer>
  <class-name>org.apache.geode.pdx.ReflectionBasedAutoSerializer</class-name>
  <parameter name="classes"><string>TestPdx</string></parameter>
</pdx-serializer>
</pdx>
<region name="webclient" refid="REPLICATE_PERSISTENT"/>
</cache>

然后运行下面的代码:

static void Main(string[] args)
{
    // 1. cache
    CacheFactory cacheFactory = CacheFactory.CreateCacheFactory();
    Cache cache = cacheFactory
        .SetSubscriptionEnabled(true)
        .SetPdxReadSerialized(true)
        .Create();

    Serializable.RegisterPdxSerializer(new ReflectionBasedAutoSerializer());

    RegionFactory regionFactory = cache.CreateRegionFactory(RegionShortcut.CACHING_PROXY);
    IRegion<string, TestPdx> region = regionFactory.Create<string, TestPdx>("webclient");

    // 3. TestPx object
    TestPdx t = new TestPdx();
    t.Test1 = "test1";
    t.Test2 = "test2";
    t.Test3 = "test3";

    region["1"] = t;

    // 4. Get the entries
    TestPdx result1 = region["1"];

    // 5. Print result
    Console.WriteLine(result1.Test1);
    Console.WriteLine(result1.Test2);
    Console.WriteLine(result1.Test3);
}

这个代码在线路崩溃region["1"] = t; 有错误

GFCLI_EXCEPTION:System.Runtime.InteropServices.SEHException (0x80004005): External component has thrown an exception.
at apache.geode.client.SerializationRegistry.GetPDXIdForType(SByte* , SharedPtr<apache::geode::client::Serializable>* )

所以我还没有注册的PDX型正常。 你怎么做,与本地客户端?

谢谢

Answer 1:

这里有一个答案是落实IPdxSerializableTestPdx如下:

public class TestPdx : IPdxSerializable
{
    public string Test1 { get; set; }
    public string Test2 { get; set; }
    public string Test3 { get; set; }
    public int Pid { get; set; }

    public void ToData(IPdxWriter writer)
    {
        writer.WriteString("Test1", Test1);
        writer.WriteString("Test2", Test2);
        writer.WriteString("Test3", Test3);
        writer.WriteInt("Pid", Pid);
        writer.MarkIdentityField("Pid");
    }

    public void FromData(IPdxReader reader)
    {
        Test1 = reader.readString("Test1");
        Test2 = reader.readString("Test2");
        Test3 = reader.readString("Test3");
        Pid = reader.readInt("Pid");
    }

    public static IPdxSerializable CreateDeserializable()
    {
        return new TestPdx();
    }

    public TestPdx() { }
}

然后注册在的Geode对PDX类型,并使用类型的区域object或键入TestPdx如下:

 Serializable.RegisterPdxType(TestPdx.CreateDeserializable);
 IRegion<string, Object> t = regionFactory.Create<string, Object>("test");

并写TestPdx简单的区域:

TestPdx value = new TestPdx();
value.Test1 = "hello";
value.Test2 = "world";
value.Test3 = "again";
t[key] = value;

并且会有一个PdxInstance中的Geode区域,所以你可以运行OQL查询就可以了,等等。



文章来源: registering PDX type in type registry with Geode C# native client
标签: gemfire geode