# AmazonSqsService.java

    package cloud.wconnect.finance.service;

    import cloud.wconnect.commonlib.cons.BaseErrorCodeEnum;
    import cloud.wconnect.commonlib.exception.InternalServerErrorException;
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.beans.factory.annotation.Value;
    import org.springframework.scheduling.annotation.Async;
    import org.springframework.stereotype.Service;
    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;
    import java.util.Collections;
    import java.util.List;

    /**
     * 服务类：Amazon SQS
     *
     * @author chenx
     * @date 2017/2/11
     */
    @Service
    public class AmazonSqsService {

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

        private SqsClient client;

        @Autowired
        private AlertEmailService alertEmailService;

        /**
         * 项目启动加载：初始化客户端到SQS的连接
         *
         * @return void
         * @author lichen
         * @date 2023/6/6
         */
        @PostConstruct
        public void initClient() {
            this.client = SqsClient.builder()
                    .credentialsProvider(() -> AwsBasicCredentials.create(accessKey, secretKey))
                    .region(Region.of(sqsRegion))
                    .httpClient(UrlConnectionHttpClient.builder().build())
                    .build();
        }

        /**
         * 向指定的队列发送消息
         *
         * @param queueUrl       队列url
         * @param messageContent 消息内容
         * @return void
         * @author lichen
         * @date 2023/6/6
         */
        @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);
            }
        }

        /**
         * 批量获取队列消息(最多10条)
         *
         * @param queueUrl 队列url
         * @return java.util.List<com.amazonaws.services.sqs.model.Message>
         * @author lichen
         * @date 2023/6/6
         */
        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();
        }

        /**
         * 删除指定队列的消息
         *
         * @param queueUrl      队列url
         * @param receiptHandle 消息句柄
         * @return void
         * @author lichen
         * @date 2023/6/6
         */
        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);
            }
        }
    }