Uploaded image for project: 'RESTEasy'
  1. RESTEasy
  2. RESTEASY-1081

When using StreamingOutput, the Resource freezes until it's done and no other users can call it

XMLWordPrintable

      There's a problem managing StreamingOutput, it seems it's not thread safe and cannot be paralleled, if I call the StreamingOutput that streams a big file for example, until the service is done streaming the file, no other user can call the Resource serving the files.

      Is there a way around this? that way many users can download big files.

      I use StreamingOutput to output the file because if not, then the file gets all loaded into RAM effectively killing the application, so streaming files avoids loading the whole file into memory.

      Here's a code sample of the Resource method I use for streaming the file output, I'm using my OpenSuSE ISO file which is 4.7GB so I can test big file buffering:

          @GET
          @Produces(MediaType.APPLICATION_OCTET_STREAM)
          @Path("getFile")
          public Response getFile() throws FileNotFoundException {
              final FileInputStream file = new FileInputStream("/home/documents/openSUSE-13.1-DVD-x86_64.iso");
              StreamingOutput stream = new StreamingOutput() {
                  @Override
                  public void write(OutputStream out) throws IOException, WebApplicationException {
                      //buffer size set to 10MB
                      int bufferSize = 1024 * 1024 * 10;
                      int buffer = 1;
                      int rs = file.read();
                      while (rs != -1)
                      {
                          out.write(rs);
                          rs = file.read();
                          buffer++;
                          //flush the output stream every 10MB
                          if (buffer == bufferSize)
                          {
                              buffer = 1;
                              out.flush();
                          }
                      }
                      out.flush();
                  }
              };
              return Response.ok(stream).header("Content-Disposition", "attachment; filename=\"openSUSE-13.1-DVD-x86_64.iso\"").build();
          }
      

      I tested this Resource using JBoss WildFly 8.1.0, but if using resteasy-netty4 for serving this resource, the problem is even bigger, all file downloads get frozen and then suddenly the output is flushed to RAM and the java process grows out filling the heap killing the application.

      Mind me saying that even though I tested WildFly, that was just to make sure it wasn't a problem with the netty4 adapter, but I'm using resteasy-netty4, so if someone knows a workaround using netty4 then great! I've seen things in the resteasy-netty4 source like ChunkedOutputStream but I don't even know where to start figuring out a workaround to this issue.

        1. ex03_1.war
          1.95 MB
        2. ex03_1-sources.jar
          4 kB
        3. resteasy-jaxrs-3.0.8.Final.jar
          803 kB
        4. ScreenSnapz730.png
          ScreenSnapz730.png
          44 kB

            patriot1burke@gmail.com Bill Burke (Inactive)
            jimi.com.mx Raul Guerrero Deschamps (Inactive)
            Votes:
            0 Vote for this issue
            Watchers:
            4 Start watching this issue

              Created:
              Updated:
              Resolved: