java - 使用 java jersey 客户端异常丢弃 HTTP 流
问题描述
对于我们的一个项目,我们正在使用 java jersey 客户端使用 HTTP 提要流
客户端Feed消费正常,但10分钟后,流异常下降;即使流生产者已启动并运行并产生流
这就是我尝试过的;
import java.util.Date;
import javax.ws.rs.client.Client;
import javax.ws.rs.client.ClientBuilder;
import javax.ws.rs.client.WebTarget;
import javax.ws.rs.client.Invocation.Builder;
import javax.ws.rs.core.GenericType;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;
import org.glassfish.jersey.client.ChunkedInput;
public class StreamClient {
private static final String BOUNDARY = "\n";
public void makeCall() {
System.out.println("Start Time"+ new Date()) ;
Client client = ClientBuilder.newClient();
BasicAuth auth = new BasicAuth();
auth.setPassword("username");
auth.setUserName("password");
BasicAuthentication basicAuthentication = new BasicAuthentication(auth);
client.register(basicAuthentication);
WebTarget target = client.target("https://localhost:7211/stream/v1/");
Builder request = target.request(MediaType.APPLICATION_JSON);
Response response = request.get();
final ChunkedInput<String> chunkedInput = response.readEntity(new GenericType<ChunkedInput<String>>() {
});
chunkedInput.setParser(ChunkedInput.createParser(BOUNDARY));
String chunk;
do {
if((chunk = chunkedInput.read()) != null)
System.out.println(chunk);
}while (!chunkedInput.isClosed());
System.out.println("End Time " + new Date());
}
public static void main(String[] args) {
StreamClient client = new StreamClient();
client.makeCall();
}
}
如果流没有下降;线程必须在 while 循环内;一旦流关闭,线程就会出来并打印 sysout 语句
这里的问题是;为什么 chunkedinput 被关闭。
在这里,对于服务器端的每个有效负载/块分离,它都是 \r\n 并使其与服务器的连接处于活动状态,发送 \n (在客户端,我必须忽略这一点。)
解决方案
我建议使用wireshark 在连接丢失时准确分析流量,可能是防火墙正在切断连接。
在任何情况下,尝试在没有双面记录的情况下调试这些场景都是 50% 的俄罗斯规则情况。
如果要丢弃场景尝试改变正在传输的数据量,连接的参数(使用其他构造函数方法)并查看超时是否及时一致,如果超时取决于通常的流量您在防火墙等中间设备中触发某些阈值的线索。
推荐阅读
- javascript - 如何使用js自动控制浏览器
- cmake - 如何在 CMake 中显示 Foo::Bar 的内容?
- asp.net-mvc - 如何正确构建一组 mvc 视图以最小化许多模型的视图数量?
- python-3.x - 在 Celery 和 Django 中没有这样的带有 RabbitMQ 的传输 RPC
- reactjs - 我如何通过身份验证对路由做出明智的反应?
- vba - 在页脚中插入图像的 .doc 和 .docx 文档之间的代码差异
- c# - 有没有办法在 xaml 用户控件的视图模型中引发事件,用户控件的父视图模型可以使用该事件
- excel - 根据变量有条件地在轴上显示刻度标签
- python - 如何在数据框中查找日期时间值的上一行和下一行?
- python - 尽管目录中存在该文件,为什么python不会加载该文件?