Web / Apache Axiom Interview questions
How do you troubleshoot "already consumed" builder exceptions in Axiom?
This class of error almost always traces back to one of two root causes: calling serializeAndConsume() more than once on the same tree, or attempting to navigate/serialize a tree after its underlying XMLStreamReader has already been closed or fully drained by an earlier operation.
- Check whether
serializeAndConsume()is called more than once, directly or indirectly, on the same element - if the tree needs to be written out more than once, switch toserialize()instead, which caches nodes and leaves the tree reusable. - Check for premature
close()calls on the builder or reader, especially in code with early-return branches or exception handling that might close a resource before all intended navigation has actually happened. - Check for concurrent access - two threads sharing the same builder or tree can race to consume the same underlying stream, since Axiom's default implementation isn't synchronized for this kind of shared mutation.
- If the tree genuinely needs to be read, modified, and then read again, consider calling
build()explicitly up front to fully materialize it into an independent, stream-free structure before further processing.
A useful diagnostic habit is adding a clear comment or a small wrapper at every call site that uses serializeAndConsume(), explicitly noting that the tree becomes unusable afterward - most instances of this bug come from a later code change adding a second read or write path without realizing the first one had already consumed the stream.
More Related questions...