Guest User

StreamingOutput fails to access Ivy

a guest
Oct 3rd, 2016
170
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.31 KB | None | 0 0
  1. package com.axonivy.lab.rest;
  2.  
  3. import java.io.BufferedWriter;
  4. import java.io.IOException;
  5. import java.io.OutputStream;
  6. import java.io.OutputStreamWriter;
  7. import java.io.PrintWriter;
  8. import java.util.stream.IntStream;
  9.  
  10. import javax.ws.rs.GET;
  11. import javax.ws.rs.Path;
  12. import javax.ws.rs.WebApplicationException;
  13. import javax.ws.rs.core.Response;
  14. import javax.ws.rs.core.StreamingOutput;
  15.  
  16. import ch.ivyteam.ivy.environment.Ivy;
  17. import ch.ivyteam.log.Logger;
  18.  
  19. @Path("{applicationName}/streams")
  20. public class StreamingResponseRestSResource {
  21.  
  22.     @GET
  23.     public Response getStreamingResponse() {
  24.        
  25.         // This works by keeping a reference then pass it to the anonymous class.
  26.         // It may NOT work in all situations.
  27.         final Logger logger = Ivy.log();
  28.        
  29.        
  30.         StreamingOutput streamingOutput = new StreamingOutput() {
  31.  
  32.             @Override
  33.             public void write(OutputStream output) throws IOException,
  34.                     WebApplicationException {
  35.  
  36.                 PrintWriter out = new PrintWriter(new BufferedWriter(
  37.                         new OutputStreamWriter(output)), true);
  38.  
  39.                 Ivy.log().error("HHH - BREAK HERE");
  40.                
  41.                 logger.error("HHH - It works with passed reference of Logger");
  42.  
  43.                 IntStream.range(0, 100_000)
  44.                     .forEach(c -> out.println("HHH - Count up to " + c));
  45.                
  46.             }
  47.         };
  48.  
  49.         return Response.ok(streamingOutput).build();
  50.     }
  51. }
Add Comment
Please, Sign In to add comment