[英]Signing AWS HTTP requests with Apache HttpComponents Client
[英]Hystrix: Apache http client requests are not interrupted
我有一个 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 请求被线程睡眠或非常大的 for 循环之类的其他内容替换,则 hystrix 线程会按预期中断。
有没有人知道为什么会发生这种情况?
原因是在 Java 中中断线程不会“强制停止”它。 相反,调用Thread.interrupt()
只是设置一个标志,该标志可以但不必由正在运行的线程解释。 在此处查看更多信息: java.lang.Thread.interrupt() 是做什么的?
Apache HTTP 客户端不解释此标志(因为 JRE 的套接字读取操作不能被中断)。 因此,HTTP 请求不会被取消而只是完成。
似乎过去一些 IO 操作,在某些平台(solaris)上可以使用Thread.interrupt()
中断。 现在,对于 java.io 似乎不再可能/不再指定,但对于 java.nio 仍然可能。 (有关更多详细信息,请参阅Java 错误 4385444和Java 错误 7188233 )
因此,即使对于某些平台/版本,似乎仍然可以使用Thread.interrupt()
中断阻塞 IO,这似乎显然不推荐:
所以httpclient没有任何问题。 使用 Hystrix,您应该在 Hystrix 超时时abort()
请求。 如果您正在使用回退,您可以在getFallback()
调用它,否则您可以在获得结果后调用它。 例如:
try {
hystrixCommand.execute();
} catch (HystrixRuntimeException e) {
switch (e.getFailureType()) {
case TIMEOUT:
// supposing you add a getHttpRequest getter on your command.
hystrixCommand.getHttpRequest().abort();
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.