gpt4 book ai didi

java - Hystrix:Apache http客户端请求不中断

转载 作者:搜寻专家 更新时间:2023-11-01 03:18:46 25 4
gpt4 key购买 nike

我有一个 apache HTTP 客户端定义如下:

private static HttpClient httpClient = null;
HttpParams httpParams = new BasicHttpParams();
httpParams.setParameter(CoreProtocolPNames.USE_EXPECT_CONTINUE, Boolean.TRUE);
httpParams.setParameter(CoreProtocolPNames.USER_AGENT, "ABC");

HttpConnectionParams.setStaleCheckingEnabled(httpParams, Boolean.TRUE);

SSLSocketFactory sf = SSLSocketFactory.getSocketFactory();

SchemeRegistry schemeRegistry = new SchemeRegistry();
schemeRegistry.register(new Scheme("http", 80, PlainSocketFactory.getSocketFactory()));
schemeRegistry.register(new Scheme("https", 443, sf));

//Initialize the http connection pooling
PoolingClientConnectionManager connectionManager = new PoolingClientConnectionManager(schemeRegistry);

// Initialize the connection parameters for performance tuning
connectionManager.setMaxTotal(12);
connectionManager.setDefaultMaxPerRoute(10);

httpClient = new DefaultHttpClient(connectionManager, httpParams);

我有一个 hystrix 命令 play 并启用了以下属性:

hystrix.command.play.execution.isolation.thread.timeoutInMilliseconds=1
hystrix.command.play.execution.isolation.thread.interruptOnTimeout=true

命令本身定义如下:

    @HystrixCommand(groupKey="play_group",commandKey="play")
public String process(String request) throws UnsupportedOperationException, IOException, InterruptedException {
System.out.println("Before - process method : " + request);
callHttpClient(request);
System.out.println("After - process method" + request);
return "";
}

private void callHttpClient(String request) throws ClientProtocolException, IOException, InterruptedException {
HttpGet get = new HttpGet("http://www.google.co.in");
HttpResponse response = httpClient.execute(get);
System.out.println("Response:" + response);
}

我现在尝试循环执行命令 5 次:

    public static void main(String[] args) throws UnsupportedOperationException, IOException, InterruptedException {
ApplicationContext ctx = new ClassPathXmlApplicationContext("applicationContextTest.xml");
HystrixPlayground obj = ctx.getBean(HystrixPlayground.class);

long t1 = System.currentTimeMillis();
for (int i = 0; i < 5; i++) {
try{
System.out.println(obj.process("test" + i));
} catch(Exception ex) {
System.out.println(ex);
}
long t2 = System.currentTimeMillis();
System.out.println("Time(ms) : ---->" + (t2 - t1));

超时设置为 1 毫秒,因此 process 方法抛出 HystrixRunTimeException。但是http请求继续执行并打印“After-process method”字符串。

我只在 http 客户端请求中看到过这种行为。如果 http 请求被其他任何东西(如线程 sleep 或非常大的 for 循环)替换,hystrix 线程将按预期中断。

有没有人知道为什么会发生这种情况?

最佳答案

原因是在 Java 中中断一个线程并不会“强行停止”它。相反,调用 Thread.interrupt() 只是设置一个标志,该标志可以但不必由正在运行的线程解释。在这里查看更多信息:What does java.lang.Thread.interrupt() do?

Apache HTTP 客户端不解释这个标志(因为 JRE 的套接字读取操作不能被中断)。因此,HTTP 请求不会被取消而只是完成。

关于java - Hystrix:Apache http客户端请求不中断,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38077687/

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