Java多线程处理数据同步
1.代码
@Test
public void injectDataByThread() throws IOException {
//创建线程池
ThreadPoolExecutor threadPool = new ThreadPoolExecutor(5, 10, 60,
TimeUnit.SECONDS, new ArrayBlockingQueue<>(3),
new ThreadPoolExecutor.DiscardOldestPolicy());
//查询总条数
Integer total = hotelService.count();
//计算总页数
int pages = (int) Math.ceil((double) total / 20);
//分页导入数据
for (int page = 1; page <= pages; page++) {
Page<Hotel> objectPage = new Page<>(page, 20);
hotelService.page(objectPage);
if (objectPage.getRecords() != null && objectPage.getRecords().size() > 0) {
//
threadPool.execute(new Runnable() {
@Override
public void run() { //加入到线程池任务
try {
//创建request对象
BulkRequest bulkRequest = new BulkRequest();
//遍历每条数据转换为es实体
for (Hotel hotel : objectPage.getRecords()) {
// 2.1.转为HotelDoc
HotelDoc hotelDoc = new HotelDoc(hotel);
// 2.2.转json
String json = JSON.toJSONString(hotelDoc);
// 2.3.添加请求
bulkRequest.add(new IndexRequest("hotel").id(hotel.getId().toString()).source(json, XContentType.JSON));
}
client.bulk(bulkRequest, RequestOptions.DEFAULT);
log.info("【es】thread:{},msg:本次同步 {} 条数据",
Thread.currentThread().getName(), objectPage.getRecords().size());
} catch (IOException e) {
e.printStackTrace();
}
}
});
}
try {
//延迟3秒等elasticsearch完成写入数据
Thread.sleep(3000L);
} catch (InterruptedException e) {
throw new RuntimeException(e);
}
}
}