我有一个执行 MTOM 流式处理的 JAX-WS RI 服务器。从客户端来看,一切都很好。服务器收到一个 StreamingDataHandler,并且双方都没有使用太多内存发送 1 GB。然而,在检索文件时,服务器在发送文件之前读取了全部内容。客户端没问题。我在两端都使用 DataHandler,所以这方面的东西很好。我只是不希望服务器在开始发送之前从 InputStream 读取所有数据。我在 impl 上有以下注释:
@MTOM
@StreamingAttachment(parseEagerly = false, memoryThreshold = 1L)
@WebService(...)
我试过 parseEagerly
true
和 false
。我将内存阈值设置得很低,因为我不担心小文件的小性能影响。
这是 JAX-WS RI 的错误吗?
最佳答案
我就是这样做的。工作起来很有魅力。
界面(觉得很像你做的)
@WebService
@SOAPBinding(style = Style.RPC)
public interface ResultsServer {
@WebMethod
@XmlMimeType("application/octet-stream")
public DataHandler getResultDataAsXML(@WebParam Integer raceId) throws Exception;
}
实现
@MTOM
@WebService(endpointInterface = "ca.sportstats.wtc.ResultsServer")
@StreamingAttachment(parseEagerly=true, memoryThreshold=4000000L)
public class ResultsServerImpl implements ResultsServer {
@Override
public DataHandler getResultDataAsXML(@WebParam Integer raceId) throws Exception {
File xmlFile = new File("c:/tmp/xml/response.xml");
//... write my xml file using an XMLStreamWriter
return new DataHandler(new FileDataSource(xmlFile));
}
}
测试
public class Test {
public static void main(String[] args) throws Exception {
URL url = new URL("http://url/to/ipmlementation/ResultsServerImplService?wsdl");
QName qname = new QName("http://test.test.com/", "ResultsServer");
Service service = Service.create(url, qname);
ResultsServerImpl resultServer = service.getPort(ResultsServerImpl.class);
/************ test download ***************/
DataHandler handler = resultServer.getResultDataAsXML(4);
StreamingDataHandler dh = (StreamingDataHandler) handler;
File file = File.createTempFile("test-received.xml", "");
dh.moveTo(file);
dh.close();
}
}
这对我来说很有效(从服务器)流式传输一个大的 XML 文件,而不用在内存中读取它。
另见
关于java - JAX-WS MTOM 流式传输 : server reads entire stream before sending,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15796516/