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,使用泛型类型。

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值