# CalculateIwcWorker.java

    package cloud.wconnect.rateshop.consumer.iwc;

    import cloud.wconnect.commonlib.service.MailInternalService;
    import cloud.wconnect.commonlib.util.RateLimitUtils;
    import cloud.wconnect.rateshop.service.AmazonSqsService;
    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;

    /**
     * IWC计算的Worker
     *
     * @author lichen
     * @date 2025/4/3
     */
    @Slf4j
    @Component
    public class CalculateIwcWorker {

        @Autowired
        private AmazonSqsService amazonSqsService;
        @Autowired
        private MailInternalService mailInternalService;
        @Autowired
        private CalculateIwcConsumer calculateIwcConsumer;
        /**
         * 计算IWC的队列url
         */
        @Value("${sqs.queue-url.iwc_queue}")
        private String iwcUrl;

        /**
         * 抓取市场趋势事件
         *
         * @author lichen
         * @date 2024/4/19
         */
        @Scheduled(cron = "${schedule.cron.market-demand-worker}")
        public void run() {
            List<Message> messages = amazonSqsService.getMessagesBatch(iwcUrl);
            if (CollectionUtils.isNotEmpty(messages)) {
                for (Message message : messages) {
                    try {
                        //处理消息
                        calculateIwcConsumer.handleMessage(message);
                    } catch (Exception e) {
                        log.error("SQS consume error[IWCCalculateWorker] - Message: {}; Exception: {}", message.body(), e.getMessage());
                        if (RateLimitUtils.tryAcquire(this)) {
                            sendAlertEmail(iwcUrl, message.body(), e.getMessage());
                        }
                    } finally {
                        // 删除消息
                        amazonSqsService.deleteMessage(iwcUrl, message.receiptHandle());
                    }
                }
                run();
            }
        }

        /**
         * 发送alertEmail
         *
         * @param sqsURL    队列地址
         * @param message   消息
         * @param exception 异常信息
         * @author lichen
         * @date 2024/4/19
         */
        public void sendAlertEmail(String sqsURL, String message, String exception) {
            // 主题
            String subject = "[Warning] - process IWCCalculateWorker";
            // 邮件内容
            String mailContent = MessageFormat.format("SQS url:{0}<br/>Message:{1}<br/>Exception:{2}",
                    sqsURL, message, exception);
            // 发送 alert
            mailInternalService.sendAlertEmail(subject, mailContent);
        }

    }