Gábor Nagy
Gábor Nagy

Reputation: 189

How to enable non-threadsafe mode of XmlBeans from an Apache POI-based Java application?

I am working on an Excel exporter web application that uses POI to generate Excel documents.

I was profiling this web application with VisualVM and found out that there are a lot of lock acquire/release operations happening under the hood, inside the XmlBeans library that POI is built on. These operations seem to eat up a lot of CPU time - I think unncessarily, because my app always uses a separate thread for rendering.

I also found out that there is an UNSYNCHRONIZED mode in XmlBeans: https://xmlbeans.apache.org/docs/2.6.0/reference/org/apache/xmlbeans/XmlOptions.html#setUnsynchronized()

In the POI source code, the XmlBeans configuration seems to be hard-wired:

public static final XmlOptions DEFAULT_XML_OPTIONS;
static {
    DEFAULT_XML_OPTIONS = new XmlOptions();
    DEFAULT_XML_OPTIONS.setSaveOuter();
    DEFAULT_XML_OPTIONS.setUseDefaultNamespace();
    DEFAULT_XML_OPTIONS.setSaveAggressiveNamespaces();
    DEFAULT_XML_OPTIONS.setCharacterEncoding("UTF-8");
    DEFAULT_XML_OPTIONS.setLoadEntityBytesLimit(4096);

    Map<String, String> map = new HashMap<String, String>();
    map.put("http://schemas.openxmlformats.org/drawingml/2006/main", "a");
    map.put("http://schemas.openxmlformats.org/drawingml/2006/chart", "c");
    map.put("http://schemas.openxmlformats.org/drawingml/2006/wordprocessingDrawing", "wp");
    map.put("http://schemas.openxmlformats.org/markup-compatibility/2006", "ve");
    map.put("http://schemas.openxmlformats.org/officeDocument/2006/math", "m");
    map.put("http://schemas.openxmlformats.org/officeDocument/2006/relationships", "r");
    map.put("http://schemas.openxmlformats.org/officeDocument/2006/docPropsVTypes", "vt");
    map.put("http://schemas.openxmlformats.org/presentationml/2006/main", "p");
    map.put("http://schemas.openxmlformats.org/wordprocessingml/2006/main", "w");
    map.put("http://schemas.microsoft.com/office/word/2006/wordml", "wne");
    map.put("urn:schemas-microsoft-com:office:office", "o");
    map.put("urn:schemas-microsoft-com:office:excel", "x");
    map.put("urn:schemas-microsoft-com:office:word", "w10");
    map.put("urn:schemas-microsoft-com:vml", "v");
    DEFAULT_XML_OPTIONS.setSaveSuggestedPrefixes(Collections.unmodifiableMap(map));
}

As you can see, no DEFAULT_XML_OPTIONS.setUnsynchronized(); call is done there.

My question is: is there any way (even an ugly hack) to enable the UNSYNCHRONIZED mode in POI without forking and re-building it?

Upvotes: 0

Views: 256

Answers (1)

kiwiwings
kiwiwings

Reputation: 3446

Although you could set the unsynchronized option in POIXMLTypeLoader.DEFAULT_XML_OPTIONS from user code, I've applied this change now via #61350 and it will be available in the next POI release, which will be probably POI 4.0.

Upvotes: 2

Related Questions