com.sun.xml.bind.v2.runtime.unmarshaller.UnmarshallerImpl.unmarshal(UnmarshallerImpl.java:241) 多线程

29 篇文章 1 订阅
  1. public class AppsImport {   
  2.   
  3.     private static final BranchService BRANCH_SERVICE = MinasServiceLocator.getBranchService();   
  4.     private static final String        token          = TokenService.getToken();   
  5.     private static final String        REPO_INTL      = "intl";   
  6.   
  7.     public static void main(String[] args) {   
  8.         InputStream in = ClassLoader.getSystemResourceAsStream("app_name.txt");   
  9.         BufferedReader bufferReader = new BufferedReader(new InputStreamReader(in));   
  10.   
  11.         String tempString = null;   
  12.         try {   
  13.             tempString = bufferReader.readLine();   
  14.             String[] appNames = tempString.split(",");   
  15.             for (int i = 0; i < appNames.length; i++) {   
  16.                 appNames[i] = appNames[i].substring(1, appNames[i].length() - 2);   
  17.             }   
  18.             bufferReader.close();   
  19.         } catch (IOException e) {   
  20.             e.printStackTrace();   
  21.         }   
  22.     }   
  23. }  
  1.  

    Thread.currendThread().getName()
    这个可以返回线程对象的ID

     

    http://jira.codehaus.org/browse/XFIRE-1033

    We have an intermittent bug that stops our webservice from accepting requests - it has occurred approx once a week thus far, i.e. unfortunately we can not reproduce it.

    Basically something appears to trigger the processing of the XML to throw an nullpointerexception and then from there on all calls to that service fail. The first time it happened it stayed throwing nullpointer exceptions for 1 hour. The second time we were in the office - the only thing to stop it from failing was actually to query the WSDL, ie. ....Service?WSDL - This seemed to trigger a reset of the service - all calls worked properly afterwards.

    This is the stack trace of the first exceptions we see in the logs when this happens (inside xerces code):

    2007-06-11 15:28:33,859 ERROR http-80-4 org.codehaus.xfire.handler.DefaultFaultHandler - Fault occurred!
    java.lang.NullPointerException
    at com.sun.org.apache.xerces.internal.impl.XMLEntityScanner.load(XMLEntityScanner.java:1750)
    at com.sun.org.apache.xerces.internal.impl.XMLEntityScanner.skipSpaces(XMLEntityScanner.java:1492)
    at com.sun.org.apache.xerces.internal.impl.XMLDocumentScannerImpl$TrailingMiscDriver.next(XMLDocumentScannerImpl.java:1371)
    at com.sun.org.apache.xerces.internal.impl.XMLDocumentScannerImpl.next(XMLDocumentScannerImpl.java:645)
    at com.sun.org.apache.xerces.internal.impl.XMLNSDocumentScannerImpl.next(XMLNSDocumentScannerImpl.java:140)
    at com.sun.org.apache.xerces.internal.impl.XMLStreamReaderImpl.next(XMLStreamReaderImpl.java:548)
    at org.codehaus.xfire.soap.handler.ReadHeadersHandler.invoke(ReadHeadersHandler.java:44)
    at org.codehaus.xfire.handler.HandlerPipeline.invoke(HandlerPipeline.java:131)
    at org.codehaus.xfire.transport.DefaultEndpoint.onReceive(DefaultEndpoint.java:64)
    at org.codehaus.xfire.transport.AbstractChannel.receive(AbstractChannel.java:38)
    at org.codehaus.xfire.transport.http.XFireServletController.invoke(XFireServletController.java:304)
    at org.codehaus.xfire.transport.http.XFireServletController.doService(XFireServletController.java:129)
    at org.codehaus.xfire.spring.remoting.XFireServletControllerAdapter.handleRequest(XFireServletControllerAdapter.java:67)
    at org.codehaus.xfire.spring.remoting.XFireExporter.handleRequest(XFireExporter.java:48)
    at org.springframework.web.servlet.mvc.SimpleControllerHandlerAdapter.handle(SimpleControllerHandlerAdapter.java:45)
    at ....

    Then all subsequent calls to the service fail with the following stack trace:

    2007-06-11 15:28:47,734 ERROR http-80-14 org.codehaus.xfire.handler.DefaultFaultHandler - Fault occurred!
    java.lang.NullPointerException
    at com.sun.org.apache.xerces.internal.impl.XMLEntityScanner.arrangeCapacity(XMLEntityScanner.java:1598)
    at com.sun.org.apache.xerces.internal.impl.XMLEntityScanner.skipString(XMLEntityScanner.java:1684)
    at com.sun.org.apache.xerces.internal.impl.XMLDocumentScannerImpl$XMLDeclDriver.next(XMLDocumentScannerImpl.java:785)
    at com.sun.org.apache.xerces.internal.impl.XMLDocumentScannerImpl.next(XMLDocumentScannerImpl.java:645)
    at com.sun.org.apache.xerces.internal.impl.XMLNSDocumentScannerImpl.next(XMLNSDocumentScannerImpl.java:140)
    at com.sun.org.apache.xerces.internal.impl.XMLStreamReaderImpl.next(XMLStreamReaderImpl.java:548)
    at org.codehaus.xfire.soap.handler.ReadHeadersHandler.invoke(ReadHeadersHandler.java:44)
    at org.codehaus.xfire.handler.HandlerPipeline.invoke(HandlerPipeline.java:131)
    at org.codehaus.xfire.transport.DefaultEndpoint.onReceive(DefaultEndpoint.java:64)
    at org.codehaus.xfire.transport.AbstractChannel.receive(AbstractChannel.java:38)
    at org.codehaus.xfire.transport.http.XFireServletController.invoke(XFireServletController.java:304)
    at org.codehaus.xfire.transport.http.XFireServletController.doService(XFireServletController.java:129)
    at org.codehaus.xfire.spring.remoting.XFireServletControllerAdapter.handleRequest(XFireServletControllerAdapter.java:67)
    at org.codehaus.xfire.spring.remoting.XFireExporter.handleRequest(XFireExporter.java:48)
    at org.springframework.web.servlet.mvc.SimpleControllerHandlerAdapter.handle(SimpleControllerHandlerAdapter.java:45)
    .....

    Looking at the Xerces code the first failure appears to occur when accessing the 'fCurrentEntity' object, however two lines above it has already accessed this so it appears like a possible threading issue?

    Anybody seen this before or know how we might look to resolve it?

    I have attached our xfire-servlet.xml file - note the fault is occurring in our NovaWebService service (FYI The service NovaMaintWebservice uses a local custom type mapping registry in order to use the same POJOS but use annotations for NovaWebService & aegis mapping files for NovaMaintWebService).

    Thanks,
    Ted.

     

     

    ============================

    http://old.uplook.cn/biancheng/139/1390236/

    在多线程环境中,应每次new一个Unmarshaller对象实例保证线程安全,否则可以使用Unmarshaller的静态方法。

    http://www.docjar.com/html/api/com/sun/xml/internal/bind/v2/runtime/unmarshaller/UnmarshallerImpl.java.html

    http://www.2cto.com/kf/201207/143011.html

    1. Jaxb2.0实现Java Object转换Xml转换Java Object.

     

    [java]
    //测试类 
    [java] 
    <pre class="java" name="code">import com.toft.webservice.client.domino.DominoObject; 
    import com.toft.webservice.core.JaxbBinder; 
     
     
    public class JaxbTest { 
         
        private static final String DECLARATION = "<?xml version='1.0' encoding='utf-8'?><!DOCTYPE document SYSTEM 'xmlschemas/domino_7_0_1.dtd'>"; 
        private static final String ENCODING = "UTF-8"; 
        private DominoObject dominoObject = new DominoObject(); 
        public static void main(String[] args){ 
             
        } 
         
        /**
         *  使用JAXB生成符合接口的XML.
         * @return String
         * @throws Exception
         */ 
        private String getDominoXml() throws Exception { 
            try { 
                JaxbBinder binder = new JaxbBinder(DominoObject.class); 
                return binder.toXml(dominoObject, ENCODING, DECLARATION); 
            } catch (Exception e) { 
                throw e; 
            } 
        } 

    </pre><br> 
    <pre></pre> 
    <pre class="java" name="code"> </pre><pre class="java" name="code">//工具类</pre><pre class="java" name="code">import java.io.StringReader; 
    import java.io.StringWriter; 
     
    import javax.xml.bind.JAXBContext; 
    import javax.xml.bind.JAXBException; 
    import javax.xml.bind.Marshaller; 
    import javax.xml.bind.Unmarshaller; 
     
    /**
     * 使用Jaxb2.0实现XML<->Java Object的Binder.
     * 
     * @author 孙祖强
     */ 
    public class JaxbBinder { 
        //多线程安全的Context. 
        private JAXBContext jaxbContext; 
     
        /**
         * @param types 所有需要序列化的Root对象的类型.
         */ 
        public JaxbBinder(Class<?>... types) { 
            try { 
                jaxbContext = JAXBContext.newInstance(types); 
            } catch (JAXBException e) { 
                throw new RuntimeException(e); 
            } 
        } 
     
        /**
         * Java Object->Xml.
         */ 
        public String toXml(Object root) { 
            try { 
                StringWriter writer = new StringWriter(); 
                createMarshaller("UTF-8", null, true).marshal(root, writer); 
                return writer.toString(); 
            } catch (JAXBException e) { 
                throw new RuntimeException(e); 
            } 
        } 
     
        /**
         * Java Object->Xml.
         */ 
        public String toXml(Object root, String encoding) { 
            try { 
                StringWriter writer = new StringWriter(); 
                createMarshaller(encoding, null, false).marshal(root, writer); 
                return writer.toString(); 
            } catch (JAXBException e) { 
                throw new RuntimeException(e); 
            } 
        } 
     
        /**
         * Java Object->Xml.
         */ 
        public String toXml(Object root, String encoding, String declaration) { 
            try { 
                StringWriter writer = new StringWriter(); 
                writer.append(declaration); 
                createMarshaller(encoding, declaration, false).marshal(root, writer); 
                return writer.toString(); 
            } catch (JAXBException e) { 
                throw new RuntimeException(e); 
            } 
        } 
     
        /**
         * Xml->Java Object.
         */ 
        @SuppressWarnings("unchecked") 
        public <T> T fromXml(String xml) { 
            try { 
                StringReader reader = new StringReader(xml); 
                return (T) createUnmarshaller().unmarshal(reader); 
            } catch (JAXBException e) { 
                throw new RuntimeException(e); 
            } 
        } 
     
        /**
         * 创建Marshaller, 设定encoding(可为Null),  设定declaration(可为Null).
         */ 
        public Marshaller createMarshaller(String encoding, String declaration, Boolean formatted) { 
            try { 
                Marshaller marshaller = jaxbContext.createMarshaller(); 
     
                //是否格式化XML 
                if (formatted) { 
                    marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE); 
                } 
     
                //设置编码方式 
                if (null != encoding && !"".equals(encoding)) { 
                    marshaller.setProperty(Marshaller.JAXB_ENCODING, encoding); 
                } 
     
                //设置XML声明 
                if (null != declaration && !"".equals(declaration)) { 
                    marshaller.setProperty("com.sun.xml.bind.xmlDeclaration", Boolean.FALSE); 
                } 
                return marshaller; 
            } catch (JAXBException e) { 
                throw new RuntimeException(e); 
            } 
        } 
     
        /**
         * 创建UnMarshaller.
         */       www.2cto.com
        public Unmarshaller createUnmarshaller() { 
            try { 
                return jaxbContext.createUnmarshaller(); 
            } catch (JAXBException e) { 
                throw new RuntimeException(e); 
            } 
        } 
     

    </pre><br> 
    作者:sunzuqiang

     

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值