java Spring消息队列 监听

package com.connext.soa.platform.business.config;

import com.connext.soa.platform.business.consumer.model.SyncVariantToES;
import com.connext.soa.platform.business.service.ESIndexDataBuildService;
import lombok.extern.slf4j.Slf4j;
import org.joda.time.DateTime;
import org.joda.time.LocalDateTime;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.elasticsearch.core.query.IndexQuery;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled;

import java.util.ArrayList;
import java.util.List;
@Configuration
@EnableScheduling
@Slf4j
public class ESQueueConfig {

    private final ESIndexDataBuildService esIndexDataBuildService;

    private static final List skuCodeList = new ArrayList<>();
    private static final List spuInfoList = new ArrayList<>();
    private static final List spuInfoDeleteList = new ArrayList<>();

    public ESQueueConfig(ESIndexDataBuildService esIndexDataBuildService) {
        this.esIndexDataBuildService = esIndexDataBuildService;
    }

    private static LocalDateTime sendSkuTime = LocalDateTime.now();
    private static LocalDateTime sendSpuTime = LocalDateTime.now();
    private static LocalDateTime sendSpuDeleteTime = LocalDateTime.now();

    @Scheduled(fixedRate = 100)
    public void processEsSkuData() {
        while (!ESQueue.skuQueue.isEmpty() || (!skuCodeList.isEmpty() && LocalDateTime.now().minusSeconds(15).isAfter(sendSkuTime))) {
            if(!ESQueue.skuQueue.isEmpty()) {
                skuCodeList.add(ESQueue.skuQueue.poll());
            }
            if (skuCodeList.size() >= 500 || LocalDateTime.now().minusSeconds(15).isAfter(sendSkuTime)) {
                log.info("定时线程开始处理ES数据SKU同步size{}",skuCodeList.size());
                sendSkuTime = LocalDateTime.now();
                esIndexDataBuildService.esSaveBySkuCodeBatch(skuCodeList);
                skuCodeList.clear();
            }
        }
    }

    @Scheduled(fixedRate = 100)
    public void processEsSaveSpuData() {
        while (!ESQueue.spuSaveQueue.isEmpty() || (!spuInfoList.isEmpty() && LocalDateTime.now().minusSeconds(15).isAfter(sendSpuTime))) {
            if(!ESQueue.spuSaveQueue.isEmpty()) {
                spuInfoList.add(ESQueue.spuSaveQueue.poll());
            }
            if (spuInfoList.size() >= 500 || LocalDateTime.now().minusSeconds(15).isAfter(sendSpuTime)) {
                log.info("定时线程开始处理ES数据SPU同步size{}",spuInfoList.size());
                sendSpuTime = LocalDateTime.now();
                esIndexDataBuildService.esSaveByVariantIdsBatch(spuInfoList);
                spuInfoList.clear();
            }
        }
    }

    @Scheduled(fixedRate = 300)
    public void processEsDeleteSpuData() {
        while (!ESQueue.spuDeleteQueue.isEmpty() || (!spuInfoDeleteList.isEmpty() && LocalDateTime.now().minusSeconds(15).isAfter(sendSpuDeleteTime))) {
            if (!ESQueue.spuDeleteQueue.isEmpty()){
                spuInfoDeleteList.add(ESQueue.spuDeleteQueue.poll());
            }
            if (spuInfoDeleteList.size() >= 50 || LocalDateTime.now().minusSeconds(15).isAfter(sendSpuDeleteTime)) {
                log.info("定时线程开始处理ES数据删除SPU同步size{}",spuInfoDeleteList.size());
                sendSpuDeleteTime = LocalDateTime.now();
                esIndexDataBuildService.esDeleteBatch(spuInfoDeleteList);
                spuInfoDeleteList.clear();
            }
        }
    }
}

你可能感兴趣的:(java,开发语言)