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