从JAX-WS服务返回大文件

时间:2013-03-11 14:50:27

标签: java web-services jax-ws mtom

我正在尝试从JAX-WS服务方法返回大文件,但是出现了奇怪的客户端错误。这是我的代码:

@WebService(targetNamespace = "http://java.CAEServer", portName = "CAEInstance")
public interface Instance  {
    @WebMethod(action = "http://java.CAEServer/getResultsArch")
    DataHandler getResultsArch(org.caebeans.caeserver.Instance instance);
}

实现:

MTOM(enabled = true, threshold = 2048)
@BindingType(SOAPBinding.SOAP11HTTP_MTOM_BINDING)
@StreamingAttachment(parseEagerly=true, memoryThreshold=4000L)
@WebService(endpointInterface = "org.caebeans.wsrf.Instance")
public class InstanceImpl implements Instance {
    @Override
    public DataHandler getResultsArch(org.caebeans.caeserver.Instance instance) {
        try {
            return workStorageManager.getWorkPackage(instance.getId());
        } catch (Exception e) {
            logger.fatal("Failed to zip work", e);
            throw new RuntimeException("Failed to zip results");
        }
    }
}

getWorkArch返回带有压缩数据的DataHandler。这是客户端代码:

Instance instanceTransport = new InstanceImplService().getInstanceImplPort();
SOAPBinding binding = (SOAPBinding) ((BindingProvider) instanceTransport).getBinding();
binding.setMTOMEnabled(true);

byte[] resultArch = instanceTransport.getResultsArch(instance);

当我试图运行它时,我遇到了错误。这是服务器堆栈跟踪:

java.io.IOException: Broken pipe
at sun.nio.ch.FileDispatcher.write0(Native Method)
at sun.nio.ch.SocketDispatcher.write(SocketDispatcher.java:29)
at sun.nio.ch.IOUtil.writeFromNativeBuffer(IOUtil.java:69)
at sun.nio.ch.IOUtil.write(IOUtil.java:40)
at sun.nio.ch.SocketChannelImpl.write(SocketChannelImpl.java:336)
at sun.net.httpserver.Request$WriteStream.write(Request.java:397)
at sun.net.httpserver.ChunkedOutputStream.writeChunk(ChunkedOutputStream.java:108)
at sun.net.httpserver.ChunkedOutputStream.write(ChunkedOutputStream.java:77)
at sun.net.httpserver.PlaceholderOutputStream.write(ExchangeImpl.java:390)
at com.sun.xml.internal.ws.transport.http.server.ServerConnectionImpl$2.write(ServerConnectionImpl.java:163)
at javax.activation.DataHandler.writeTo(DataHandler.java:294)
at com.sun.xml.internal.ws.encoding.MtomCodec$ByteArrayBuffer.write(MtomCodec.java:189)
at com.sun.xml.internal.ws.encoding.MtomCodec.encode(MtomCodec.java:156)
at com.sun.xml.internal.ws.encoding.SOAPBindingCodec.encode(SOAPBindingCodec.java:249)
at com.sun.xml.internal.ws.transport.http.HttpAdapter.encodePacket(HttpAdapter.java:328)
at com.sun.xml.internal.ws.transport.http.HttpAdapter.access$100(HttpAdapter.java:82)
at com.sun.xml.internal.ws.transport.http.HttpAdapter$HttpToolkit.handle(HttpAdapter.java:470)
at com.sun.xml.internal.ws.transport.http.HttpAdapter.handle(HttpAdapter.java:233)
at com.sun.xml.internal.ws.transport.http.server.WSHttpHandler.handleExchange(WSHttpHandler.java:95)
at com.sun.xml.internal.ws.transport.http.server.WSHttpHandler.handle(WSHttpHandler.java:80)
at com.sun.net.httpserver.Filter$Chain.doFilter(Filter.java:65)
at sun.net.httpserver.AuthFilter.doFilter(AuthFilter.java:65)
at com.sun.net.httpserver.Filter$Chain.doFilter(Filter.java:68)
at sun.net.httpserver.ServerImpl$Exchange$LinkHandler.handle(ServerImpl.java:557)
at com.sun.net.httpserver.Filter$Chain.doFilter(Filter.java:65)
at sun.net.httpserver.ServerImpl$Exchange.run(ServerImpl.java:529)
at java.util.concurrent.ThreadPoolExecutor$Worker.runTask(ThreadPoolExecutor.java:886)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:908)
at java.lang.Thread.run(Thread.java:680)

这是客户端堆栈跟踪:

xception in thread "main" java.lang.OutOfMemoryError: Java heap space
at com.sun.xml.internal.org.jvnet.staxex.ByteArrayOutputStreamEx.readFrom(ByteArrayOutputStreamEx.java:60)
at com.sun.xml.internal.org.jvnet.staxex.Base64Data.get(Base64Data.java:225)
at com.sun.xml.internal.org.jvnet.staxex.Base64Data.length(Base64Data.java:266)
at com.sun.xml.internal.ws.encoding.MtomCodec$MtomXMLStreamReaderEx.getTextCharacters(MtomCodec.java:508)
at com.sun.xml.internal.bind.v2.runtime.unmarshaller.StAXStreamConnector.handleCharacters(StAXStreamConnector.java:312)
at com.sun.xml.internal.bind.v2.runtime.unmarshaller.StAXStreamConnector.bridge(StAXStreamConnector.java:176)
at com.sun.xml.internal.bind.v2.runtime.unmarshaller.UnmarshallerImpl.unmarshal0(UnmarshallerImpl.java:351)
at com.sun.xml.internal.bind.v2.runtime.BridgeImpl.unmarshal(BridgeImpl.java:109)
at com.sun.xml.internal.bind.api.Bridge.unmarshal(Bridge.java:222)
at com.sun.xml.internal.ws.client.sei.ResponseBuilder$DocLit.readResponse(ResponseBuilder.java:514)
at com.sun.xml.internal.ws.client.sei.SyncMethodHandler.invoke(SyncMethodHandler.java:110)
at com.sun.xml.internal.ws.client.sei.SyncMethodHandler.invoke(SyncMethodHandler.java:78)
at com.sun.xml.internal.ws.client.sei.SEIStub.invoke(SEIStub.java:107)
at $Proxy31.getResultsArch(Unknown Source)
at Main.main(Main.java:73)

怎么了?

编辑:这是我的Main类中的托管代码:

Endpoint instanceEndpoint = Endpoint.publish(serverHost + INSTANCE_PATH, instance);

使用默认的MacOS java 6设置运行客户端。我正在尝试下载400Mb文件,如果没有启用TOM,服务器就会出现OutOfMemory错误。

1 个答案:

答案 0 :(得分:3)

以下是一些建议和意见:

    当服务器响应客户端但客户端连接中途终止时,会引发
  1. Broken pipe异常。
  2. 这很明显,因为您的客户端因内存不足而崩溃,并且无法处理从给定堆大小的服务器接收的数据量(我看到您试图将数据保存在字节数组中)。 )。
  3. 一个最快解决方案是将客户端VM的堆大小增加到适当的值。
  4. 其他更实用的选项(如果您的用例受理此来流式传输持久位置(例如文件)中的数据。这样整个数据就不会被加载到内存中
  5. 编辑:

    如果您查看文档。有一个StreamingDataHandler类可用于流式传输数据。这this particular link。有一个名为 Streaming SOAP Attachments 的子主题。它提供了大文件下载的示例,非常接近您的用例。    我认为您只需要更改客户端代码并使用StreamingDataHandler而不是DataHandler