ale*_*oot 5 java exception amazon-sqs apache-httpclient-4.x
我遇到了一个奇怪的问题,其中 aorg.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)
Run Code Online (Sandbox Code Playgroud)
在我的代码中抛出异常的方法是:
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");
}
Run Code Online (Sandbox Code Playgroud)
的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";
}
Run Code Online (Sandbox Code Playgroud)
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>
Run Code Online (Sandbox Code Playgroud)
产生这个结果:
这个异常在应该被检查的情况下怎么可能被威胁为未经检查?有什么我在这里想念的吗?
该异常并不总是可重现的,因为它仅在生产中发生 Amazon 服务缺少响应时。
我已经验证了堆栈跟踪直到到达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;
}
Run Code Online (Sandbox Code Playgroud)
并且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
Run Code Online (Sandbox Code Playgroud)
我的猜测是您是堆栈跟踪格式化的受害者。
lastReset()
当你指出罪魁祸首时,我认为你是对的。这是您看到throws IOException
从堆栈跟踪中消失的地方。这个方法显然抛出了一个AmazonClientException
(运行时异常),而原始的NoHttpResponseException
“内部”是它。
您可以使用如下代码片段来模拟这一点:
throw new AmazonClientException("Oh no!", new NoHttpResponseException("The AWS server doesn't like you"));
Run Code Online (Sandbox Code Playgroud)
如果我将这行代码插入现有的 Java 应用程序(在本例中为 Spring Boot),我会在 Eclipse 控制台中看到以下内容:
没有任何迹象AmazonClientException
!直到我向右滚动:
亚马逊决定采用未经检查的例外,并在此处记录。
因此,它们确实(我很确定)将您包装IOException
在运行时异常中,通过为您提供“对您处理的错误的细粒度控制”来“帮助”您,尽管这并不总是显而易见的。
综上所述,我可能是错的。如果我是对的,我希望SqsReceiverException
在堆栈的顶部看到您的自定义,因为您确实捕获了AmazonClientException
.
如果没有堆栈跟踪之前标准输出的最后几行,很难确定。如果我不合时宜,你能把它们贴出来吗?
更新
您用 ( ) 更新问题的行AmazonHttpClient:496
是打印堆栈跟踪的行。当您将 a 传递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:
Run Code Online (Sandbox Code Playgroud)
我无法与失踪者交谈SqsReceiverException
。但我不认为 的签名DefaultRequestDirector.execute()
是在说谎,而且我不认为我们正在处理编译器错误。
也许你可以添加oe.printStackTrace()
到你的catch (AmazonClientException oe)
区块?
最后,我建议使用调试器逐步完成此操作。要模拟您的生产问题,只需在 处设置一个断点DefaultHttpResponseParser:140
,并在执行此行后,更改i
为 -1。然后沿着堆栈一路返回到您的代码。
我还设置了一个断点,AmazonHttpClient:971
这样我就可以更改retries
并避免循环四次。
归档时间: |
|
查看次数: |
1121 次 |
最近记录: |