Kryonet - How can I register classes in 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 will register it like this:

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 works well before trying to serialize QueryResultsdue to the fact that it contains a container, in this case a HashSet(we also tried ArrayListwith the same results).

At the endpoint filling 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);, everything works smoothly. However, this can be annoying when we begin to implement more advanced classes with many types of containers.

Am I doing something wrong?

+4
source share
1

kryo:

public class QueryResults {

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

    ...
}

.

+1

Source: https://habr.com/ru/post/1606900/


All Articles