代码之家  ›  专栏  ›  技术社区  ›  Ian Stewart

AWS Java SDK:调用AmazonSQSClient.receiveMessage时的异常中止

  •  6
  • Ian Stewart  · 技术社区  · 6 年前

    我们试图从运行在EC2实例上的Java程序中读取Amazon SQL Sqs队列。有时,我们会得到 com.amazonaws.AbortedException .我们的代码如下:

    AmazonSQSClient sqs = new AmazonSQSClient(myCredentials);
    ReceiveMessageRequest request = new ReceiveMessageRequest()
                .withWaitTimeSeconds(20)
                .withMaxNumberOfMessages(1)
                .withQueueUrl(queueUrl);
    ReceiveMessageResult result = sqs.receiveMessage(request);
    

    我们的堆栈跟踪如下:

    com.amazonaws.AbortedException: 
    at com.amazonaws.internal.SdkFilterInputStream.abortIfNeeded(SdkFilterInputStream.java:51)
    at com.amazonaws.internal.SdkFilterInputStream.read(SdkFilterInputStream.java:65)
    at com.amazonaws.event.ProgressInputStream.read(ProgressInputStream.java:159)
    at com.sun.org.apache.xerces.internal.impl.XMLEntityManager$RewindableInputStream.read(XMLEntityManager.java:2890)
    at com.sun.org.apache.xerces.internal.impl.XMLEntityManager.setupCurrentEntity(XMLEntityManager.java:674)
    at com.sun.org.apache.xerces.internal.impl.XMLEntityManager.startEntity(XMLEntityManager.java:1304)
    at com.sun.org.apache.xerces.internal.impl.XMLEntityManager.startDocumentEntity(XMLEntityManager.java:1255)
    at com.sun.org.apache.xerces.internal.impl.XMLDocumentScannerImpl.setInputSource(XMLDocumentScannerImpl.java:253)
    at com.sun.org.apache.xerces.internal.impl.XMLStreamReaderImpl.setInputSource(XMLStreamReaderImpl.java:199)
    at com.sun.org.apache.xerces.internal.impl.XMLStreamReaderImpl.<init>(XMLStreamReaderImpl.java:184)
    at com.sun.xml.internal.stream.XMLInputFactoryImpl.getXMLStreamReaderImpl(XMLInputFactoryImpl.java:277)
    at com.sun.xml.internal.stream.XMLInputFactoryImpl.createXMLStreamReader(XMLInputFactoryImpl.java:129)
    at com.sun.xml.internal.stream.XMLInputFactoryImpl.createXMLEventReader(XMLInputFactoryImpl.java:78)
    at com.amazonaws.http.StaxResponseHandler.handle(StaxResponseHandler.java:91)
    at com.amazonaws.http.StaxResponseHandler.handle(StaxResponseHandler.java:43)
    at com.amazonaws.http.response.AwsResponseHandlerAdapter.handle(AwsResponseHandlerAdapter.java:70)
    at com.amazonaws.http.AmazonHttpClient$RequestExecutor.handleResponse(AmazonHttpClient.java:1501)
    at com.amazonaws.http.AmazonHttpClient$RequestExecutor.executeOneRequest(AmazonHttpClient.java:1222)
    at com.amazonaws.http.AmazonHttpClient$RequestExecutor.executeHelper(AmazonHttpClient.java:1035)
    at com.amazonaws.http.AmazonHttpClient$RequestExecutor.doExecute(AmazonHttpClient.java:747)
    at com.amazonaws.http.AmazonHttpClient$RequestExecutor.executeWithTimer(AmazonHttpClient.java:721)
    at com.amazonaws.http.AmazonHttpClient$RequestExecutor.execute(AmazonHttpClient.java:704)
    at com.amazonaws.http.AmazonHttpClient$RequestExecutor.access$500(AmazonHttpClient.java:672)
    at com.amazonaws.http.AmazonHttpClient$RequestExecutionBuilderImpl.execute(AmazonHttpClient.java:654)
    at com.amazonaws.http.AmazonHttpClient.execute(AmazonHttpClient.java:518)
    at com.amazonaws.services.sqs.AmazonSQSClient.doInvoke(AmazonSQSClient.java:1663)
    at com.amazonaws.services.sqs.AmazonSQSClient.invoke(AmazonSQSClient.java:1639)
    at com.amazonaws.services.sqs.AmazonSQSClient.receiveMessage(AmazonSQSClient.java:1269)
    at <our code>
    

    文件 com.amazonaws.services.sqs.AmazonSQSClient 不会告诉我们期望异常中止。

    我们使用的是1.11.77版的SDK。

    任何帮助都将不胜感激。谢谢。

    1 回复  |  直到 6 年前
        1
  •  2
  •   Krease    6 年前

    尽管JavaDoc开着 AbortedException 是稀疏的,它是 AmazonClientException &安培; RuntimeException (无需申报)。此外,还有 AWS Exception handling docs 说:

    AuthOnClient异常指示Java客户端代码中出现问题,当试图向AWS发送请求时,或试图解析AWS的响应时发生问题。AmazonClientException通常比AmazonServiceException更严重,它指出了一个主要问题,即阻止客户端对AWS服务进行服务调用。例如,如果试图在一个客户端上调用一个操作,则如果没有网络连接可用,则Java的AWS SDK会抛出一个AdMaunclieExtExchange。

    中止异常 通常在sdk处理 InterruptedException (即: the thread was signalled to stop doing work )。不过,出于您的目的,您可能只想重试该操作。

    (如果是你做的 Thread.interrupt 不过,要想造成这种情况,就要看您想如何继续了——要么将其视为停止工作的信号,要么只需重试该操作)