jersey - StreamingOutput作为响应实体 [英] jersey - StreamingOutput as Response entity

查看:1087
本文介绍了jersey - StreamingOutput作为响应实体的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在Jersey Resource类中实现了流输出。

  @GET 
@Path(xxxxx )
@Produces(BulkConstants.TEXT_XML_MEDIA_TYPE})
public Response getFile(){

FeedReturnStreamingOutput sout = new FeedReturnStreamingOutput();
response = Response.ok(sout).build();
返回回复;
}

类FeedReturnStreamingOutput实现StreamingOutput {

public FeedReturnStreamingOutput()

@Override
public void write(OutputStream outputStream ){
//写入输出流
}
}

问题是尽管在调用FeedReturnStreamingOutput之前从资源发回响应但是Jersey客户端等待FeedReturnStreamingOutput执行完成。



客户端代码:

 客户端客户端= Client.create(); 

ClientResponse response = webResource
// headers
.get(ClientResponse.class);

//执行FeedReturnStreamingOutput后执行下面的代码,这破坏了流式传输的必要性

OutputStream os = new FileOutputStream(c:\\test\\ feedoutput5.txt);
System.out.println(new Date()+:Reached point A);

if(response.getStatus()== 200){
System.out.println(new Date()+:Reached point B);
InputStream io = response.getEntityInputStream();

byte [] buff = new byte [1024000];
int count = 0;

while((count = io.read(buff,0,buff.length))!= -1){
os.write(buff,0,count);
}

os.close();
io.close();

} else {
System.out.println(响应代码:+ response.getStatus());
}

System.out.println(Time time - >>+(System.currentTimeMillis() - startTime)+ms);


解决方案

问题是缓冲 Jersey用于缓冲实体以确定Content-Length标头的OutputStream 。缓冲区的大小默认为8 kb。如果需要,可以禁用缓冲,或者仅使用属性更改缓冲区的大小




这是设置完成后的结果属性值为 0

 公共类AppConfig扩展ResourceConfig {
public AppConfig(){
...
property(ServerProperties.OUTBOUND_CONTENT_LENGTH_BUFFER,0);
}
}


I had implemented streaming output in my Jersey Resource class.

@GET
@Path("xxxxx")
@Produces(BulkConstants.TEXT_XML_MEDIA_TYPE})   
public Response getFile() {

    FeedReturnStreamingOutput sout = new FeedReturnStreamingOutput();
    response = Response.ok(sout).build();
    return response;
}

class FeedReturnStreamingOutput implements StreamingOutput {

    public FeedReturnStreamingOutput()

    @Override
    public void write(OutputStream outputStream)  {
        //write into Output Stream
    }
}

The problem is eventhough a response is sent back from the resource before FeedReturnStreamingOutput is called Jersey client waits until FeedReturnStreamingOutput execution is completed.

Client Code :

Client client = Client.create();

ClientResponse response = webResource
    //headers
    .get(ClientResponse.class);

//The codes underneath executes after FeedReturnStreamingOutput is executed which undermines the necessity of streaming

OutputStream os = new FileOutputStream("c:\\test\\feedoutput5.txt");
System.out.println(new Date() + " : Reached point A");

if (response.getStatus() == 200) {
    System.out.println(new Date() + " : Reached point B");
    InputStream io = response.getEntityInputStream();

    byte[] buff = new byte[1024000];
    int count = 0;

    while ((count = io.read(buff, 0, buff.length)) != -1) {
        os.write(buff, 0, count);
    }

    os.close();
    io.close();

} else {
    System.out.println("Response code :" + response.getStatus());
}

System.out.println("Time taken -->> "+(System.currentTimeMillis()-startTime)+" ms");

解决方案

The problem is the buffering OutputStream that Jersey uses to buffer the entity in order to determine the Content-Length header. The size of the buffer default to 8 kb. You disable the buffering if you want, or just change the size of the buffer, with the property

ServerProperties.OUTBOUND_CONTENT_LENGTH_BUFFER

An integer value that defines the buffer size used to buffer server-side response entity in order to determine its size and set the value of HTTP "Content-Length" header.

If the entity size exceeds the configured buffer size, the buffering would be cancelled and the entity size would not be determined. Value less or equal to zero disable the buffering of the entity at all.

This property can be used on the server side to override the outbound message buffer size value - default or the global custom value set using the "jersey.config.contentLength.buffer" global property.

The default value is 8192.

Here's an example

@Path("streaming")
public class StreamingResource {

    @GET
    @Produces("application/octet-stream")
    public Response getStream() {
        return Response.ok(new FeedReturnStreamingOutput()).build();
    }

    public static class FeedReturnStreamingOutput implements StreamingOutput {

        @Override
        public void write(OutputStream output)
                throws IOException, WebApplicationException {
            try {
                for (int i = 0; i < 10; i++) {
                    output.write(String.format("Hello %d\n", i).getBytes());
                    output.flush();
                    TimeUnit.MILLISECONDS.sleep(500);
                }
            } catch (InterruptedException e) {  throw new RuntimeException(e); }
        }
    }
}

Here's the result without setting the property

And here's the result after setting the property value to 0

public class AppConfig extends ResourceConfig {
    public AppConfig() {
        ...
        property(ServerProperties.OUTBOUND_CONTENT_LENGTH_BUFFER, 0);
    }
}

这篇关于jersey - StreamingOutput作为响应实体的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆