| | |
| | | package com.yssh.service; |
| | | |
| | | import com.google.common.collect.Lists; |
| | | import com.yssh.dao.VocValsMapper; |
| | | import com.yssh.entity.VocVals; |
| | | import org.apache.commons.logging.Log; |
| | | import org.apache.commons.logging.LogFactory; |
| | | import org.springframework.scheduling.annotation.Async; |
| | | import org.springframework.stereotype.Service; |
| | | |
| | | import javax.annotation.Resource; |
| | | import java.util.List; |
| | | import java.util.concurrent.CountDownLatch; |
| | | |
| | | @Service |
| | | public class VocValsService implements VocValsMapper { |
| | | @Resource |
| | | VocValsMapper vocValsMapper; |
| | | |
| | | protected final Log logger = LogFactory.getLog(this.getClass()); |
| | | |
| | | @Override |
| | | public List<VocVals> selectByTime(String time, Double startVal) { |
| | |
| | | public int deleteLastYear() { |
| | | return vocValsMapper.deleteLastYear(); |
| | | } |
| | | |
| | | @Async("threadPoolTaskExecutor") |
| | | public int insertVocVals(List<VocVals> list) throws InterruptedException { |
| | | List<List<VocVals>> lists = Lists.partition(list, IAsyncService.BATCH_INSERT_500); |
| | | |
| | | CountDownLatch countDownLatch = new CountDownLatch(list.size()); |
| | | for (List<VocVals> corpList : lists) { |
| | | executeAsync(corpList, countDownLatch); |
| | | } |
| | | |
| | | countDownLatch.await(); |
| | | logger.info("------ VOC.csv," + lists.size() + " 条数据已入库 ------"); |
| | | |
| | | return list.size(); |
| | | } |
| | | |
| | | private void executeAsync(List<VocVals> corpList, CountDownLatch countDownLatch) { |
| | | try { |
| | | // 异步线程要做的事情 |
| | | vocValsMapper.inserts(corpList); |
| | | } finally { |
| | | // 很关键, 无论上面程序是否异常必须执行countDown,否则await无法释放 |
| | | countDownLatch.countDown(); |
| | | } |
| | | } |
| | | } |