Search in sources :

Example 1 with StreamingResponse

use of org.apache.cxf.jaxrs.ext.StreamingResponse in project cxf by apache.

the class BookStoreWebSocket method getBookStream.

@GET
@Path("/bookstream")
@Produces("application/json")
public StreamingResponse<Book> getBookStream() {
    return new StreamingResponse<Book>() {

        public void writeTo(final StreamingResponse.Writer<Book> out) throws IOException {
            out.write(new Book("WebSocket1", 1));
            executor.execute(new Runnable() {

                public void run() {
                    try {
                        for (int i = 2; i <= 5; i++) {
                            Thread.sleep(500);
                            out.write(new Book("WebSocket" + i, i));
                            out.getEntityStream().flush();
                        }
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                }
            });
        }
    };
}
Also used : StreamingResponse(org.apache.cxf.jaxrs.ext.StreamingResponse) Book(org.apache.cxf.systest.jaxrs.Book) IOException(java.io.IOException) WebApplicationException(javax.ws.rs.WebApplicationException) Path(javax.ws.rs.Path) Produces(javax.ws.rs.Produces) GET(javax.ws.rs.GET)

Aggregations

IOException (java.io.IOException)1 GET (javax.ws.rs.GET)1 Path (javax.ws.rs.Path)1 Produces (javax.ws.rs.Produces)1 WebApplicationException (javax.ws.rs.WebApplicationException)1 StreamingResponse (org.apache.cxf.jaxrs.ext.StreamingResponse)1 Book (org.apache.cxf.systest.jaxrs.Book)1