OpenHFT Chronicle Queue 写入字段标记为瞬态

OpenHFT Chronicle Queue writing fields marked as transient

好的,所以我有几个 类 基本上看起来像这样。

public class Foo implements Serializable {
    private A a;

    //More code
}

public class A implements Serializable {
    public Vector vector = new Vector();

    //More code
}

public class B implements Serializable {
    private transient A parent;

    //More code
}

public static void main(String[] args) {

    A a = new A();
    a.vector.add(new B(a));

    Foo foo = new Foo(a);
    //More code to write foo with OpenHFT
}

所以这里的奇怪之处在于 A 和 B 循环指向对方,其中 A 在其属性 vector 中有 BBA 作为其属性 parent。通常,如果您尝试编写它,这将是一个问题,因为它会导致无限循环。因此在 parent.

上使用 transient 关键字

但是,出于某种原因,OpenHFT 没有记录 parent 被设置为瞬态的事实,并且仍在尝试编写它,导致我收到 WhosebugException(呵呵,实际上是在询问 WhosebugException Stack Overflow,这是第一次)。

关于为什么会发生这种情况有什么建议吗?

顺便说一句,我正在使用 Maven 导入 OpenHFT,并且我使用的是版本 5.17.17

我不知道你是怎么得到 WhosebugException 的,你的代码(经过一些明显的修改以使其有意义)对我有用:

public class Test {
    public static class Foo implements Serializable {
        private A a;

        public Foo(A a) {
            this.a = a;
        }

        //More code
    }

    public static class A implements Serializable {
        public Collection<B> vector = new Vector<>();

        //More code
    }

    public static class B implements Serializable {
        private transient A parent;
        private String name;

        public B(A a, String name) {
            this.parent = a;
            this.name = name;
        }

        //More code
    }

    public static void main(String[] args) {

        A a = new A();
        a.vector.add(new B(a, "name"));

        Foo foo = new Foo(a);

        Bytes bytes = Wires.acquireBytes();
        final ValueOut out = WireType.JSON.apply(bytes).getValueOut();
        out.marshallable(foo);

        System.err.println(bytes.toString());
    }
}

这将输出:

"a":{"vector":[ {"name":"name"} ]
}

因此很明显瞬态场被忽略了。