【发布时间】:2016-06-28 13:32:38
【问题描述】:
我有一个 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请求继续执行并打印“后处理方法”字符串。
我已经看到这种行为仅适用于 http 客户端请求。如果 http 请求被其他任何东西(例如线程睡眠或非常大的 for 循环)替换,则 hystrix 线程会按预期中断。
有没有人知道为什么会发生这种情况?
【问题讨论】: