简体   繁体   English

Kryonet - 如何在课堂上注册课程?

[英]Kryonet — How can I register classes within my class?

I have the following class: 我有以下课程:

public class QueryResults {
    protected Set<String> resultList = new HashSet<String>();
    protected long executionTime = 0;

    public long getExecutionTime() { return executionTime; }
    [...]
}

And I register it as so: 我将其注册为:

Registrar.RegisterClass(this, QueryResults.class);
------------
public class Registrar {
    public static void RegisterClass(Node n, Class theClass) {
        Map<String, Node> nodeMap = Node.getNodeMap();
        for (Map.Entry<String, Node> node : nodeMap.entrySet()) {
            if (node.getKey().equals(n.getHostname())) {
                Log.info("Registering " + theClass.getSimpleName() + " for " + node.getValue().getHostname());
                node.getValue().getServer().getConnection().getKryo().register(theClass);
                node.getValue().getClient().getConnection().getKryo().register(theClass);
            }
        }
    }
}

This has worked well until attempting to serialize QueryResults , due to it containing a container, in this case a HashSet (We've tried an ArrayList as well, with the same results). 这一直很有效,直到尝试序列化QueryResults ,因为它包含一个容器,在本例中是一个HashSet (我们也试过了一个ArrayList ,结果相同)。

On the endpoint populating and ultimately serializing this class to send back to the caller, I get this output: 在端点上填充并最终序列化此类以发送回调用者,我得到以下输出:

 Exception in thread "Server" com.esotericsoftware.kryo.KryoException: java.lang.IllegalArgumentException: Class is not registered: java.util.HashSet Note: To register this class use: kryo.register(java.util.HashSet.class); 

If I explicitly call Registrar.RegisterClass(this, HashSet.class); 如果我显式调用Registrar.RegisterClass(this, HashSet.class); , everything works swimmingly. 一切都在游泳。 However, this can be annoying once we start to implement more advanced classes, with many types of containers. 但是,一旦我们开始实现更多高级类,包含许多类型的容器,这可能会很烦人。

Am I doing something wrong? 难道我做错了什么?

How about using kryo bind annotations : 如何使用kryo绑定注释

public class QueryResults {

    @CollectionSerializer.BindCollection(
         elementClass = QueryResults.class,
         elementSerializer = DefaultSerializers.StringSerializer.class) 
    protected Set<String> resultList = new HashSet<String>();

    ...
}

Also check a set of additional kryo serialisers . 还要检查一组额外的kryo序列化程序

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM