丢失连接后,Spring AWS SQS重新连接

时间:2015-06-10 16:39:24

标签: spring amazon-web-services amazon-sqs spring-cloud

我正在使用带有Spring Boot的Spring Cloud AWS(1.0.1.RELEASE)来运行SQS使用者。应用程序运行正常,但当它失去网络连接时(例如,如果我在笔记本电脑上运行时关闭我的WIFI),我看到控制台上的错误,应用程序永远不会恢复。它只是挂在那里,在网络可用后不再重新连接。我必须将它杀死并提起它。我如何强迫它自行恢复?

// Spring Boot entry point: 
public static void main(String[] args) {
    SpringApplication.run(MyConsumerConfiguration.class, args);
}

// Message Listener (A different class)
@MessageMapping(value = "myLogicalQueueName" )
public void receive(MyPOJO object) {

}

我在控制台看到的错误:

  

线程中的异常" simpleMessageListenerContainer-1" com.amazonaws.AmazonClientException:无法执行HTTP请求:sqs.us-east-1.amazonaws.com       在com.amazonaws.http.AmazonHttpClient.executeHelper(AmazonHttpClient.java:473)       在com.amazonaws.http.AmazonHttpClient.execute(AmazonHttpClient.java:297)       在com.amazonaws.services.sqs.AmazonSQSClient.invoke(AmazonSQSClient.java:2422)       在com.amazonaws.services.sqs.AmazonSQSClient.receiveMessage(AmazonSQSClient.java:1130)       在com.amazonaws.services.sqs.AmazonSQSAsyncClient $ 23.call(AmazonSQSAsyncClient.java:1678)       在com.amazonaws.services.sqs.AmazonSQSAsyncClient $ 23.call(AmazonSQSAsyncClient.java:1676)       at java.util.concurrent.FutureTask.run(FutureTask.java:266)       在java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)       at java.util.concurrent.ThreadPoolExecutor $ Worker.run(ThreadPoolExecutor.java:617)       在java.lang.Thread.run(Thread.java:745

1 个答案:

答案 0 :(得分:3)

我刚刚弄清楚了网络连接丢失后SQS无法重新连接的问题。

实际上,org.springframework.cloud.aws.messaging.listener.SimpleMessageListenerContainer.java

的当前Spring AWS实施中似乎存在问题
private class AsynchronousMessageListener implements Runnable {

    private final QueueAttributes queueAttributes;
    private final String logicalQueueName;

    private AsynchronousMessageListener(String logicalQueueName, QueueAttributes queueAttributes) {
        this.logicalQueueName = logicalQueueName;
        this.queueAttributes = queueAttributes;
    }

    @Override
    public void run() {
        while (isRunning()) {
            ReceiveMessageResult receiveMessageResult = getAmazonSqs().receiveMessage(this.queueAttributes.getReceiveMessageRequest());
            CountDownLatch messageBatchLatch = new CountDownLatch(receiveMessageResult.getMessages().size());
            for (Message message : receiveMessageResult.getMessages()) {
                if (isRunning()) {
                    MessageExecutor messageExecutor = new MessageExecutor(this.logicalQueueName, message, this.queueAttributes);
                    getTaskExecutor().execute(new SignalExecutingRunnable(messageBatchLatch, messageExecutor));
                } else {
                    break;
                }
            }
            try {
                messageBatchLatch.await();
            } catch (InterruptedException e) {
                Thread.currentThread().interrupt();
            }
        }
    }
}

上面的代码旋转了一个新线程,该线程执行轮询到SQS队列以获取消息。删除网络连接后getAmazonSqs().receiveMessage(this.queueAttributes.getReceiveMessageRequest())抛出UnknownHostException,这不会在代码中处理并导致线程终止。 因此,当稍后建立网络连接时,没有线程轮询队列来检索数据。

我已经为Spring提出了一个问题。以下是链接:https://github.com/spring-cloud/spring-cloud-aws/issues/82

希望这能解释一切。