NoHttpResponseException作为运行时异常抛出,即使是已检查的异常

时间:2017-10-19 10:41:34

标签: java exception apache-httpclient-4.x amazon-sqs

我遇到一个奇怪的问题,即org.apache.http.NoHttpResponseException被抛出为未经检查的异常,即使它是一个已检查的异常,因为它扩展了java.io.IOException ...从下面的贴出的堆栈跟踪可以看出我我得到的异常应该在编译时检查为未经检查的运行时异常。

我得到的异常的堆栈跟踪如下(我的类在包中:com.example.staticsite):

org.apache.http.NoHttpResponseException: sqs.eu-west-1.amazonaws.com failed to respond
    at org.apache.http.impl.conn.DefaultHttpResponseParser.parseHead(DefaultHttpResponseParser.java:143)
    at org.apache.http.impl.conn.DefaultHttpResponseParser.parseHead(DefaultHttpResponseParser.java:57)
    at org.apache.http.impl.io.AbstractMessageParser.parse(AbstractMessageParser.java:260)
    at org.apache.http.impl.AbstractHttpClientConnection.receiveResponseHeader(AbstractHttpClientConnection.java:283)
    at org.apache.http.impl.conn.DefaultClientConnection.receiveResponseHeader(DefaultClientConnection.java:251)
    at org.apache.http.impl.conn.ManagedClientConnectionImpl.receiveResponseHeader(ManagedClientConnectionImpl.java:197)
    at org.apache.http.protocol.HttpRequestExecutor.doReceiveResponse(HttpRequestExecutor.java:271)
    at com.amazonaws.http.protocol.SdkHttpRequestExecutor.doReceiveResponse(SdkHttpRequestExecutor.java:66)
    at org.apache.http.protocol.HttpRequestExecutor.execute(HttpRequestExecutor.java:123)
    at org.apache.http.impl.client.DefaultRequestDirector.tryExecute(DefaultRequestDirector.java:685)
    at org.apache.http.impl.client.DefaultRequestDirector.execute(DefaultRequestDirector.java:487)
    at org.apache.http.impl.client.AbstractHttpClient.doExecute(AbstractHttpClient.java:863)
    at org.apache.http.impl.client.CloseableHttpClient.execute(CloseableHttpClient.java:82)
    at org.apache.http.impl.client.CloseableHttpClient.execute(CloseableHttpClient.java:57)
    at com.amazonaws.http.AmazonHttpClient.executeOneRequest(AmazonHttpClient.java:728)
    at com.amazonaws.http.AmazonHttpClient.executeHelper(AmazonHttpClient.java:489)
    at com.amazonaws.http.AmazonHttpClient.execute(AmazonHttpClient.java:310)
    at com.amazonaws.services.sqs.AmazonSQSClient.invoke(AmazonSQSClient.java:2419)
    at com.amazonaws.services.sqs.AmazonSQSClient.receiveMessage(AmazonSQSClient.java:1130)
    at com.example.staticsite.aws.SqsReceiverImpl.receiveReceipt(SqsReceiverImpl.java:57)
    at com.example.staticsite.core.processsite.ProcessSiteImpl.runOneTime(ProcessSiteImpl.java:59)
    at com.example.staticsite.core.processsite.ProcessSiteImpl.run(ProcessSiteImpl.java:44)
    at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:473)
    at java.util.concurrent.FutureTask.run(FutureTask.java:262)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1152)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:622)
    at java.lang.Thread.run(Thread.java:748) 

在我的代码中抛出异常的方法是:

public class SqsReceiverImpl implements SqsReceiver {
    private AmazonSQS client;
    private String queueUrl;

    @Inject
    public SqsReceiverImpl(AmazonSQS client,@Assisted String queueUrl) {        
        this.client = client;
        this.queueUrl = queueUrl;
    }

    public List<String> receiveReceipt() throws SqsReceiverException {
        if(queueUrl == null)
            throw new SqsReceiverException(SqsReceiverException.MESSAGE_NO_QUEURURL);
        ReceiveMessageRequest request = new ReceiveMessageRequest();
        request.setMaxNumberOfMessages(10);
        request.setQueueUrl(queueUrl);
        request.setWaitTimeSeconds(20);

        ReceiveMessageResult results = null;
        try {
            results = client.receiveMessage(request);
        }
        catch(OverLimitException oe){
            throw new SqsReceiverException("OverLimitException thrown");
        }
        catch(AmazonServiceException oe){
            throw new SqsReceiverException("AmazonServiceException thrown");
        }
        catch(AmazonClientException oe){
            throw new SqsReceiverException("AmazonClientException thrown");
        } 

SqsReceiverException的定义如下:

public class SqsReceiverException extends Exception{

    public SqsReceiverException(String messageNoQueururl) {
        super(messageNoQueururl);
    }
    public static final String MESSAGE_NO_QUEURURL = "Queue url not found. Se the queue url";
}

pom文件dependecies声明如下:

  <dependencies>
      <dependency>
       <groupId>junit</groupId>
       <artifactId>junit</artifactId>
       <version>4.11</version>
       <scope>test</scope>
    </dependency>
    <dependency>
    <groupId>log4j</groupId>
    <artifactId>log4j</artifactId>
    <version>1.2.17</version>
</dependency>
<dependency>
    <groupId>com.amazonaws</groupId>
    <artifactId>aws-java-sdk-sqs</artifactId>
    <version>1.10.12</version>
</dependency>
<dependency>
    <groupId>org.mockito</groupId>
    <artifactId>mockito-core</artifactId>
    <version>1.10.19</version>
    <scope>test</scope>
</dependency>
<dependency>
    <groupId>com.google.inject</groupId>
    <artifactId>guice</artifactId>
    <version>4.0</version>
</dependency>
<dependency>
  <groupId>com.google.inject.extensions</groupId>
  <artifactId>guice-assistedinject</artifactId>
  <version>4.0</version>
</dependency>
<dependency>
    <groupId>org.apache.commons</groupId>
    <artifactId>commons-lang3</artifactId>
    <version>3.4</version>
</dependency>
</dependencies>

产生此结果:

Maven dependencies tree

如果应该检查此异常是否未经检查,该怎么可能? 我在这里找不到什么东西?

注意

异常并不总是可重现的,因为只有在亚马逊服务缺少响应时才会在生产中发生。

更新

我已经验证了堆栈跟踪,直到到达AmazonHttpClient类,而这个代码正在捕获`IOException':

catch (IOException ioe) {
                if (log.isInfoEnabled()) {
                    log.info("Unable to execute HTTP request: " + ioe.getMessage(), ioe);
                }
                captureExceptionMetrics(ioe, awsRequestMetrics);
                awsRequestMetrics.addProperty(AWSRequestID, null);
                AmazonClientException ace = new AmazonClientException(
                        "Unable to execute HTTP request: " + ioe.getMessage(),
                        ioe);
                if (!shouldRetry(request.getOriginalRequest(),
                                p.apacheRequest,
                                ace,
                                p.requestCount,
                                config.getRetryPolicy())) {
                    throw lastReset(ace, request);
                }
                // Cache the retryable exception
                p.retriedException = ace;
            }

lastReset应该是抛出异常的责任,我不明白的是,记录的异常是如何org.apache.http.NoHttpResponseException ...

堆栈跟踪之前的行总是:

2017-09-15 07:41:39 INFO  AmazonHttpClient:496 - Unable to execute HTTP request: sqs.eu-west-1.amazonaws.com failed to respond

1 个答案:

答案 0 :(得分:2)

我的猜测是你是堆栈跟踪格式的受害者。

当你指责lastReset()作为罪魁祸首时,我认为你是对的。您可以在此处看到throws IOException从堆栈跟踪中消失。这个方法显然抛出了AmazonClientException(一个运行时异常),原来的NoHttpResponseException“在里面”。

你可以使用如下代码片段来模拟这个:

throw new AmazonClientException("Oh no!", new NoHttpResponseException("The AWS server doesn't like you"));

如果我将这行代码插入现有的Java应用程序(在本例中为Spring Boot),这就是我在Eclipse控制台中看到的内容:

No sign of the runtime exception

没有AmazonClientException的迹象!直到,我滚动到右边:

There it is

亚马逊决定采用未经检查的例外情况和documented it here

所以他们确实(我很确定)将IOException包装在运行时异常中,通过“对你处理的错误进行细粒度控制”来“帮助”你,尽管这不是'总是显而易见的。

所有这一切,我可能是错的。如果我是对的,我希望您可以在堆栈顶部看到自定义SqsReceiverException,因为您抓住了AmazonClientException

如果在堆栈跟踪之前没有标准输出的最后几行,很难确定。如果我没有标记,你可以张贴它们吗?

<强> 更新

您使用(AmazonHttpClient:496)更新问题的行是打印堆栈跟踪的行。将Throwable传递给log.info()时,将打印堆栈跟踪。 <=>在>包装并重新抛出异常之前记录此跟踪。

所以这一点似乎被“吞噬”了:

com.amazonaws.AmazonClientException: Unable to execute HTTP request: sqs.us-east-1.amazonaws.com failed to respond
    at com.amazonaws.http.AmazonHttpClient.executeHelper(AmazonHttpClient.java:500)
    at com.amazonaws.http.AmazonHttpClient.execute(AmazonHttpClient.java:310)
    at com.amazonaws.services.sqs.AmazonSQSClient.invoke(AmazonSQSClient.java:2419)
    at com.amazonaws.services.sqs.AmazonSQSClient.receiveMessage(AmazonSQSClient.java:1130)
    at httptest.Main.main(Main.java:32)
Caused by:

我无法与失踪的SqsReceiverException交谈。但我不认为DefaultRequestDirector.execute()的签名是谎言,我认为我们没有处理编译器错误。

也许您可以将oe.printStackTrace()添加到catch (AmazonClientException oe)块中?

最后,我建议使用调试器逐步完成此操作。要模拟生产问题,只需在DefaultHttpResponseParser:140处设置断点,并在执行此行后,将i更改为-1。然后逐步将堆栈备份回到您的代码。

我还在AmazonHttpClient:971设置了一个断点,这样我就可以更改retries并避免四次循环。