0

在使用 SAX 时,我在 Java 中遇到了一些奇怪的行为。正在设置值,但随后在输出的 toString 方法中恢复为默认值。这似乎真的很不寻常。我查看了代码并输出了变量的内存地址,一切都以正确的顺序发生并且似乎做得正确。其他反序列化的 XML 元素很好,所以我无法解决。

我的系统是这样工作的:有些类具有抽象方法,用户在其中定义属性助手和元素处理程序。元素处理程序处理基于子 XML 元素的子类的生成,而属性处理程序从构造函数中获取属性列表,并通过属性帮助程序使用此列表,通过属性帮助程序中实现的抽象方法设置字段。

这是有问题的类:

package kokuks.flowmon;

import kokuks.flowmon.FlowmonParser.Handler;

import org.xml.sax.Attributes;

public class FlowProbeFlowStats extends FlowmonParserElement {
    protected final FlowProbe parent;
    protected int flowId = 0;
    protected int packets = 0;
    protected int bytes = 0;
    protected long delayFromFirstProbeSum = 0;
    protected boolean set = false;

    /**
     * @param handler
     * @param parent
     * @param uri
     * @param localName
     * @param qName
     * @param attributes
     */
    FlowProbeFlowStats(Handler handler, FlowProbe parent, String uri, String localName, String qName, Attributes attributes) {
        super(handler, parent, uri, localName, qName, attributes);
        this.parent = parent;
    }

    /**
     * @return the parent
     */
    public FlowProbe getParent() {
        return parent;
    }

    /* (non-Javadoc)
     * @see kokuks.flowmon.FlowmonParserElement#getXMLName()
     */
    @Override
    protected String getXMLName() {
        return "FlowStats";
    }

    /* (non-Javadoc)
     * @see kokuks.flowmon.FlowmonParserElement#createAttributeHandlers()
     */
    @Override
    protected IAttributeHandler[] createAttributeHandlers() {
        return new IAttributeHandler[] {
            new AttributeHandler("flowId") {
                @Override
                public void perform(String argValue) {
                    System.out.println("AH flowId: " + argValue);
                    flowId = Integer.parseInt(argValue);
                    set = true;
                }
            },
            new AttributeHandler("packets") {
                @Override
                public void perform(String argValue) {
                    System.out.println("AH packets: " + argValue);
                    packets = Integer.parseInt(argValue);
                    set = true;
                }
            },
            new AttributeHandler("bytes") {
                @Override
                public void perform(String argValue) {
                    System.out.println("AH bytes: " + argValue);
                    bytes = Integer.parseInt(argValue);
                    set = true;
                }
            },
            new AttributeHandler("delayFromFirstProbeSum") {
                @Override
                public void perform(String argValue) {
                    System.out.println("AH delayFromFirstProbeSum: " + argValue);
                    delayFromFirstProbeSum = Long.parseLong(argValue.substring(0, argValue.length() - 2));
                    set = true;
                }
            }
        };
    }

    /* (non-Javadoc)
     * @see java.lang.Object#toString()
     */
    @Override
    public String toString() {
        return "FlowStats/" + super.toString() + "[flowId: " + flowId + ", packets: " + packets +
        ", bytes: " + bytes + ", delayFromFirstProbeSum: " + delayFromFirstProbeSum + "]";
    }
}

我还有一些调试输出,其中查找了属性并调用了相关的实现方法。

    if (this instanceof FlowProbeFlowStats) {
        System.out.println("ahaaaa1:: " + this.toString());
    }

    IAttributeHandler[] ahandlers = createAttributeHandlers();

    if (ahandlers != null) {
        Map<String, IAttributeHandler> attributeHandlers = new HashMap<String, IAttributeHandler>(ahandlers.length);
        for (IAttributeHandler ah : ahandlers) {
            attributeHandlers.put(ah.getName(), ah);
        }

        for (int i = 0; i < attributes.getLength(); i++) {
            IAttributeHandler ah = attributeHandlers.get(attributes.getQName(i));
            if (ah == null) {
                throw new IllegalStateException("Attribute helper not found in qName: " + qName +  " for attrib with localName: " + attributes.getLocalName(i) + ", qname*: " + attributes.getQName(i));
            }

            try {
                ah.perform(attributes.getValue(i));
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }

    if (this instanceof FlowProbeFlowStats) {
        System.out.println("ahaaaa2:: " + this.toString());
    }

无论如何,这是输出:

ahaaaa1:: FlowStats/kokuks.flowmon.FlowProbeFlowStats@89cd21[flowId: 0, packets: 0, bytes: 0, delayFromFirstProbeSum: 0]
AH flowId: 1
AH packets: 5
AH bytes: 333
AH delayFromFirstProbeSum: 50274395ns
ahaaaa2:: FlowStats/kokuks.flowmon.FlowProbeFlowStats@89cd21[flowId: 1, packets: 5, bytes: 333, delayFromFirstProbeSum: 50274395]
ahaaaa1:: FlowStats/kokuks.flowmon.FlowProbeFlowStats@849f16[flowId: 0, packets: 0, bytes: 0, delayFromFirstProbeSum: 0]
AH flowId: 2
AH packets: 5
AH bytes: 333
AH delayFromFirstProbeSum: 50274395ns
ahaaaa2:: FlowStats/kokuks.flowmon.FlowProbeFlowStats@849f16[flowId: 2, packets: 5, bytes: 333, delayFromFirstProbeSum: 50274395]
ahaaaa1:: FlowStats/kokuks.flowmon.FlowProbeFlowStats@825c9d[flowId: 0, packets: 0, bytes: 0, delayFromFirstProbeSum: 0]
AH flowId: 3
AH packets: 5
AH bytes: 1082
AH delayFromFirstProbeSum: 0ns
ahaaaa2:: FlowStats/kokuks.flowmon.FlowProbeFlowStats@825c9d[flowId: 3, packets: 5, bytes: 1082, delayFromFirstProbeSum: 0]
ahaaaa1:: FlowStats/kokuks.flowmon.FlowProbeFlowStats@1577d9b[flowId: 0, packets: 0, bytes: 0, delayFromFirstProbeSum: 0]
AH flowId: 4
AH packets: 4
AH bytes: 762
AH delayFromFirstProbeSum: 0ns
ahaaaa2:: FlowStats/kokuks.flowmon.FlowProbeFlowStats@1577d9b[flowId: 4, packets: 4, bytes: 762, delayFromFirstProbeSum: 0]
ahaaaa1:: FlowStats/kokuks.flowmon.FlowProbeFlowStats@8097e5[flowId: 0, packets: 0, bytes: 0, delayFromFirstProbeSum: 0]
AH flowId: 2
AH packets: 5
AH bytes: 333
AH delayFromFirstProbeSum: 0ns
ahaaaa2:: FlowStats/kokuks.flowmon.FlowProbeFlowStats@8097e5[flowId: 2, packets: 5, bytes: 333, delayFromFirstProbeSum: 0]
ahaaaa1:: FlowStats/kokuks.flowmon.FlowProbeFlowStats@745c78[flowId: 0, packets: 0, bytes: 0, delayFromFirstProbeSum: 0]
AH flowId: 4
AH packets: 4
AH bytes: 762
AH delayFromFirstProbeSum: 40615996ns
ahaaaa2:: FlowStats/kokuks.flowmon.FlowProbeFlowStats@745c78[flowId: 4, packets: 4, bytes: 762, delayFromFirstProbeSum: 40615996]
ahaaaa1:: FlowStats/kokuks.flowmon.FlowProbeFlowStats@162e843[flowId: 0, packets: 0, bytes: 0, delayFromFirstProbeSum: 0]
AH flowId: 1
AH packets: 5
AH bytes: 333
AH delayFromFirstProbeSum: 0ns
ahaaaa2:: FlowStats/kokuks.flowmon.FlowProbeFlowStats@162e843[flowId: 1, packets: 5, bytes: 333, delayFromFirstProbeSum: 0]
ahaaaa1:: FlowStats/kokuks.flowmon.FlowProbeFlowStats@7e9dfc[flowId: 0, packets: 0, bytes: 0, delayFromFirstProbeSum: 0]
AH flowId: 3
AH packets: 5
AH bytes: 1082
AH delayFromFirstProbeSum: 51335996ns
ahaaaa2:: FlowStats/kokuks.flowmon.FlowProbeFlowStats@7e9dfc[flowId: 3, packets: 5, bytes: 1082, delayFromFirstProbeSum: 51335996]
[kks-j~KokuKS~/Names] fmxml:FlowMonitor[FlowStats[Flow[flowID: 1,timeFirstTxPacket: 1011735011,timeLastTxPacket: 1343457103,timeFirstRxPacket: 1021768610,timeLastRxPacket: 1353490702,delaySum: 50274395,jitterSum: 212800,lastDelay: 10033599,txBytes: 333, rxBytes: 333,txPackets: 5,rxPackets: 5,lostPackets: 0,timesForwarded: 0],Flow[flowID: 2,timeFirstTxPacket: 1015259481,timeLastTxPacket: 1543226881,timeFirstRxPacket: 1025293080,timeLastRxPacket: 1553260480,delaySum: 50274395,jitterSum: 212800,lastDelay: 10033599,txBytes: 333, rxBytes: 333,txPackets: 5,rxPackets: 5,lostPackets: 0,timesForwarded: 0],Flow[flowID: 3,timeFirstTxPacket: 1021768610,timeLastTxPacket: 1332941904,timeFirstRxPacket: 1031802209,timeLastRxPacket: 1343457103,delaySum: 51335996,jitterSum: 481600,lastDelay: 10515199,txBytes: 1082, rxBytes: 1082,txPackets: 5,rxPackets: 5,lostPackets: 0,timesForwarded: 0],Flow[flowID: 4,timeFirstTxPacket: 1025293080,timeLastTxPacket: 1332969282,timeFirstRxPacket: 1035326679,timeLastRxPacket: 1343226881,delaySum: 40615996,jitterSum: 291200,lastDelay: 10257599,txBytes: 762, rxBytes: 762,txPackets: 4,rxPackets: 4,lostPackets: 0,timesForwarded: 0]],Ipv4FlowClassifier[Flow[flowId: 4, sourceAddress: 10.1.0.1, destinationAddress: 10.1.0.2, protocol: 6],Flow[flowId: 3, sourceAddress: 10.1.0.1, destinationAddress: 10.1.1.2, protocol: 6],Flow[flowId: 2, sourceAddress: 10.1.0.2, destinationAddress: 10.1.0.1, protocol: 6],Flow[flowId: 1, sourceAddress: 10.1.1.2, destinationAddress: 10.1.0.1, protocol: 6]],FlowProbes[FlowProbe[index: 0, stats: <FlowStats/kokuks.flowmon.FlowProbeFlowStats@89cd21[flowId: 0, packets: 0, bytes: 0, delayFromFirstProbeSum: 0],FlowStats/kokuks.flowmon.FlowProbeFlowStats@849f16[flowId: 0, packets: 0, bytes: 0, delayFromFirstProbeSum: 0],FlowStats/kokuks.flowmon.FlowProbeFlowStats@825c9d[flowId: 0, packets: 0, bytes: 0, delayFromFirstProbeSum: 0],FlowStats/kokuks.flowmon.FlowProbeFlowStats@1577d9b[flowId: 0, packets: 0, bytes: 0, delayFromFirstProbeSum: 0]>],FlowProbe[index: 1, stats: <FlowStats/kokuks.flowmon.FlowProbeFlowStats@8097e5[flowId: 0, packets: 0, bytes: 0, delayFromFirstProbeSum: 0],FlowStats/kokuks.flowmon.FlowProbeFlowStats@745c78[flowId: 0, packets: 0, bytes: 0, delayFromFirstProbeSum: 0]>],FlowProbe[index: 2, stats: <FlowStats/kokuks.flowmon.FlowProbeFlowStats@162e843[flowId: 0, packets: 0, bytes: 0, delayFromFirstProbeSum: 0],FlowStats/kokuks.flowmon.FlowProbeFlowStats@7e9dfc[flowId: 0, packets: 0, bytes: 0, delayFromFirstProbeSum: 0]>]]]

注意内存值——这很奇怪。整个 toString 输出(来自转换后的 XML 根元素)位于最后一行。

无论如何,谢谢你的帮助,
克里斯

编辑:我为变量设置了一些不同的默认值,而不是 0:

protected final FlowProbe parent;
protected int flowId = 1000;
protected int packets = 1000;
protected int bytes = 1000;
protected long delayFromFirstProbeSum = 1000;
protected boolean set = false;

这是设置过程中的结果:

ahaaaa1:: FlowStats/kokuks.flowmon.FlowProbeFlowStats@1156508[flowId: 0, packets: 0, bytes: 0, delayFromFirstProbeSum: 0]
kokuks.flowmon.FlowProbeFlowStats@1156508: AH flowId: 4
kokuks.flowmon.FlowProbeFlowStats@1156508: AH packets: 5
kokuks.flowmon.FlowProbeFlowStats@1156508: AH bytes: 1082
kokuks.flowmon.FlowProbeFlowStats@1156508: AH delayFromFirstProbeSum: 0ns
ahaaaa2:: FlowStats/kokuks.flowmon.FlowProbeFlowStats@1156508[flowId: 4, packets: 5, bytes: 1082, delayFromFirstProbeSum: 0]

所以最初发生了一些奇怪的事情,值是 0 而不是 1000。非常奇怪。当涉及到最终输出时,正在输出的对象的值是 1000 而不是 0(字段默认值的初始值)。

Edit2:我现在在标题中有以下内容:

protected int countdown = 4;

设置属性时递减。一个例子是:

        new AttributeHandler("packets") {
            @Override
            public void perform(String argValue) {
                synchronized (FlowProbeFlowStats.this) {
                    System.out.println(FlowProbeFlowStats.super.toString() + ": AH packets: " + argValue);
                    packets = Integer.parseInt(argValue);
                    System.out.println("packets set to " + packets);
                    set = true;
                    countdown--;
                }
            }
        },

oString 语句现在是:

/* (non-Javadoc)
 * @see java.lang.Object#toString()
 */
@Override
public String toString() {
    return "FlowStats/" + super.toString() + "[flowId: " + flowId + ", packets: " + packets +
    ", bytes: " + bytes + ", delayFromFirstProbeSum: " + delayFromFirstProbeSum +
    ", countdown: " + countdown + "]";
}

打印出来的内容如下:

ahaaaa2:: FlowStats/kokuks.flowmon.FlowProbeFlowStats@1b27882[flowId: 1, packets: 5, bytes: 333, delayFromFirstProbeSum: 0, countdown: -4]

...

[kks-j~KokuKS~/Names] fmxml:FlowMonitor[FlowStats[Flow[flowID: 1,timeFirstTxPacket: 1007829288,timeLastTxPacket: 1524157706,timeFirstRxPacket: 1017862887,timeLastRxPacket: 1534191305,delaySum: 50274395,jitterSum: 212800,lastDelay: 10033599,txBytes: 333, rxBytes: 333,txPackets: 5,rxPackets: 5,lostPackets: 0,timesForwarded: 0],Flow[flowID: 2,timeFirstTxPacket: 1010210025,timeLastTxPacket: 1324387511,timeFirstRxPacket: 1020243624,timeLastRxPacket: 1334421110,delaySum: 50274395,jitterSum: 212800,lastDelay: 10033599,txBytes: 333, rxBytes: 333,txPackets: 5,rxPackets: 5,lostPackets: 0,timesForwarded: 0],Flow[flowID: 3,timeFirstTxPacket: 1017862887,timeLastTxPacket: 1313900107,timeFirstRxPacket: 1027896486,timeLastRxPacket: 1324157706,delaySum: 40615996,jitterSum: 291200,lastDelay: 10257599,txBytes: 762, rxBytes: 762,txPackets: 4,rxPackets: 4,lostPackets: 0,timesForwarded: 0],Flow[flowID: 4,timeFirstTxPacket: 1020243624,timeLastTxPacket: 1313872312,timeFirstRxPacket: 1030277223,timeLastRxPacket: 1324387511,delaySum: 51335996,jitterSum: 481600,lastDelay: 10515199,txBytes: 1082, rxBytes: 1082,txPackets: 5,rxPackets: 5,lostPackets: 0,timesForwarded: 0]],Ipv4FlowClassifier[Flow[flowId: 4, sourceAddress: 10.1.0.1, destinationAddress: 10.1.0.2, protocol: 6],Flow[flowId: 3, sourceAddress: 10.1.0.1, destinationAddress: 10.1.1.2, protocol: 6],Flow[flowId: 2, sourceAddress: 10.1.0.2, destinationAddress: 10.1.0.1, protocol: 6],Flow[flowId: 1, sourceAddress: 10.1.1.2, destinationAddress: 10.1.0.1, protocol: 6]],FlowProbes[FlowProbe[index: 0, stats: <FlowStats/kokuks.flowmon.FlowProbeFlowStats@1b0d2d0[flowId: 1000, packets: 1000, bytes: 1000, delayFromFirstProbeSum: 1000, countdown: 4],FlowStats/kokuks.flowmon.FlowProbeFlowStats@83df14[flowId: 1000, packets: 1000, bytes: 1000, delayFromFirstProbeSum: 1000, countdown: 4],FlowStats/kokuks.flowmon.FlowProbeFlowStats@1aabc29[flowId: 1000, packets: 1000, bytes: 1000, delayFromFirstProbeSum: 1000, countdown: 4],FlowStats/kokuks.flowmon.FlowProbeFlowStats@2758d0[flowId: 1000, packets: 1000, bytes: 1000, delayFromFirstProbeSum: 1000, countdown: 4]>],FlowProbe[index: 1, stats: <FlowStats/kokuks.flowmon.FlowProbeFlowStats@137e19e[flowId: 1000, packets: 1000, bytes: 1000, delayFromFirstProbeSum: 1000, countdown: 4],FlowStats/kokuks.flowmon.FlowProbeFlowStats@1bd5f28[flowId: 1000, packets: 1000, bytes: 1000, delayFromFirstProbeSum: 1000, countdown: 4]>],FlowProbe[index: 2, stats: <FlowStats/kokuks.flowmon.FlowProbeFlowStats@1b27882[flowId: 1000, packets: 1000, bytes: 1000, delayFromFirstProbeSum: 1000, countdown: 4],FlowStats/kokuks.flowmon.FlowProbeFlowStats@5b84b[flowId: 1000, packets: 1000, bytes: 1000, delayFromFirstProbeSum: 1000, countdown: 4]>]]]

属性设置器和 toString 中的同步语句用于检查 JVM 错误,但它没有做任何事情。奇怪吧?

编辑3:

我将字段更改为 Atomic*。我必须认真地把它归结为一个 JVM 错误,因为原子字段是最终的,并且在内部由默认值设置,我得到了 NullPointerException。

protected final AtomicInteger flowId = new AtomicInteger();
protected final AtomicInteger packets = new AtomicInteger();
protected final AtomicInteger bytes = new AtomicInteger();
protected final AtomicLong delayFromFirstProbeSum = new AtomicLong();

例外:

Exception in thread "FlowMonitor Updater" java.lang.NullPointerException
    at kokuks.flowmon.FlowProbeFlowStats.toString(FlowProbeFlowStats.java:95)

编辑 4:我从 JDK1.7 更改为 1.6 的稳定版本,同样的问题。

我刚刚想过,也许在构造函数期间未设置字段,然后再设置。我不确定初始化的顺序,但在这种情况下可能搞砸了。无论如何,我会把它放在一个 init 方法中,看看它是否有任何作用。奇怪的是,它在单个类的情况下不起作用。它适用于其他人。

4

2 回答 2

1

您的输出有些奇怪:带有前缀的行ahaaaa2似乎在带有前缀的行之前打印ahaaaa1。然而,事实上,ahaaaa2线条来自上一组值,而ahaaaa1线条来自下一组值。

带有前缀的行ahaaaa1是具有默认值 (0) 的行。正如我所料,这些行ahaaaa2似乎具有正确的值。我认为如果您在 之后添加一两个换行符System.out.println("ahaaaa2:: " + this.toString()),您会看到输出是正确的。


编辑:好的,我终于看到了这个问题。在(非常难以阅读!)最后一行输出的末尾,您再次打印出所有统计信息。您希望在哪里看到这个:

FlowStats/kokuks.flowmon.FlowProbeFlowStats@1577d9b[flowId: 4, packets: 4, bytes: 762, delayFromFirstProbeSum: 0]

你反而得到:

FlowStats/kokuks.flowmon.FlowProbeFlowStats@1577d9b[flowId: 0, packets: 0, bytes: 0, delayFromFirstProbeSum: 0]

对于其他 FlowProbeFlowStats 对象,依此类推

我不知道发生了什么,但我也看不到最后一行是在哪里产生的,同时 FlowProbeFlowStats 对象可能会发生什么。在没有看到更多代码的情况下,我认为我无能为力。

于 2011-02-02T20:52:04.543 回答
0

通过将设置移动到 init 方法来解决问题,结果证明在构造函数中执行所有操作都会发生一些奇怪的事情。因此,当我尝试访问它们时,为什么 Atomic* 字段为空。发生这种情况是因为它们尚未设置,因为对父构造函数的超级调用首先出现。为什么其他元素都很好,我无法理解,但这解决了它。

于 2011-02-04T04:35:23.167 回答