Java多线程处理数据同步

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);
            }
        }
    }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值