gpt4 book ai didi

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

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

我遇到了一个奇怪的问题,即 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文件依赖声明如下:

  <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 类,而这段代码正在捕获“I​​OException”:

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

最佳答案

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

当您指出 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

Amazon 决定采用未经检查的异常,并且 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) block 中?

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

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

关于java - NoHttpResponseException 作为运行时异常抛出,即使是已检查的异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46828057/

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