users@jax-ws.java.net

Streaming Data with JAX-WS

From: Fisher, Brice A <bafishe_at_sandia.gov>
Date: Thu, 12 Apr 2007 11:15:45 -0600

  One of our key reasons for choosing to go with JAX-WS was the ability
to stream data to and from a web service (in particular uploading and
downloading very large files). I've been trying to get this
functionality working for several weeks now and have had little luck.
There seems to be lots of people asking about this, but their questions
are only getting incompletely answered. I found a blog entry here
http://blogs.sun.com/sdimilla/entry/sending_and_receiving_attachments_us
ing that shows exactly what I want to accomplish in the mtomInOut()
function (i.e. passing a DataHandler in a Holder which allows streaming
on the Server), but it was developed in JAX-WS 2.0 and I'm currently
using 2.1.

  Jitu mentions that you can use DataHandler for streaming here
https://jax-ws.dev.java.net/servlets/ReadMsg?listName=users&msgNo=721,
but doesn't provide any examples or references.

  The MTOM example in the samples directory includes tantalizing
functions implying streaming, but those functions are not used by any of
the executed code in that sample.

  Currently, trying to replicate the functionality in Stephen DiMilla's
blog, I get the following exception, using either JAX-WS 2.1ri or JAX-WS
2.1 nightly 2007-04-12, when the DataHandler argument is passed to the
stub function generated by wsimport:

Exception: java.lang.ClassCastException: javax.activation.DataHandler
cannot be cast to [B

  That is not a typo, the actual exception ends with "cast to [B".

  Could someone please respond and:

  1) Explain if this functionality is working or is planned to be
working in the near future

  2) Point to or provide specific examples that implement streaming data
to and/or from a Web Service

  3) If it's not working now, provide an example of how to implement our
code currently so that when the functionality does become available we
can take immediate advantage of it (rather than having to reimplement
the functions). For example, if DataHandlers are the correct way to go,
is there a way to implement them now, even if they aren't actually
streamed yet, so that when streaming is working they will start
streaming?

  Thanks,
  - Brice

Here's the test (server) function I'm trying to get working:

@MTOM
@WebService()
public class GSWebService2 {
  @WebMethod()
  public String putStreamingFileMTOM(Holder<DataHandler> holder, String
filename) {
    String len = null;
    FileOutputStream os = null;
    InputStream is = null;
    byte[] buf = new byte[8192];
    int cnt = 0;
    
    try {
      os = new FileOutputStream("C:/"+filename);
      is = holder.value.getInputStream();
      int b = is.read(buf);
      while (b > -1) {
        cnt += b;
        os.write(buf,0,b);
        b = is.read(buf);
      }
      len = "success - "+cnt;
    } catch (Exception ex) {
      System.out.println("GSWebService2 - putStreamingFile - Exception:
"+ex.toString());
    } finally {
      try {
        if (null != is) is.close();
        if (null != os) os.close();
      } catch (Exception ex) { }
    }
    
    return len;
  }
}

Here's how I'm trying to call it in the client:

  String res = proxy.putStreamingFileMTOM(new Holder(new DataHandler(new
FileDataSource("tests/testfile.dat"))),"testfile.dat");


Supporting links:
Large file upload bug:
https://jax-ws.dev.java.net/issues/show_bug.cgi?id=29
Thread talking about Streaming:
http://forums.java.net/jive/thread.jspa?messageID=209777&tstart=0
Jitu mentions that you can use DataHandler for streaming:
https://jax-ws.dev.java.net/servlets/ReadMsg?listName=users&msgNo=721
Stephen DiMilla's Blog from 7/17/2006: Sending and Receiving attachments
using the SOAP MessageTransmission Optimization Mechanism (MTOM) in
JAX-WS 2.0
http://blogs.sun.com/sdimilla/entry/sending_and_receiving_attachments_us
ing