用kryo注册类的策略 [英] Strategy for registering classes with kryo

查看:266
本文介绍了用kryo注册类的策略的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我最近发现了图书馆kryonet,这是非常棒的,非常适合我的需求。

I recently discovered the library kryonet, which is super awesome and fits my needs excellently.

然而,我遇到的一个问题是为注册所有可以转移的类制定了一个好的策略。

However, the one problem that I am having is developing a good strategy for registering all of the classes that can be transferred.

我知道我可以在每个对象中编写一个静态方法,它将返回它使用的所有类的列表,但我真的不想这样做(为了我自己的时间目的,以及那些将扩展这些对象的人。)

I know that I can write a static method in each object that will return a list of all of the classes that it uses, but I would really rather not have to do that (for my own time purposes, as well as those who will be extending these objects).

我正在试图查看是否有办法获取对象所引用的所有类(在来自getClass()方法的字段以及它的继承),但我无法取得任何成功。

I was playing around with trying to see if there was a way to get all of the classes that an object references (in it's fields as well as it's inheritance) from the getClass() method, but I was unable to have any success.

最后,我知道kryo有kryo.setRegistrationOptional(是的)但我正在努力弄清楚如何使用它。当打开此选项时,如果我没有注册类,kryo似乎仍然会抛出异常。此外,这种方法假设比能够注册所有类要慢得多。我很好,如果你第一次使用这种方法发送一个对象的速度很慢,但我不知道每次我想发送一个对象时我是否都会出现严重的性能下降。

Finally, I know that kryo has kryo.setRegistrationOptional(true) but I am having a very difficult time trying to figure out how to use it. When this option is turned on, kryo still seems to throw exceptions if I haven't registered classes. Also, this method supposed is much slower than being able to register all of the classes. I'm fine if the first time you need to send an object using this method is slow, but I don't know if I'm okay with serious performance degradation every time that I want to send an object.

理想情况下,我会想要使用kryonet发送一个对象包。如果只有一些是扫描该包并确定我需要注册的所有类,那将是非常好的。现在并非我的所有客户都需要注册每个对象,但这是一个单独的问题,我不知道是否有解决方案。

Ideally, I'll have a package of objects that I will want to send using kryonet. If there was just some was to scan that package and determine all of the classes that I need to register, that would be excellent. Now not all of my clients would need to register every object, but that's something of a separate issue, and I don't know if there is a solution to that.

如果有人能指出我的方向非常好。

If anyone could point me in the right direction that would be excellent.

推荐答案

类可能来自不同的地方,如磁盘,网络,内存(动态生成)。因此,获取有关要向Kryo注册的类的信息必须针对每个特定情况单独处理。

Classes may come from different places such as disk, network, memory (dynamically generated). Therefore, obtaining information about classes to be registered with Kryo has to be handled separately for each specific case.

如果您可以从jar文件中读取类,那么以下代码段应该是让你开始。

If you can read classes from a jar file then the following snippet should get you started.

private static List<Class<?>> getFromJarFile(final String jar, final String packageName)    throws ClassNotFoundException, IOException {
    final List<Class<?>> classes = new ArrayList<Class<?>>();
    final JarInputStream jarFile = new JarInputStream(new FileInputStream(jar));
    JarEntry jarEntry = null;
    do {
        jarEntry = jarFile.getNextJarEntry();
        if (jarEntry != null) {
            String className = jarEntry.getName();
            if (className.endsWith(".class")) {
                className = className.substring(0, className.lastIndexOf('.')); // strip filename extension
                if (className.startsWith(packageName + "/")) {  // match classes in the specified package and its subpackages       
                    classes.add(Class.forName(className.replace('/', '.')));
                }
            }
        }
    } while (jarEntry != null);
    return classes;
}

这篇关于用kryo注册类的策略的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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