# MarketDemandTrendWorker.java

    package cloud.wconnect.rateshop.consumer.marketdemand;

    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;

    /**
     * 市场趋势抓取队列的worker
     *
     * @author lichen
     * @date 2024/4/19
     */
    @Slf4j
    @Component
    public class MarketDemandTrendWorker {

        @Autowired
        private AmazonSqsService amazonSqsService;
        @Autowired
        private MailInternalService mailInternalService;
        @Autowired
        private MarketDemandTrendConsumer marketDemandTrendConsumer;
        /**
         * 市场需求 的队列url
         */
        @Value("${sqs.queue-url.market_demand_queue}")
        private String marketDemandUrl;

        /**
         * 抓取市场趋势事件
         *
         * @author lichen
         * @date 2024/4/19
         */
        @Scheduled(cron = "${schedule.cron.market-demand-worker}")
        public void run() {
            List<Message> messages = amazonSqsService.getMessagesBatch(marketDemandUrl);
            if (CollectionUtils.isNotEmpty(messages)) {
                for (Message message : messages) {
                    try {
                        //处理消息
                        marketDemandTrendConsumer.handleMessage(message);
                    } catch (Exception e) {
                        log.error("SQS consume error[MarketDemandWorker] - Message: {}; Exception: {}", message.body(), e.getMessage());
                        if (RateLimitUtils.tryAcquire(this)) {
                            sendAlertEmail(marketDemandUrl, message.body(), e.getMessage());
                        }
                    } finally {
                        // 删除消息
                        amazonSqsService.deleteMessage(marketDemandUrl, 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 MarketDemandWorker";
            // 邮件内容
            String mailContent = MessageFormat.format("SQS url:{0}<br/>Message:{1}<br/>Exception:{2}",
                    sqsURL, message, exception);
            // 发送 alert
            mailInternalService.sendAlertEmail(subject, mailContent);
        }

    }