gpt4 book ai didi

java - jersey - StreamingOutput 作为响应实体

转载 作者:IT老高 更新时间:2023-10-28 20:45:14 32 4
gpt4 key购买 nike

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

@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
}
}

问题是即使在调用 FeedReturnStreamingOutput 之前从资源发回响应,泽西客户端仍会等待直到 FeedReturnStreamingOutput 执行完成。

客户代码:

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");

最佳答案

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

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.

这是一个例子

@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); }
}
}
}

这是没有设置属性的结果

enter image description here

这是将属性值设置为0

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

enter image description here

关于java - jersey - StreamingOutput 作为响应实体,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29637151/

32 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com