1.创建多线程配置 AsyncTaskConfig
blog.csdn.net/qqzhengwei/…
@Configuration
@EnableAsync
public class AsyncTaskConfig implements AsyncConfigurer {
@Override
@Bean
public Executor getAsyncExecutor() {
ThreadPoolTaskExecutor threadPool = new ThreadPoolTaskExecutor();
threadPool.setCorePoolSize(10);
threadPool.setMaxPoolSize(15);
threadPool.setQueueCapacity(20);
threadPool.setWaitForTasksToCompleteOnShutdown(true);
threadPool.setAwaitTerminationSeconds(60);
threadPool.setThreadNamePrefix("mds-async-task-");
threadPool.initialize();
return threadPool;
}
@Override
public AsyncUncaughtExceptionHandler getAsyncUncaughtExceptionHandler() {
return null;
}
}
2.创建异步多线程接口和实现类
public interface IZhiyiAsyncService {
Future<Integer> saveItemPropertyAsync(int limit,int offset);
}
@Async
@Override
public Future<Integer> saveItemPropertyAsync(int limit,int offset) {
log.info("limit={} offset={}", limit, offset);
int exeCount = 0;
List<ZhiyiShop> shopList = this.findZhiyiShopPage(limit, offset);
for (ZhiyiShop shop : shopList) {
int pageNo = 1;
int pageSize = 200;
log.info("开始 5、获取商品昨日最新属性 shopId={},每页数量={}", shop.getShopId(), pageSize);
while (pageNo > 0) {
String reqUrl = String.format("%s/item-property?account=%s&appcode=%s&shopId=%s&pageNo=%d&pageSize=%d", zhiyiUrl, zhiyiAccount, zhiyiApppcode, shop.getShopId(), pageNo, pageSize);
log.info(reqUrl);
ParameterizedTypeReference<ZhiyiRsp<ZhiyiPageDto<List<ZhiyiItemProperty>>>> reference = new ParameterizedTypeReference<ZhiyiRsp<ZhiyiPageDto<List<ZhiyiItemProperty>>>>() {
};
ResponseEntity<ZhiyiRsp<ZhiyiPageDto<List<ZhiyiItemProperty>>>> entity = restTemplate.exchange(reqUrl, HttpMethod.GET, null, reference);
ZhiyiRsp<ZhiyiPageDto<List<ZhiyiItemProperty>>> rsp = entity.getBody();
if (rsp.getResult() == null || CollectionUtils.isEmpty(rsp.getResult().getResultList())) {
pageNo = 0;
} else {
pageNo++;
List<Object> ids = new ArrayList<>();
for (ZhiyiItemProperty item : rsp.getResult().getResultList()) {
ids.add(item.getId());
}
zhiyiMapper.commonDelete("zhiyi_item_property", "id", ids);
exeCount += zhiyiItemPropertyMapper.insertBatch(rsp.getResult().getResultList());
}
}
log.info("结束 5、获取商品昨日最新属性 shopId={},每页量={},成功行数={}",shop.getShopId(),pageSize, exeCount);
}
return new AsyncResult<>(exeCount);
}
3.调用多线程
@Slf4j
@Service
public class ZhiyiServiceImpl {
@SneakyThrows
@Override
public Integer saveItemProperty() {
int exeCount=0;
int totalCount=zhiyiMapper.findCount("zhiyi_shop");
int totalPage=5;
int limit = (int) Math.ceil(totalCount / Double.valueOf(totalPage));
List<Future> futureList=new ArrayList<>();
for(int i=0;i<totalPage;i++) {
futureList.add(zhiyiAsyncService.saveItemPropertyAsync(limit,Math.min(i*limit,totalCount) ));
}
for (Future<Integer> future : futureList) {
while (true) {
if (future.isDone() && !future.isCancelled()) {
exeCount+=future.get();
log.info("5、获取商品昨日最新属性 完成线程任务 ,总行数={}",future.get() );
break;
} else {
Thread.sleep(1);
}
}
}
return exeCount;
}
}