javaJDK8中的transform相关类,线程不安全导致的错误

作者: admin 分类: 异常处理 发布时间: 2021-03-05 09:15  阅读: 512 views

最近在做相关word中xml解析的功能工作,在并发操作时,经常会碰到报错如下:

解析word中的mathML时遇到。

javax.xml.transform.TransformerException: java.lang.IndexOutOfBoundsException: Index: 24, Size: 23
    at com.sun.org.apache.xalan.internal.xsltc.trax.TransformerImpl.transform(TransformerImpl.java:746)
    at com.sun.org.apache.xalan.internal.xsltc.trax.TransformerImpl.transform(TransformerImpl.java:351)
    at com.ewt360.questionproduceprod.service.word.util.NodeUtil.getMathMl(NodeUtil.java:468)
    at com.ewt360.questionproduceprod.service.word.handler.MathMlHandler.extract(MathMlHandler.java:71)
    at com.ewt360.questionproduceprod.service.word.DocContext.packageResult(DocContext.java:263)
    at com.ewt360.questionproduceprod.service.word.DocContext.lambda$parseWordContent$2(DocContext.java:120)
    at java.util.concurrent.CompletableFuture$AsyncSupply.run(CompletableFuture.java:1590)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
    at java.lang.Thread.run(Thread.java:745)
Caused by: java.lang.IndexOutOfBoundsException: Index: 24, Size: 23
    at java.util.ArrayList.rangeCheckForAdd(ArrayList.java:661)
    at java.util.ArrayList.add(ArrayList.java:473)
    at com.sun.org.apache.xalan.internal.xsltc.runtime.AbstractTranslet.pushParamFrame(AbstractTranslet.java:158)
    at GregorSamsa.template$dot$1()
    at GregorSamsa.applyTemplates()
    at GregorSamsa.transform()
    at com.sun.org.apache.xalan.internal.xsltc.runtime.AbstractTranslet.transform(AbstractTranslet.java:620)
    at com.sun.org.apache.xalan.internal.xsltc.trax.TransformerImpl.transform(TransformerImpl.java:739)
    at com.sun.org.apache.xalan.internal.xsltc.trax.TransformerImpl.transform(TransformerImpl.java:351)
    at com.ewt360.questionproduceprod.service.word.util.NodeUtil.getMathMl(NodeUtil.java:468)
    at com.ewt360.questionproduceprod.service.word.handler.MathMlHandler.extract(MathMlHandler.java:71)
    at com.ewt360.questionproduceprod.service.word.DocContext.packageResult(DocContext.java:263)
    at com.ewt360.questionproduceprod.service.word.DocContext.lambda$parseWordContent$2(DocContext.java:120)
    at java.util.concurrent.CompletableFuture$AsyncSupply.run(CompletableFuture.java:1590)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
    at java.lang.Thread.run(Thread.java:745)

或者如下错误:

MathML转Latex时遇到

java.util.concurrent.CompletionException: java.lang.VerifyError: (class: GregorSamsa, method: replaceEntities signature: (Lcom/sun/org/apache/xalan/internal/xsltc/DOM;Lcom/sun/org/apache/xml/internal/dtm/DTMAxisIterator;Lcom/sun/org/apache/xml/internal/serializer/SerializationHandler;ILjava/lang/Object;)V) Illegal target of jump or branch
    at java.util.concurrent.CompletableFuture.encodeThrowable(CompletableFuture.java:273)
    at java.util.concurrent.CompletableFuture.completeThrowable(CompletableFuture.java:280)
    at java.util.concurrent.CompletableFuture$AsyncSupply.run(CompletableFuture.java:1592)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
    at java.lang.Thread.run(Thread.java:745)
Caused by: java.lang.VerifyError: (class: GregorSamsa, method: replaceEntities signature: (Lcom/sun/org/apache/xalan/internal/xsltc/DOM;Lcom/sun/org/apache/xml/internal/dtm/DTMAxisIterator;Lcom/sun/org/apache/xml/internal/serializer/SerializationHandler;ILjava/lang/Object;)V) Illegal target of jump or branch
    at java.lang.Class.getDeclaredConstructors0(Native Method)
    at java.lang.Class.privateGetDeclaredConstructors(Class.java:2671)
    at java.lang.Class.getConstructor0(Class.java:3075)
    at java.lang.Class.newInstance(Class.java:412)
    at com.sun.org.apache.xalan.internal.xsltc.trax.TemplatesImpl.getTransletInstance(TemplatesImpl.java:455)
    at com.sun.org.apache.xalan.internal.xsltc.trax.TemplatesImpl.newTransformer(TemplatesImpl.java:486)
    at com.sun.org.apache.xalan.internal.xsltc.trax.TransformerFactoryImpl.newTransformer(TransformerFactoryImpl.java:792)
    at com.ewt360.questionproduceprod.service.common.Math2Latex.mml2latexTransformer(Math2Latex.java:58)
    at com.ewt360.questionproduceprod.service.common.Math2Latex.getLatex(Math2Latex.java:40)
    at com.ewt360.questionproduceprod.service.biz.service.impl.ConvertServiceImpl.replaceRidTag(ConvertServiceImpl.java:573)
    at com.ewt360.questionproduceprod.service.biz.service.impl.ConvertServiceImpl.doReplaceQuestionML(ConvertServiceImpl.java:379)
    at com.ewt360.questionproduceprod.service.biz.service.impl.ConvertServiceImpl.lambda$null$1(ConvertServiceImpl.java:201)
    at java.util.concurrent.CompletableFuture$AsyncSupply.run(CompletableFuture.java:1590)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
    at java.lang.Thread.run(Thread.java:745)

通过查询可以看到stackoverflow上也有相关释义

地址: https://stackoverflow.com/questions/49758534/java-lang-verifyerror-class-gregorsamsa-method-illegal-target-of-jump

核心代码块,只放部分如下:

//创建部分
@Bean
public Transformer transformer() {
    Transformer transformer = null;
    try {
        TransformerFactory tFactory = TransformerFactory.newInstance();
        StreamSource styleSource = null;
        ClassPathResource resource = new ClassPathResource("xsl/OMML2MML.XSL");
        BufferedReader reader = new BufferedReader(new InputStreamReader(resource.getInputStream()));
        styleSource = new StreamSource(reader);
        transformer = tFactory.newTransformer(styleSource);
    } catch (Exception e) {
        log.error("errorinfo {}",e);
    }
    return transformer;
}


//调用部分
DOMSource source = new DOMSource(node);
StringWriter stringwriter = new StringWriter();
StreamResult result = new StreamResult(stringwriter);
transformer.setOutputProperty("omit-xml-declaration", "yes");
transformer.transform(source, result);

最终处理方式为:
在调用时,对方法加锁,或者针对transformer实例每次调用时都创建一次。
可以解决报错问题。

注:应该在jdk8、jdk9包括之前版本。在并发时都会产生该问题。请合理使用及调用。


   原创文章,转载请标明本文链接: javaJDK8中的transform相关类,线程不安全导致的错误

如果觉得我的文章对您有用,请随意打赏。您的支持将鼓励我继续创作!

发表评论

电子邮件地址不会被公开。 必填项已用*标注