使用Geode C#本机客户端在类型注册表中注册PDX类型 [英] registering PDX type in type registry with Geode C# native client

查看:231
本文介绍了使用Geode C#本机客户端在类型注册表中注册PDX类型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在Geode中实现非常简单的PDX自动序列化.我使用零arg构造函数创建了自己的域类:

I am trying to implement a very simple PDX autoserialization in Geode. I've created a domain class of my own with a zero arg constructor:

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

    public TestPdx() { }
}

现在,我希望此类自动序列化.我使用以下cache.xml启动服务器缓存,在其中尝试为自动PDX注册此类型:

Now I want this class to auto serialize. I start a server cache with the following cache.xml where I attempt to register this type for auto 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类型.您如何使用本地客户端做到这一点?

So I haven't registered the PDX type properly. How do you do that with native client?

谢谢

推荐答案

这里的答案是在TestPdx中实现IPdxSerializable,如下所示:

An answer here is to implement IPdxSerializable in the TestPdx as follows:

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类型的区域,如下所示:

and then register the Pdx type in the Geode, and use a region of type object or type TestPdx as follows:

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

并简单地将TestPdx写入该区域:

and to write the TestPdx to the region simply:

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

,并且在Geode区域中会出现一个PdxInstance,因此您可以在其上运行OQL查询,等等.

and there will be a PdxInstance in the Geode region so you can run OQL queries on it, etc.

这篇关于使用Geode C#本机客户端在类型注册表中注册PDX类型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆