FWK005 parse may not be called while parsing

1、报错原因:(程序运行时有多个线程同时使用一个DocumentBuilder对象,报错)

程序当前DocumentBuilder对象正在转换文档,此时再次请求转换文档,那么直接抛出XNIException(“FWK005 parse may not be called while parsing.”);异常。

2、检查DocumentBuilder 在程序中定义是不是使用static 变量或者全局变量,造成以上错误, 改成局部变量可以测一下。

参考地址:https://blog.csdn.net/caolaosanahnu/article/details/8779397 

3、使用ThreadLocal 解决办法:

自定义一个线程助手类,将实例化的DocumentBuilder放在当前线程中:例如 DocumentHolder类

public class DocumentHolder {
    
    private static ThreadLocal<DocumentBuilder> docBuildeIns = new ThreadLocal<DocumentBuilder>() {
        protected DocumentBuilder initialValue() {
            try {
                return DocumentBuilderFactory.newInstance().newDocumentBuilder();
            } catch (ParserConfigurationException e) {
                String msg = "DocumentBuilder 对象初始化失败!";
//                log.error(msg, e);
                throw new IllegalStateException(msg, e);
            }
        }
    };
    public static DocumentBuilder getDocumentBuilder() {
        return  docBuildeIns.get();
    }
    
    public static void set(DocumentBuilder documentBuilder) {
        docBuildeIns.set(documentBuilder);
    }

}

调用方法:

①、将DocumentHolder放入到当前线程: DocumentHolder.set(DocumentBuilder documentBuilder)

②、DocumentHolder.docBuildIns.get().parse(File);

4、通用的ThreadLocal,使用泛型类型。

猜你喜欢

转载自blog.csdn.net/qwer123456u/article/details/106411305