Fork/Join框架解决并发问题实例

// 拼接报文字段
    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;
        }
    }
}

你可能感兴趣的:(Fork/Join框架解决并发问题实例)