多线程写法:
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)); } } }