Spring Boot Health Check - SQS Consumer

旧街凉风 提交于 2021-02-11 06:36:25

问题


Is there a Spring Boot Actuator Health Check endpoint for SQS? I have built a SQS consumer and I want to check if SQS is up and running. I am not using JMSlistener for connecting to SQS but rather using Spring Cloud Libraries.

I implemented the below health check endpoint. This returns the below error when I delete the queue and try to hit the health check endpoint. If there is a connectivity issue or if the SQS service goes down , will I be getting a similar error which will eventually cause the health check endpoint to fail?

com.amazonaws.services.sqs.model.QueueDoesNotExistException: The specified queue does not exist for this wsdl version. (Service: AmazonSQS; Status Code: 400; Error Code: AWS.SimpleQueueService.NonExistentQueue; Request ID: cd8e205d-dc43-535e-931f-7332733bd16c)

public class SqsQueueHealthIndicator extends AbstractHealthIndicator {

    private final AmazonSQSAsync amazonSQSAsync;
    private final String queueName;

    public SqsQueueHealthIndicator(AmazonSQSAsync amazonSQSAsync, String queueName) {
        this.amazonSQSAsync = amazonSQSAsync;
        this.queueName = queueName;
    }

    @Override
    protected void doHealthCheck(Health.Builder builder) {
        try {
            amazonSQSAsync.getQueueUrl(queueName);
            builder.up();
        } catch (QueueDoesNotExistException e) {
            e.printStackTrace();
            builder.down(e);
        }
    }

}

Beans

@Bean
SqsQueueHealthIndicator queueHealthIndicator(@Autowired AmazonSQSAsync amazonSQSAsync, @Value("${sqs.queueName}") String queueName) {
    return new SqsQueueHealthIndicator(amazonSQSAsync, queueName);
}

@Bean
SqsQueueHealthIndicator deadLetterQueueHealthIndicator(@Autowired AmazonSQSAsync amazonSQSAsync, @Value("${sqs.dlQueueName}") String deadLetterQueueName) {
    return new SqsQueueHealthIndicator(amazonSQSAsync, deadLetterQueueName);
}

回答1:


You have to write a custom health check like below to check your queue exists or not by calling getQueueUrl using AWS Java SDK lib.

    @Component
public class SQSHealthCheck implements HealthIndicator {
    @Override
    public Health health() {
        int errorCode = check(); // perform some specific health check
        if (errorCode != 0) {
            return Health.down()
              .withDetail("Error Code", errorCode).build();
        }
        return Health.up().build();
    }

    public int check() {
        /**
        your logic to check queue exists or not using by calling getQueueUrl . e.g you will get queue url of a queue named "SampleQueue" like https://sqs.us-east-1.amazonaws.com/12XXX56789XXXX/SampleQueue
        **/
        return 0; // 0 or 1 based on result
    }
    }


来源:https://stackoverflow.com/questions/50283239/spring-boot-health-check-sqs-consumer

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!