开发学院软件开发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 并发

编辑录入:爽爽 [复制链接] [打 印]
[]
  • 好
  • 好的评价 如果觉得好,就请您
      0%(0)
  • 差
  • 差的评价 如果觉得差,就请您
      0%(0)
赞助商链接