# CancellationFlightsWorker.java

    package cloud.wconnect.rateshop.consumer.airport;

    import cloud.wconnect.commonlib.service.MailInternalService;
    import cloud.wconnect.commonlib.util.JsonUtils;
    import cloud.wconnect.commonlib.util.RateLimitUtils;
    import cloud.wconnect.rateshop.pojo.dto.rateshop.consumer.airport.CancellationFlightsMessage;
    import cloud.wconnect.rateshop.service.AmazonSqsService;
    import cloud.wconnect.rateshop.service.rateshop.ICancellationFlightsService;
    import com.fasterxml.jackson.databind.ObjectMapper;
    import lombok.extern.slf4j.Slf4j;
    import org.apache.commons.collections.CollectionUtils;
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.beans.factory.annotation.Value;
    import org.springframework.scheduling.annotation.Scheduled;
    import org.springframework.stereotype.Component;
    import software.amazon.awssdk.services.sqs.model.Message;

    import java.text.MessageFormat;
    import java.util.List;

    @Slf4j
    @Component
    public class CancellationFlightsWorker {

        @Autowired
        private ICancellationFlightsService cancellationFlightsService;
        @Autowired
        private AmazonSqsService amazonSqsService;
        @Autowired
        private MailInternalService mailInternalService;
        @Autowired
        private ObjectMapper objectMapper;

        /**
         * 机场取消航班数量消息的队列url
         */
        @Value("${sqs.queue-url.cancellation_flights_queue}")
        private String queueUrl;

        /**
         * 消费 rate shop 功能 机场取消航班数量 队列任务
         *
         * @author zc
         * @date 2024/3/25
         */
        @Scheduled(cron = "0 0/1 * * * ?", zone = "${default.timezone}")
        public void run() {
            List<Message> messages = amazonSqsService.getMessagesBatch(queueUrl);
            if (CollectionUtils.isNotEmpty(messages)) {
                for (Message message : messages) {
                    // message 为 json 字符串：
                    // 警告: 执行任务的具体方法应该使用新的线程，而不是在本线程执行（要确保本线程不会被阻塞或异常退出）

                    try {
                        // 获取取消航班数量数据
                        List<CancellationFlightsMessage> cancellationFlightsMessages = JsonUtils.convertStringToList(objectMapper,
                                message.body(), CancellationFlightsMessage.class);
                        // 处理消息数据
                        cancellationFlightsService.handleMessages(cancellationFlightsMessages);
                    } catch (Exception e) {
                        log.error("SQS consume error[CancellationFlightsWorker] - Message: {}; Exception: {}", message.body(), e.getMessage());
                        if (RateLimitUtils.tryAcquire(this)) {
                            sendAlertEmail(queueUrl, message.body(), e.getMessage());
                        }
                    } finally {
                        // 删除消息
                        amazonSqsService.deleteMessage(queueUrl, message.receiptHandle());
                    }
                }
                run();
            }
        }

        /**
         * 发送 alertEmail
         *
         * @param queueUrl  消息队列 url
         * @param message   消息
         * @param exception 异常信息
         * @author zc
         * @date 2024/3/25
         */
        public void sendAlertEmail(String queueUrl, String message, String exception) {
            String subject = "[Warning] - process CancellationFlightsWorker";
            String mailContent = MessageFormat.format("SQS url:{0}<br/>Message:{1}<br/>Exception:{2}",
                    queueUrl, message, exception);
            mailInternalService.sendAlertEmail(subject, mailContent);
        }
    }