자바 다 중 스 레 드 처리 가 져 오기 데이터 분할 List 집합, 동기 화 처리 삽입 데이터

    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 list = new ArrayList();

            //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 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  list;
        public ExecupteHp (List  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);

                }
            }

        }
    }

   

}

좋은 웹페이지 즐겨찾기