WEB开发网
开发学院软件开发Java CXF client在并发下的线程安全问题 阅读

CXF client在并发下的线程安全问题

 2009-09-24 00:00:00 来源:WEB开发网   
核心提示: * Session support - if you turn on sessions support (see jaxws spec), the session cookie is stored in the conduit. Thus, it would fall into the abov

* Session support - if you turn on sessions support (see jaxws spec), the session cookie is stored in the conduit. Thus, it would fall into the above rules on conduit settings and thus be shared across threads.

For the conduit issues, you COULD install a new ConduitSelector that uses a thread local or similar. That's a bit complex though.

For most "simple" use cases, you can use CXF proxies on multiple threads. The above outlines the workarounds for the others.

2) cxf的wiki中谈到Client API中的Proxy-based API

wiki 地址: http://cwiki.apache.org/CXF20DOC/jax-rs.html

Limitations

Proxy methods can not have @Context method parameters and subresource methods returning Objects can not be invoked - perhaps it is actually not too bad at all - please inject contexts as field or bean properties and have subresource methods returning typed classes : interfaces, abstract classes or concrete implementations.

Proxies are currently not thread-safe.

3) thread safe issue caused by XMLOutputFactoryImpl

找到的一个cxf的bug,https://issues.apache.org/jira/browse/CXF-2229

Description   

Currently CXF calls StaxUtils.getXMLOutputFactory() to get the cached instance of XMLOutputFactoryImpl. But XMLOutputFactoryImpl.createXMLStreamWriter is not thread-safe. See below.

javax.xml.stream.XMLStreamWriter createXMLStreamWriter(javax.xml.transform.stream.StreamResult sr, String encoding) throws javax.xml.stream.XMLStreamException {
         try{
           if(fReuseInstance && fStreamWriter != null && fStreamWriter.canReuse() && !fPropertyChanged){
               fStreamWriter.reset();
               fStreamWriter.setOutput(sr, encoding);
               if(DEBUG)System.out.println("reusing instance, object id : " + fStreamWriter);
               return fStreamWriter;
           }
           return fStreamWriter = new XMLStreamWriterImpl(sr, encoding, new PropertyManager(fPropertyManager)); -- this is not thread safe, since the new instance is assigned to the field fStreamWriter first, then it is possible that different threads get the same XMLStreamWriterImpl when they call this method at the same time.
         }catch(java.io.IOException io){
           throw new XMLStreamException(io);
       }
   }

The solution might be, StaxUtils.getXMLOutputFactory() method creates a new instance of XMLOutputFactory every time, don't cache it.

上一页  1 2 3 4 5 

Tags:CXF client 并发

编辑录入:爽爽 [复制链接] [打 印]
赞助商链接