All Downloads are FREE. Search and download functionalities are using the official Maven repository.

cn.schoolwow.download.module.task.flow.PostDownloadFlow Maven / Gradle / Ivy

The newest version!
package cn.schoolwow.download.module.task.flow;

import cn.schoolwow.download.module.pool.domain.PoolContext;
import cn.schoolwow.quickflow.domain.FlowContext;
import cn.schoolwow.quickflow.flow.BusinessFlow;

import java.io.File;
import java.nio.file.Path;
import java.util.List;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.Future;
import java.util.concurrent.TimeUnit;
import java.util.function.Consumer;

public class PostDownloadFlow implements BusinessFlow {
    @Override
    public void executeBusinessFlow(FlowContext flowContext) throws Exception {
        PoolContext poolContext = flowContext.checkInstanceData(PoolContext.class);
        CountDownLatch countDownLatch = flowContext.getInstanceData(CountDownLatch.class);
        Consumer downloadFinished = (Consumer) flowContext.checkData("downloadFinished");
        List downloadProgressIdList = (List) flowContext.checkData("downloadProgressIdList");

        //专门的后处理线程来处理
        poolContext.postDownloadTaskThreadPoolExecutor.execute(()->{
            try {
                if(!countDownLatch.await(2, TimeUnit.HOURS)){
                    //TODO 后边用流程串起来
//                    logger.warn("文件下载时间超过阈值,停止处理!");
                    return;
                }
                Path[] pathList = new Path[downloadProgressIdList.size()];
                for(int i=0;i fileFuture = poolContext.downloadTaskFutureMap.get(downloadProgressIdList.get(i));
                    pathList[i] = fileFuture.get().toPath();
                }
                downloadFinished.accept(pathList);
            } catch (InterruptedException | ExecutionException e) {
//                logger.debug("后处理线程发生线程中断,停止处理!");
            }finally {
                synchronized (poolContext.downloadTaskFutureMap){
                    for(Long downloadProgressId:downloadProgressIdList){
                        poolContext.downloadTaskFutureMap.remove(downloadProgressId);
                    }
                }
            }
        });
    }

    @Override
    public String name() {
        return "下载完成后处理";
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy