# AmazonSqsSchedulerService.java

    package cloud.wconnect.rateshop.service;

    import cloud.wconnect.commonlib.cons.BaseErrorCodeEnum;
    import cloud.wconnect.commonlib.exception.InternalServerErrorException;
    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 jakarta.annotation.Resource;
    import java.util.Collections;
    import java.util.List;

    /**
     * 服务类：Amazon SQS Scheduler
     *
     * @author zc
     * @date 2024/6/5
     */
    @Service
    public class AmazonSqsSchedulerService {

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

        private SqsClient client;

        @Resource
        private AlertEmailService alertEmailService;

        /**
         * 项目启动加载：初始化客户端到SQS的连接
         *
         * @author zc
         * @date 2024/6/5
         */
        @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 消息内容
         * @author zc
         * @date 2024/6/5
         */
        @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 zc
         * @date 2024/6/5
         */
        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 消息句柄
         * @author zc
         * @date 2024/6/5
         */
        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);
            }
        }
    }