|
|
@@ -12,21 +12,31 @@ import com.alibaba.fastjson.JSONArray;
|
|
|
import com.alibaba.fastjson.JSONObject;
|
|
|
import lombok.extern.slf4j.Slf4j;
|
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
|
+import org.springframework.scheduling.annotation.EnableScheduling;
|
|
|
+import org.springframework.scheduling.annotation.Scheduled;
|
|
|
import org.springframework.stereotype.Service;
|
|
|
|
|
|
-import java.util.ArrayList;
|
|
|
-import java.util.HashMap;
|
|
|
-import java.util.List;
|
|
|
-import java.util.Map;
|
|
|
+import javax.annotation.PreDestroy;
|
|
|
+import java.util.*;
|
|
|
|
|
|
@Service
|
|
|
@Slf4j
|
|
|
+@EnableScheduling
|
|
|
public class KuaiShouAccountReportServiceImpl implements IKuaiShouAccountReportService {
|
|
|
@Autowired
|
|
|
private IKuaiShouErrorLogService errorLogService;
|
|
|
@Autowired
|
|
|
private KuaiShouAccountReportMapper accountReportMapper;
|
|
|
|
|
|
+ private static final int MAX_BATCH_SIZE = 5000;
|
|
|
+ private static final long MAX_WAIT_TIME_MS = 2 * 60 * 1000;
|
|
|
+
|
|
|
+ private final List<KuaishouAccountReportHour> advertiserReportHourBuffer = Collections.synchronizedList(new ArrayList<>());
|
|
|
+ private final List<KuaishouAccountReportDaily> advertiserReportDailyBuffer = Collections.synchronizedList(new ArrayList<>());
|
|
|
+ private long advertiserReportHourLastFlushTime = System.currentTimeMillis();
|
|
|
+ private long advertiserReportDailyLastFlushTime = System.currentTimeMillis();
|
|
|
+
|
|
|
+
|
|
|
@Override
|
|
|
public void queryAccountReportHourly(String accessToken, Long advertiserId, String startDate, String endDate, int page) {
|
|
|
try {
|
|
|
@@ -73,11 +83,7 @@ public class KuaiShouAccountReportServiceImpl implements IKuaiShouAccountReportS
|
|
|
}
|
|
|
if (!Check.isNull(addList)) {
|
|
|
accountReportMapper.replaceBatcKuaiShouAccountReportHour(addList);
|
|
|
- try {
|
|
|
- accountReportMapper.replaceBatcKuaiShouAccountReportHourSD(addList);
|
|
|
- } catch (Exception e) {
|
|
|
- log.error("replaceBatcKuaiShouAccountReportHourSD", e);
|
|
|
- }
|
|
|
+ advertiserReportHourBuffer.addAll(addList);
|
|
|
}
|
|
|
if (details.size() >= 2000) {
|
|
|
queryAccountReportHourly(accessToken, advertiserId, startDate, endDate, page + 1);
|
|
|
@@ -131,11 +137,7 @@ public class KuaiShouAccountReportServiceImpl implements IKuaiShouAccountReportS
|
|
|
}
|
|
|
if (!Check.isNull(addList)) {
|
|
|
accountReportMapper.replaceBatcKuaiShouAccountReportDaily(addList);
|
|
|
- try {
|
|
|
- accountReportMapper.replaceBatcKuaiShouAccountReportDailySD(addList);
|
|
|
- } catch (Exception e) {
|
|
|
- log.error("replaceBatcKuaiShouAccountReportDailySD", e);
|
|
|
- }
|
|
|
+ advertiserReportDailyBuffer.addAll(addList);
|
|
|
}
|
|
|
if (details.size() >= 2000) {
|
|
|
queryAccountReportDaily(accessToken, advertiserId, startDate, endDate, page + 1);
|
|
|
@@ -153,4 +155,69 @@ public class KuaiShouAccountReportServiceImpl implements IKuaiShouAccountReportS
|
|
|
public void updateClAccountStatus() {
|
|
|
accountReportMapper.updateClAccountStatus();
|
|
|
}
|
|
|
+
|
|
|
+ @Scheduled(cron = "*/20 * * * * ?") // 每30秒检查一次
|
|
|
+ public void flushIfNeeded() {
|
|
|
+ log.info("开始执行账户报表定时批处理任务....");
|
|
|
+ synchronized (advertiserReportHourBuffer) {
|
|
|
+ long now = System.currentTimeMillis();
|
|
|
+ if (advertiserReportHourBuffer.size() >= MAX_BATCH_SIZE ||
|
|
|
+ (now - advertiserReportHourLastFlushTime) >= MAX_WAIT_TIME_MS && !advertiserReportHourBuffer.isEmpty()) {
|
|
|
+ try {
|
|
|
+ log.info("Flushed advertiserReportHour batch size: {}", advertiserReportHourBuffer.size());
|
|
|
+ accountReportMapper.replaceBatcKuaiShouAccountReportHourSD(new ArrayList<>(advertiserReportHourBuffer)); advertiserReportHourBuffer.clear();
|
|
|
+ advertiserReportHourLastFlushTime = now;
|
|
|
+ } catch (Exception e) {
|
|
|
+ log.error("Error flushing advertiserReportHour batch", e);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ synchronized (advertiserReportDailyBuffer) {
|
|
|
+ long now = System.currentTimeMillis();
|
|
|
+ if (advertiserReportDailyBuffer.size() >= MAX_BATCH_SIZE ||
|
|
|
+ (now - advertiserReportDailyLastFlushTime) >= MAX_WAIT_TIME_MS && !advertiserReportDailyBuffer.isEmpty()) {
|
|
|
+ try {
|
|
|
+ log.info("Flushed advertiserReportDaily batch size: {}", advertiserReportDailyBuffer.size());
|
|
|
+ accountReportMapper.replaceBatcKuaiShouAccountReportDailySD(new ArrayList<>(advertiserReportDailyBuffer)); advertiserReportDailyBuffer.clear();
|
|
|
+ advertiserReportDailyLastFlushTime = now;
|
|
|
+ } catch (Exception e) {
|
|
|
+ log.error("Error flushing advertiserReportDaily batch", e);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ @PreDestroy
|
|
|
+ public void flushPreDestroy() {
|
|
|
+ log.info("项目重启,开始执行账户报表批处理任务....");
|
|
|
+ synchronized (advertiserReportHourBuffer) {
|
|
|
+ if (!advertiserReportHourBuffer.isEmpty()) {
|
|
|
+ try {
|
|
|
+ log.info("Flushed advertiserReportHour batch size: {}", advertiserReportHourBuffer.size());
|
|
|
+ accountReportMapper.replaceBatcKuaiShouAccountReportHourSD(new ArrayList<>(advertiserReportHourBuffer)); advertiserReportHourBuffer.clear();
|
|
|
+ advertiserReportHourBuffer.clear();
|
|
|
+ } catch (Exception e) {
|
|
|
+ log.error("Error flushing advertiserReportHour batch", e);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ synchronized (advertiserReportDailyBuffer) {
|
|
|
+ if (!advertiserReportDailyBuffer.isEmpty()) {
|
|
|
+ try {
|
|
|
+ log.info("Flushed advertiserReportDaily batch size: {}", advertiserReportDailyBuffer.size());
|
|
|
+ accountReportMapper.replaceBatcKuaiShouAccountReportDailySD(new ArrayList<>(advertiserReportDailyBuffer)); advertiserReportDailyBuffer.clear();
|
|
|
+ advertiserReportDailyBuffer.clear();
|
|
|
+ } catch (Exception e) {
|
|
|
+ log.error("Error flushing advertiserReportDaily batch", e);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
}
|