在JAXB unmarshaller下的sun.misc.URLClassPath.getLoader中的高锁争用

时间:2012-07-05 12:08:49

标签: java jaxb jaxp

我们在weblogic上运行这段特殊代码,它的功能是从XML输入字符串返回特定于输入类的java对象。代码本身将由多个线程(50 +)使用。

public static Object toXMLObject(String XMLString, Class xmlClass)
        throws Exception {

    StringReader strReader = null;

    try {

        JAXBContext context = JAXBContexts.getJAXBContext(xmlClass); //Cached JAXBContext
        Unmarshaller unmarshaller = context.createUnmarshaller();

        strReader = new StringReader(XMLString);

        return unmarshaller.unmarshal(strReader);

    } catch(Exception e){
        throw e;
    } finally {
        if(strReader != null){
            strReader.close();
        }
    }

}

我们从线程转储中看到的是多个线程(51个线程)试图锁定单个对象

    ExecuteThread: '52' for queue: 'automation'" daemon prio=3 tid=0x0000000103bcf800 nid=0x1a4 waiting for monitor entry [0xfffffffac2cfb000]**
     java.lang.Thread.State: BLOCKED (on object monitor)
at sun.misc.URLClassPath.getLoader(URLClassPath.java:279)
- locked <0xfffffffb89f00ed8> (a sun.misc.URLClassPath)
at sun.misc.URLClassPath.findResource(URLClassPath.java:145)
at java.net.URLClassLoader$2.run(URLClassLoader.java:385)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findResource(URLClassLoader.java:382)
at java.lang.ClassLoader.getResource(ClassLoader.java:1002)
at java.lang.ClassLoader.getResource(ClassLoader.java:997)
at java.lang.ClassLoader.getResource(ClassLoader.java:997)
at weblogic.utils.classloaders.GenericClassLoader.getResourceInternal(GenericClassLoader.java:168)
at weblogic.utils.classloaders.GenericClassLoader.getResource(GenericClassLoader.java:182)
at weblogic.utils.classloaders.FilteringClassLoader.getResourceInternal(FilteringClassLoader.java:129)
at weblogic.utils.classloaders.GenericClassLoader.getResourceInternal(GenericClassLoader.java:154)
at weblogic.utils.classloaders.GenericClassLoader.getResource(GenericClassLoader.java:182)
at java.lang.ClassLoader.getResourceAsStream(ClassLoader.java:1192)
at org.apache.xerces.parsers.SecuritySupport$6.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at org.apache.xerces.parsers.SecuritySupport.getResourceAsStream(Unknown Source)
at org.apache.xerces.parsers.ObjectFactory.findJarServiceProvider(Unknown Source)
at org.apache.xerces.parsers.ObjectFactory.createObject(Unknown Source)
at org.apache.xerces.parsers.ObjectFactory.createObject(Unknown Source)
at org.apache.xerces.parsers.SAXParser. (Unknown Source)
at org.apache.xerces.parsers.SAXParser. (Unknown Source)
at org.apache.xerces.jaxp.SAXParserImpl$JAXPSAXParser. (Unknown Source)
at org.apache.xerces.jaxp.SAXParserImpl. (Unknown Source)
at org.apache.xerces.jaxp.SAXParserFactoryImpl.newSAXParser(Unknown Source)
at weblogic.xml.jaxp.RegistrySAXParser. (RegistrySAXParser.java:65)
at weblogic.xml.jaxp.RegistrySAXParser. (RegistrySAXParser.java:46)
at weblogic.xml.jaxp.RegistrySAXParserFactory.newSAXParser(RegistrySAXParserFactory.java:91)
at javax.xml.bind.helpers.AbstractUnmarshallerImpl.getXMLReader(AbstractUnmarshallerImpl.java:86)
at javax.xml.bind.helpers.AbstractUnmarshallerImpl.unmarshal(AbstractUnmarshallerImpl.java:137)
at javax.xml.bind.helpers.AbstractUnmarshallerImpl.unmarshal(AbstractUnmarshallerImpl.java:194)
at com.util.XMLParserUtil.toXMLObject(XMLParserUtil.java:699)

  ExecuteThread: '78' for queue: 'automation'" daemon prio=3 tid=0x000000010363b800 nid=0x1be waiting for monitor entry [0xfffffffabf8fb000]**
   java.lang.Thread.State: BLOCKED (on object monitor)
at sun.misc.URLClassPath.getLoader(URLClassPath.java:279)
- waiting to lock <0xfffffffb89f00ed8> (a sun.misc.URLClassPath)
at sun.misc.URLClassPath.findResource(URLClassPath.java:145)
at java.net.URLClassLoader$2.run(URLClassLoader.java:385)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findResource(URLClassLoader.java:382)
at java.lang.ClassLoader.getResource(ClassLoader.java:1002)
at java.lang.ClassLoader.getResource(ClassLoader.java:997)
at java.lang.ClassLoader.getResource(ClassLoader.java:997)
at weblogic.utils.classloaders.GenericClassLoader.getResourceInternal(GenericClassLoader.java:168)
at weblogic.utils.classloaders.GenericClassLoader.getResource(GenericClassLoader.java:182)
at weblogic.utils.classloaders.FilteringClassLoader.getResourceInternal(FilteringClassLoader.java:129)
at weblogic.utils.classloaders.GenericClassLoader.getResourceInternal(GenericClassLoader.java:154)
at weblogic.utils.classloaders.GenericClassLoader.getResource(GenericClassLoader.java:182)
at java.lang.ClassLoader.getResourceAsStream(ClassLoader.java:1192)
at org.apache.xerces.parsers.SecuritySupport$6.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at org.apache.xerces.parsers.SecuritySupport.getResourceAsStream(Unknown Source)
at org.apache.xerces.parsers.ObjectFactory.findJarServiceProvider(Unknown Source)
at org.apache.xerces.parsers.ObjectFactory.createObject(Unknown Source)
at org.apache.xerces.parsers.ObjectFactory.createObject(Unknown Source)
at org.apache.xerces.parsers.SAXParser. (Unknown Source)
at org.apache.xerces.parsers.SAXParser. (Unknown Source)
at org.apache.xerces.jaxp.SAXParserImpl$JAXPSAXParser. (Unknown Source)
at org.apache.xerces.jaxp.SAXParserImpl. (Unknown Source)
at org.apache.xerces.jaxp.SAXParserFactoryImpl.newSAXParser(Unknown Source)
at weblogic.xml.jaxp.RegistryXMLReader.getXMLReader(RegistryXMLReader.java:523)
at weblogic.xml.jaxp.RegistryXMLReader.getXMLReaderInternal(RegistryXMLReader.java:453)
at weblogic.xml.jaxp.RegistryXMLReader.parse(RegistryXMLReader.java:158)
at com.sun.xml.bind.v2.runtime.unmarshaller.UnmarshallerImpl.unmarshal0(UnmarshallerImpl.java:211)
at com.sun.xml.bind.v2.runtime.unmarshaller.UnmarshallerImpl.unmarshal(UnmarshallerImpl.java:184)
at javax.xml.bind.helpers.AbstractUnmarshallerImpl.unmarshal(AbstractUnmarshallerImpl.java:137)
at javax.xml.bind.helpers.AbstractUnmarshallerImpl.unmarshal(AbstractUnmarshallerImpl.java:194)
at com.util.XMLParserUtil.toXMLObject(XMLParserUtil.java:699)

我们是否正确实施了JAXB代码?我们怎么能克服这个问题呢。 PS。我们用JDK1.6.0_33

上的最新版本(1.4.6)覆盖了JAXP

3 个答案:

答案 0 :(得分:1)

您可以尝试执行以下操作:

package forum11344031;

import java.io.StringReader;
import javax.xml.bind.*;
import javax.xml.stream.*;

public class Demo {

    private static final XMLInputFactory XIF = XMLInputFactory.newFactory();

    public static Object toXMLObject(String XMLString, Class xmlClass)
            throws Exception {
        Object o;
        StringReader strReader = null;
        try {
            JAXBContext context = JAXBContexts.getJAXBContext(xmlClass); //Cached JAXBContext
            Unmarshaller unmarshaller = context.createUnmarshaller();

            strReader = new StringReader(XMLString);
            XMLStreamReader xmlStreamReader = XIF.createXMLStreamReader(strReader);
            o = unmarshaller.unmarshal(xmlStreamReader);
            xmlStreamReader.close();
        } catch(Exception e){
            throw e;
        } finally {
            if(strReader != null){
                strReader.close();
            }
        }
        return o;
    }

}

答案 1 :(得分:0)

这个怎么样,这会有帮助吗?

“ - Djavax.xml.parsers.SAXParserFactory = org.apache.xerces.jaxp.SAXParserFactoryImpl”

根据幻灯片#49 http://www.slideshare.net/sjlee0/robust-and-scalable-concurrent-programming-lesson-from-the-trenches

答案 2 :(得分:0)

感谢。我发现演示文稿“强大且可扩展的并发编程”非常有用。我已经成功测试了49-50页的解决方案。现在我没有在ObjectFactory.createObject方法中看到锁。

神奇的解决方案是为每个线程保留不同的SAXParser。

static private final ThreadLocal<SAXParser> sps = new ThreadLocal<SAXParser>();

public static SAXParser getParser() throws ParserConfigurationException, SAXException {
    SAXParser parser = sps.get();
    if (parser == null) {
        parser = spf.newSAXParser();
        sps.set(parser);
    }
    return parser;
}