业务:定时从MySQL数据库中读取数据保存到ES中,但MySQL数据量很大,单sql查询非常耗时,同时用List接收处理也有问题。刚开始想到直接分页处理,但发现处理效率很慢。最终想到使用多线程+分页形式处理,效率真的提高了很多。
@Component
@Slf4j
public class TestHandler {
private static ThreadFactory namedFactory = new ThreadFactoryBuilder().setNameFormat("test-thread").build();
ThreadPoolExecutor threadPool = new ThreadPoolExecutor(10, 20,
30, TimeUnit.MINUTES, new LinkedBlockingQueue<>(),namedFactory);
}
int count = testService.count();
int size = 1000;
long cycles = count / size;
for (int i = 0; i <= cycles+1; i++) {
long offset = i * size;
threadPool.execute(new Runnable() {
@Override
public void run() {
List<Test> list= testService.pageList(offset,size);
try {
esProcess(list);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
|