# AmazonSqsServiceApitest.java

    package cloud.wconnect.frontoffice.apitest;

    import java.net.URI;
    import java.util.ArrayList;
    import java.util.Collections;
    import java.util.List;

    import cloud.wconnect.commonlib.cons.BaseErrorCodeEnum;
    import cloud.wconnect.commonlib.exception.InternalServerErrorException;
    import cloud.wconnect.frontoffice.service.AlertEmailService;
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.beans.factory.annotation.Value;

    import cloud.wconnect.frontoffice.service.AmazonSqsService;
    import org.springframework.scheduling.annotation.Async;
    import software.amazon.awssdk.auth.credentials.AwsBasicCredentials;
    import software.amazon.awssdk.http.urlconnection.UrlConnectionHttpClient;
    import software.amazon.awssdk.regions.Region;
    import software.amazon.awssdk.services.sqs.SqsClient;
    import software.amazon.awssdk.services.sqs.model.*;
    import jakarta.annotation.PostConstruct;

    public class AmazonSqsServiceApitest extends AmazonSqsService {

        @Value("${sqs.access-key}")
        private String accessKey;
        @Value("${sqs.secret-key}")
        private String secretKey;
        @Value("${sqs.region}")
        private String sqsRegion;

        @Value("${localstack.endpoint}")
        private String localstackEndpoint;

        private SqsClient client;

        @Autowired
        private AlertEmailService alertEmailService;

        // 移除构造函数中的初始化，改为使用@PostConstruct
        @PostConstruct
        public void initClient() {
            this.client = SqsClient.builder()
                    .endpointOverride(URI.create(localstackEndpoint))
                    .credentialsProvider(() -> AwsBasicCredentials.create(accessKey, secretKey))
                    .region(Region.of(sqsRegion))
                    .httpClient(UrlConnectionHttpClient.builder().build())
                    .build();
        }

        @Async
        public void sendMessage(String queueUrl, String messageContent) {
            try {
                client.sendMessage(SendMessageRequest.builder()
                        .queueUrl(queueUrl)
                        .messageBody(messageContent)
                        .build());
            } catch (Exception e) {
                //发送alertEmail
                alertEmailService.sendEmail4SqsException(e, queueUrl);
                throw new InternalServerErrorException(BaseErrorCodeEnum.INTERNAL_SERVICE_ERROR.getI18nCode(),
                        BaseErrorCodeEnum.INTERNAL_SERVICE_ERROR.getCode(), e.getMessage(), e);
            }
        }

        public List<Message> getMessagesBatch(String queueUrl) {
            try {
                ReceiveMessageResponse receiveMessageResponse = client.receiveMessage(ReceiveMessageRequest.builder()
                        .queueUrl(queueUrl)
                        .maxNumberOfMessages(10)
                        .waitTimeSeconds(15)
                        .build());
                return receiveMessageResponse.messages();
            } catch (Exception e) {
                //发送alertEmail
                alertEmailService.sendEmail4SqsException(e, queueUrl);
            }
            return Collections.emptyList();
        }

        public void deleteMessage(String queueUrl, String receiptHandle) {
            try {
                client.deleteMessage(DeleteMessageRequest.builder()
                        .queueUrl(queueUrl)
                        .receiptHandle(receiptHandle)
                        .build());
            } catch (Exception e) {
                //发送alertEmail
                alertEmailService.sendEmail4SqsException(e, queueUrl);
                throw new InternalServerErrorException(BaseErrorCodeEnum.INTERNAL_SERVICE_ERROR.getI18nCode(),
                        BaseErrorCodeEnum.INTERNAL_SERVICE_ERROR.getCode(), e.getMessage(), e);
            }
        }
    }