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<Map> fileList = (List) inMBean.getBody().get("FILE_CONTENT");
        if (objTemMap != null && !objTemMap.isEmpty()) {
            Iterator<String> 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<List> 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<String> 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<List> {

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

    private long beginPos;
    private long endPos;
    private String key;
    private List<Map> fileList;

    public FileDataTask(long beginPos, long endPos, String key, List<Map> 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;
        }
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值