java多线程处理导入数据拆分List集合,同步处理插入数据

4 篇文章 0 订阅
获取导入excel文件中的数据,存入list中进行多线程处理,设定每个线程处理多少条数据,进行设定值动态启动线程数量数据,根据线程处理
数量拆分动态list数据进行相关业务操作
package com.ig.bi.game.controller;

import org.apache.log4j.Logger;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;
import javax.servlet.http.HttpServletRequest;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.ScheduledThreadPoolExecutor;
import java.util.concurrent.ThreadPoolExecutor;

/**
 * Created by admin 
 */
@RestController
@RequestMapping("/importData")
public class HpImportController extends BaseController{
    private static final Logger logger = Logger.getLogger(HpImportController.class);

    @Reference(interfaceClass = SidCustManageService.class,version = Constants.SERVICE_VERSION)
    private SidCustManageService sidCustManageService;
 


    @RequestMapping(value = "importDate")
    @ResponseBody
    public Response getOrdertDate(HttpServletRequest request,MultipartFile importData){


        //创建Excel工作薄
        try {
            HSSFWorkbook hwb = new HSSFWorkbook(importData.getInputStream());
            //得到第一个工作表
            HSSFSheet sheet = hwb.getSheetAt(0);
            HSSFRow row = null;

            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
            List<SidCustPersonContactInfo> list = new ArrayList<SidCustPersonContactInfo>();

            //遍历该表格中所有的工作表,i表示工作表的数量 getNumberOfSheets表示工作表的总数
            for(int i = 0; i < hwb.getNumberOfSheets(); i++) {
                sheet = hwb.getSheetAt(i);

                //遍历该行所有的行,j表示行数 getPhysicalNumberOfRows行的总数
                for(int j = 1; j < sheet.getPhysicalNumberOfRows(); j++) {
                    row = sheet.getRow(j);
                    String loginName = ExcelUtils.getStringValue(row, 0);
                    String agentName = ExcelUtils.getStringValue(row, 1);
                    String pwd = "123123";
                    String  amount = ExcelUtils.getStringValue(row, 3);
                    String cust_lev = ExcelUtils.getStringValue(row,4);
                    String name = ExcelUtils.getStringValue(row, 5);
                    String tel = ExcelUtils.getStringValue(row, 6);
                    String qq = ExcelUtils.getStringValue(row, 7);
                    String birday = ExcelUtils.getStringValue(row, 8);
                    String email = ExcelUtils.getStringValue(row, 9);
                    String createTp = ExcelUtils.getStringValue(row, 10);
                    String bankAccountName = ExcelUtils.getStringValue(row, 11);
                    String bankNo = ExcelUtils.getStringValue(row, 12);
                    String bankName = ExcelUtils.getStringValue(row, 13);
                    String bankType = ExcelUtils.getStringValue(row, 14);


                    SidCustPersonContactInfo custCpci = new SidCustPersonContactInfo();
                    custCpci.setCustCode(loginName);
                    custCpci.setAmount(Double.valueOf(amount));
                    custCpci.setPasswd(pwd);
                    custCpci.setBankName(bankName);
                    custCpci.setBankNo(bankNo);
                    custCpci.setBankAccountName(bankAccountName);
                    if(!birday.equals("")){
                        custCpci.setBirthDate(sdf.parse(birday));
                    }
                    custCpci.setFirstName(name);
                    custCpci.setNickName(name);
                    custCpci.setQq(qq);
                    custCpci.setBankType(bankType);
                    custCpci.setEmailAddress(email);
                    custCpci.setMobileNumber(tel);
                    custCpci.setHallId(Long.valueOf(5));
                    list.add(custCpci);

                }
            }


            int count = 50;
            int listSize = list.size();
            int RunSize = (listSize / count)+1;

            ThreadPoolExecutor executor = new ScheduledThreadPoolExecutor(RunSize);
            CountDownLatch countDownLatch = new CountDownLatch(RunSize);
            List<SidCustPersonContactInfo> newList = null ;
            for (int i = 0; i < RunSize; i++) {
                if((i+1)==RunSize){
                    int startIndex = (i*count);;
                    int endIndex = list.size();
                    newList =list.subList(startIndex,endIndex);
                }else{
                    int startIndex = i*count;;
                    int endIndex = (i+1)*count;
                    newList =list.subList(startIndex,endIndex);
                }

                ExecupteHp hpRunnable = new ExecupteHp(newList);
                executor.execute(hpRunnable);
            }

            countDownLatch.await();
            executor.shutdown();


        }catch (Exception e){
        logger.info("导入数据开始.");
        e.printStackTrace();
    }
        return Response.ok(GlobalEnum.GLOBAL_ENUM_SUCCESS_CODE.getValue() , SidCustEnum.SID_CRST_ERROR_SID_SUCCESS_07.getValue());

    }



    class ExecupteHp implements Runnable{
        private List <SidCustPersonContactInfo> list;
        public ExecupteHp (List <SidCustPersonContactInfo> list){
            this.list  = list ;
        }

        @Override
        public void run() {
            if(null!=list){
                for (SidCustPersonContactInfo scpc : list){
                       System.out.print("更新会员联系方式" +scpc.getFirstName() + "---"+scpc.getCustCode() );
                    SidCustContactInfo custContactInfo  = new SidCustContactInfo();
                    custContactInfo.setCustCode(scpc.getCustCode());
                    custContactInfo.setEmailAddress(scpc.getEmailAddress());
                    custContactInfo.setMobileNumber(scpc.getMobileNumber());
                    custContactInfo.setFirstName(scpc.getFirstName());
                    custContactInfo.setNicName(scpc.getNickName());
                    custContactInfo.setQq(scpc.getQq());
                    custContactInfoService.updateByContactByCustCode(custContactInfo);

                }
            }

        }
    }

   

}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值