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

时间:2016-06-28 13:32:39

标签: java timeout hystrix

我有一个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毫秒,因此进程方法抛出HystrixRunTimeException。但是,http请求继续执行并打印“After-process method”字符串。

我仅针对http客户端请求一直看到此行为。如果http请求被其他任何东西替换,比如线程休眠或非常大的for循环,则hystrix线程会按预期中断。

有没有人知道为什么会这样?

2 个答案:

答案 0 :(得分:2)

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

Apache HTTP客户端不解释此标志。因此,HTTP请求不会被取消,只是完成。

答案 1 :(得分:0)

似乎在过去的某些IO操作中,可以使用Thread.interrupt()中断某些平台(solaris)。现在,似乎不再可能/对于java.io已指定,但对于java.nio仍然可能。 (有关更多详细信息,请参见Java bug 4385444Java bug 7188233

因此,即使with some platforms/versions it seems still possibleThread.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();
    }
}