2020-09-16

多线程写法:

public class SeeInfoService {

    private static final String SEEINFO_LAST_TIME_KEY = "seeinfo_last_time";
    private static final DateTimeFormatter DATE_FORMATTER = DateTimeFormatter.ofPattern("yyyy-MM-dd");
    private static final DateTimeFormatter DATE_TIME_FORMATTER = DateTimeFormatter.ofPattern("yyyy-MM-dd HH");


    private static final ExecutorService THREAD_POOL = new ThreadPoolExecutor(20, 20, 0L, TimeUnit.MILLISECONDS,
            new LinkedBlockingQueue<>(300000), new ThreadFactoryBuilder().setNameFormat("SeeInfoService-pool-%d").build(), new ThreadPoolExecutor.AbortPolicy());


    @Resource(name = "redisTemplate")
    private ValueOperations<String, String> valueOps;
    @Autowired
    private YfbJlLogMapper yfbJlLogMapper;
    @Autowired
    private QwsjJlLogMapper qwsjJlLogMapper;
    @Autowired
    private MongoTemplate mongoTemplate;


    public void MoveSeeInfoIntoMongo() {
        LocalDate today = LocalDate.now();
        LocalDate minusMonths = today.minusMonths(1);

        while ((StringUtils.isBlank(valueOps.get(SEEINFO_LAST_TIME_KEY)) ? LocalDate.of(2016, 7, 20) :
                LocalDate.parse(valueOps.get(SEEINFO_LAST_TIME_KEY), DATE_FORMATTER)).compareTo(today) < 0) {
            LocalDate lastTime = (StringUtils.isBlank(valueOps.get(SEEINFO_LAST_TIME_KEY)) ? LocalDate.of(2016, 7, 20) :
                    LocalDate.parse(valueOps.get(SEEINFO_LAST_TIME_KEY), DATE_FORMATTER));
            LocalDate moveEnd = lastTime.plusDays(1);
            String sstime = lastTime.format(DATE_FORMATTER);
            long lstime = lastTime.atStartOfDay().toEpochSecond(ZoneOffset.ofHours(8));
            long letime = moveEnd.atStartOfDay().toEpochSecond(ZoneOffset.ofHours(8));
            List<JlLogPO> jlLogPOS = yfbJlLogMapper.listJlLogPOWithTime(lstime, letime);
            if (CollectionUtils.isNotEmpty(jlLogPOS)) {
                List<Future<Boolean>> list = new ArrayList<>();
                for (JlLogPO jlLogPO : jlLogPOS) {
                    Future<Boolean> submit = THREAD_POOL.submit(() -> {
                        Integer channel = jlLogPO.getChannel();
                        String sourceType = channel == null ? "yfb" : (channel == 0 ? "yfb" : "app");
                        SeeInfoAllDocument seeInfoAllDocument = new SeeInfoAllDocument(SnowflakeIdWorker.instance.nextId(), jlLogPO.getMemberId(), sourceType, sstime, jlLogPO.getSeeCount(), jlLogPO.getSeeIds(), lstime);
                        mongoTemplate.save(seeInfoAllDocument);
                        return true;
                    });
                    list.add(submit);
                }
                for (Future<Boolean> future : list) {
                    try {
                        future.get();
                    } catch (InterruptedException | ExecutionException e) {
                        e.printStackTrace();
                    }
                }
            }

            List<JlLogPO> qwsjLogPOS = qwsjJlLogMapper.listJlLogPOWithTime(lstime, letime);
            if (CollectionUtils.isNotEmpty(qwsjLogPOS)) {
                List<Future<Boolean>> list = new ArrayList<>();
                for (JlLogPO jlLogPO : jlLogPOS) {
                    Future<Boolean> submit = THREAD_POOL.submit(() -> {
                        String sourceType = "qwsj";
                        SeeInfoAllDocument seeInfoAllDocument = new SeeInfoAllDocument(SnowflakeIdWorker.instance.nextId(), jlLogPO.getMemberId(), sourceType, sstime, jlLogPO.getSeeCount(), jlLogPO.getSeeIds(), lstime);
                        mongoTemplate.save(seeInfoAllDocument);
                        return true;
                    });
                    list.add(submit);
                }
                for (Future<Boolean> future : list) {
                    try {
                        future.get();
                    } catch (InterruptedException | ExecutionException e) {
                        e.printStackTrace();
                    }
                }
            }

            if (lastTime.compareTo(minusMonths) > 0) {
                // 按小时计算单天数据
                LocalDateTime slocalTime = lastTime.atStartOfDay();
                LocalDateTime elocalTime = moveEnd.atStartOfDay();
                while (slocalTime.compareTo(elocalTime) < 0) {
                    LocalDateTime eelocalTime = slocalTime.plusHours(1);
                    long lslocalTime = slocalTime.toEpochSecond(ZoneOffset.ofHours(8));
                    long leelocalTime = eelocalTime.toEpochSecond(ZoneOffset.ofHours(8));
                    String sslocalTime = slocalTime.format(DATE_TIME_FORMATTER);

                    List<JlLogPO> yfbHourjlLogPOS = yfbJlLogMapper.listJlLogPOWithTime(lslocalTime, leelocalTime);
                    if (CollectionUtils.isNotEmpty(yfbHourjlLogPOS)) {
                        List<Future<Boolean>> list = new ArrayList<>();
                        for (JlLogPO jlLogPO : yfbHourjlLogPOS) {
                            Future<Boolean> submit = THREAD_POOL.submit(() -> {
                                Integer channel = jlLogPO.getChannel();
                                String sourceType = channel == null ? "yfb" : (channel == 0 ? "yfb" : "app");
                                SeeInfoOneMonthHourDocument seeInfoAllDocument = new SeeInfoOneMonthHourDocument(SnowflakeIdWorker.instance.nextId(), jlLogPO.getMemberId(), sourceType, sslocalTime, jlLogPO.getSeeCount(), jlLogPO.getSeeIds(), lslocalTime);
                                mongoTemplate.save(seeInfoAllDocument);
                                return true;
                            });
                            list.add(submit);
                        }
                        for (Future<Boolean> future : list) {
                            try {
                                future.get();
                            } catch (InterruptedException | ExecutionException e) {
                                e.printStackTrace();
                            }
                        }
                    }

                    List<JlLogPO> qwsjHourLogPOS = qwsjJlLogMapper.listJlLogPOWithTime(lslocalTime, leelocalTime);
                    if (CollectionUtils.isNotEmpty(qwsjHourLogPOS)) {
                        List<Future<Boolean>> list = new ArrayList<>();
                        for (JlLogPO jlLogPO : qwsjHourLogPOS) {
                            Future<Boolean> submit = THREAD_POOL.submit(() -> {
                                String sourceType = "qwsj";
                                SeeInfoOneMonthHourDocument seeInfoAllDocument = new SeeInfoOneMonthHourDocument(SnowflakeIdWorker.instance.nextId(), jlLogPO.getMemberId(), sourceType, sslocalTime, jlLogPO.getSeeCount(), jlLogPO.getSeeIds(), lslocalTime);
                                mongoTemplate.save(seeInfoAllDocument);
                                return true;
                            });
                            list.add(submit);
                        }
                        for (Future<Boolean> future : list) {
                            try {
                                future.get();
                            } catch (InterruptedException | ExecutionException e) {
                                e.printStackTrace();
                            }
                        }
                    }
                    slocalTime = eelocalTime;
                }
            }
            log.info("{},导入完毕", sstime);
            valueOps.set(SEEINFO_LAST_TIME_KEY, moveEnd.format(DATE_FORMATTER));
        }
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值