Fork/Joinフレームワークによる同時問題解決の例

5372 ワード

//  
    public void doNotifyFile(MBean inMBean) throws Exception {

        FtpFile ftpConfigInfo = TariffConst.FTP_DOWN;
        long totalSize = 0;
        String rowscfg = FtpFileDom.getPropertie("TARIFFCONST_DOWN_FTP","ROWS");
        long rows = Integer.valueOf(rowscfg);
        // 
        Date date = new Date();
        SimpleDateFormat df = new SimpleDateFormat("yyyyMMddHHmmss");
        String day = df.format(date);
        String fileName = "Achieved_Product_" + day + "_0000.xml";
        String pathName = ftpConfigInfo.getTemPath() + fileName;
        Map objTemMap = (Map) inMBean.getBody().get("OBJ_TEM");
        List fileList = (List) inMBean.getBody().get("FILE_CONTENT");
        if (objTemMap != null && !objTemMap.isEmpty()) {
            Iterator s = objTemMap.keySet().iterator();
            // 
            List eleList = new ArrayList();
            while (s.hasNext()) {
                String key = s.next();
                Map outMap = (Map) objTemMap.get(key);
                List chargeList = new ArrayList();
                Map eleMap = new HashMap();

                ForkJoinPool pool = new ForkJoinPool();

                Future future = pool.submit(new FileDataTask(0, fileList.size(), key, fileList));

                chargeList.addAll(future.get());

                pool.shutdown();

                eleMap.put("ChargeInfo", chargeList);
                eleMap.put("ProdCode", outMap.get("prodCode"));
                eleMap.put("ProdName", outMap.get("prodName"));
                eleMap.put("ProdDesc", outMap.get("prodDesc"));
                eleMap.put("EfftDate", outMap.get("efftDate") + " 00:00:00");
                eleMap.put("InvalidDate", outMap.get("invalidDate") + " 00:00:00");
                eleList.add(eleMap);
                long size = eleList.size();
                if (size >= rows) {
                    //   
                    XmlFileUtil.writer(pathName,eleList,ftpConfigInfo.getTemPath());
                    eleList.clear();
                    totalSize += size;
                    log.info("@@@@ totalSize=" + totalSize);
                }
            }

            // 
            if(eleList!=null && eleList.size()>0){
                XmlFileUtil.writer(pathName,eleList,ftpConfigInfo.getTemPath());
                eleList.clear();
            }

            //  temp bak recvl 
            List paths = new ArrayList<>();
            paths.add(ftpConfigInfo.getRecvlPath());
            paths.add(ftpConfigInfo.getBakPath());
            XmlFileUtil.getAllFilesByPath(ftpConfigInfo.getTemPath(),paths);
            // FTP 
            FtpUtils ftp = new FtpUtils(ftpConfigInfo.getIp(),ftpConfigInfo.getPort(),ftpConfigInfo.getUser(),ftpConfigInfo.getPwd());
            ftp.connectServer();
            ftp.uploadFile(FtpFileDom.getPropertie("TARIFFCONST_DOWN_FTP","ACHIEVED_UPLOAD_PATH"),ftpConfigInfo.getRecvlPath(),fileName);
            ftp.closeServer();
        }
    }

ファイル処理コンカレントコンピテンシーコンポーネントコードは次のとおりです.
public class FileDataTask extends RecursiveTask {

    private static Logger log = LoggerFactory.getLogger(FileDataTask.class);

    private long beginPos;
    private long endPos;
    private String key;
    private List fileList;

    public FileDataTask(long beginPos, long endPos, String key, List fileList) {
        this.beginPos = beginPos;
        this.endPos = endPos;
        this.key = key;
        this.fileList = fileList;
    }

    @Override
    protected List compute() {
        List out = new ArrayList();

        if (endPos - beginPos < Constants.THRESHOLD) {

            log.debug("begin computer ! @@@@@@@   FileDataTask   @@@@@@@@");

            for (int i = Integer.valueOf(beginPos + ""); i < endPos; i++) {

                Map tmp = fileList.get(i);

                if (key.equals(tmp.get("prodCode"))) {
                	//  
                    Map temMap = new HashMap();
                    temMap.put("ChargeCode", tmp.get("chargeCode"));
                    temMap.put("ChargeName", tmp.get("chargeName"));
                    temMap.put("ChargeDesc", tmp.get("chargeDesc"));
                    temMap.put("ChargeDetail", tmp.get("chargeDetail"));
                    temMap.put("Price", tmp.get("price"));
                    temMap.put("Term", tmp.get("term"));
                    temMap.put("TermUnit", tmp.get("termUnit"));
                    out.add(temMap);
                }

            }

            return out;
        } else {

            long middle = (beginPos + endPos) / 2;

            FileDataTask left = new FileDataTask(beginPos, middle, key, fileList);
            FileDataTask right = new FileDataTask(middle, endPos, key, fileList);

            left.fork();
            right.fork();

            out.addAll(left.join());
            out.addAll(right.join());

            return out;
        }
    }
}