JAX-RS ChunkedOutput and ChunkedInput with Wildfly

please help make this simple example to deploy on Wildfly (preferred version 10.1.0). Sample code:

import org.glassfish.jersey.server.ChunkedOutput;
import javax.ws.rs.*;
import java.io.*;

@Path("/numbers")
public class NumbersResource {

    @GET
    public ChunkedOutput<String> streamExample(){
        final ChunkedOutput<String> output = new ChunkedOutput<String>(String.class);

        new Thread() {
            @Override
            public void run() {
                try {
                    for (int i = 0; i < 100000 ; i++){
                        output.write(i + " ");
                    }
                } catch (IOException e){
                    e.printStackTrace();
                } finally {
                    try {
                        output.close();
                    } catch (IOException e){
                        e.printStackTrace();
                    }
                }
            }
        }.start();
        return output;
    }

}

(the snippet of code belongs to the author MEMORYNOTFOUND. I had added it here just in case the side is shut down for any reason) I had made it deploy on GlassFish and everything is ok. But now, I need this functionality to be ported on Wildfly. And from the import

import org.glassfish.jersey.server.ChunkedOutput;

It seams that class ChunkedOutput belongs to GlassFish us functionality. In other words, is there something similar us functionality with the import from Wildfly jars or I don't know...?

P.S. Please provide a simple example, among the response. Thanks in advance!

Upvotes: 1

Views: 460

Answers (1)

siom
siom

Reputation: 1807

Use StreamingOutput instead:

@GET
@Produces(MediaType.TEXT_PLAIN)
@Path("/<your-path>")
public Response hello() {
    StreamingOutput stream = new StreamingOutput() {
        @Override
        public void write(OutputStream os) throws IOException, WebApplicationException {
            Writer writer = new BufferedWriter(new OutputStreamWriter(os));

            for (...) {
                writer.write(...);
            }
            writer.flush();
        }
    };
    return Response.ok(stream).build();
}

Upvotes: 4

Related Questions