多线程处理耗时的业务逻辑

上传sftp大量数据,造成页面假死,用户体验不好

使用多线程,页面显示成功,后台让他慢慢上传.有问题留言,
//定义线程池
public static ExecutorService tmasCsggpool = Executors.newFixedThreadPool(1);//线程

//创建线程,并发送sftp
            Map<Integer, Future> map = new HashMap<Integer, Future>();
            //new自己写的线程,
            UploadFileToTBSftpThread pw = new UploadFileToTBSftpThread(iouService,attachmentService,synSingleFileDownloadWorker); 

            for (int i = 0; i < contractIdStrList.size(); i++) {
            //需要的参数
                pw.setContractIds(contractIdStrList);
                Future f =(Future)tmasCsggpool.submit(pw);
                map.put(i, f);
            }

线程类,耗时的业务逻辑抽取放到这里.

package com.qb.modules.organtrans.interactive;

import java.io.IOException;
import java.io.OutputStream;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Properties;

import javax.transaction.SystemException;

import org.apache.commons.io.IOUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.io.ClassPathResource;

import com.jcraft.jsch.ChannelSftp;
import com.jcraft.jsch.SftpATTRS;
import com.jcraft.jsch.SftpException;
import com.qb.modules.organtrans.attachment.dto.AttachmentDTO;
import com.qb.modules.organtrans.attachment.service.AttachmentService;
import com.qb.modules.organtrans.contractinfo.dto.ContractInfoDTO;
import com.qb.modules.organtrans.contractinfo.service.ContractInfoService;
import com.qb.modules.organtrans.util.FileUtil;
import com.qb.modules.organtrans.util.SFTPUtils;
import com.qb.modules.util.CommonUtil;
import com.qb.platform.core.fastdfs.task.SynSingleFileDownloadWorker;

public class UploadFileToTBSftpThread implements Runnable {

    ContractInfoService         contractInfoService;
    AttachmentService           attachmentService;
    SynSingleFileDownloadWorker synSingleFileDownloadWorker;


    private Log logger = LogFactory.getLog(CallTBInterfaceService.class);

    public List getContractIds() {
        return contractIds;

    public void setContractIds(List string) {
        this.contractIds = string;
    }

    public UploadFileToTBSftpThread(ContractInfoService contractInfoService, AttachmentService attachmentService,
            SynSingleFileDownloadWorker synSingleFileDownloadWorker) {
        super();
        this.contractInfoService = contractInfoService;
        this.attachmentService = attachmentService;
        this.synSingleFileDownloadWorker = synSingleFileDownloadWorker;
    }

    @Override
    public void run() {
        try {
            uploadFileToTBSftp(contractIds);
        } catch (Exception e) {
            e.printStackTrace();
             logger.error("uploadFileToTBSftp error", e);
            }
        }


    private  void uploadFileToTBSftp(List contractIds) throws Exception{
        long start = System.currentTimeMillis();

        Date date = new Date();
        String sftpPath  = CommonUtil.getPropertiesValue("sftp.properties", "tengbang.upload")+"/"+new SimpleDateFormat("yyyyMMdd").format(date);
        SFTPUtils sftp1 = getSftp();
        ChannelSftp sftp = sftp1.getSFTPClient();
        createDir(sftpPath, sftp);//创建路径
        sftp.cd(sftpPath);//进入路径
        OutputStream outstream = null;
        List fileCodes = new ArrayList<>();
        fileCodes.add("QT15");
        try {
            for( String  contractId : contractIds ){
                ContractInfoDTO contractInfoDTO = contractInfoService.queryContractInfoByPrimaryKey(contractId);
                String filename = new String(contractInfoDTO.getCertificateCode()+".pdf");
                outstream = sftp.put(filename);
                List  attachmentDTOs = attachmentService.searchAttachemnt(contractInfoDTO.getId().toString(), FileUtil.TYPE_CONTRACT_FILE, fileCodes);
                IOUtils.write(synSingleFileDownloadWorker.downloadSeaweed(attachmentDTOs.get(0).getFilePath()), outstream );
                outstream.flush();
            }
        } catch (SftpException e) {
            e.printStackTrace();
        }finally {
            outstream.flush();
            outstream.close();
            sftp.disconnect();
        }
        long end = System.currentTimeMillis();
        long time = end-start;
        System.out.println("77777777777777777777777777777777777777777777777777");
        System.out.println("77777777777777777777777777777777777777777777777777");
        System.out.println("77777777777777777777777777777777777777777777777777");
        System.out.println(time);
}
    public void createDir(String rpath, ChannelSftp sftp) throws SystemException {
        try {
            if (isDirExist(rpath,sftp)) {
                sftp.cd(rpath);
            }else{
                String pathArry[] = rpath.split("/");

                for (String path : pathArry) {
                    if (path.equals("")) {
                        continue;
                    }
                    if (isDirExist(path,sftp)) {
                        sftp.cd(path);
                    } else {
                        // 建立目录
                        sftp.mkdir(path);
                        // 进入并设置为当前目录
                        sftp.cd(path);
                    }
                }
            }
        } catch (SftpException e) {
            throw new SystemException("创建路径错误:" + rpath);
        }
    }
    //判断文件夹时候存在
    public boolean isDirExist(String rpath, ChannelSftp sftp) {  
        boolean isDirExistFlag = false;
        try {
            SftpATTRS sftpATTRS = sftp.lstat(rpath);
            isDirExistFlag = true;
            return sftpATTRS.isDir();
        } catch (Exception e) {
            if (e.getMessage().toLowerCase().equals("no such file")) {
                isDirExistFlag = false;
            }
        }
        return isDirExistFlag;
     }

    public static SFTPUtils getSftp() {
        Properties prop = new Properties();
        ClassPathResource cp = new ClassPathResource("sftp.properties");
        try {
            prop.load(cp.getInputStream());
        } catch (IOException e) {
            e.printStackTrace();
        }
        String host = prop.getProperty("tengbang.host").trim();
        int port = Integer.parseInt(prop.getProperty("tengbang.port").trim());
        String username = prop.getProperty("tengbang.username").trim();
        String password = prop.getProperty("tengbang.password").trim();
        return new SFTPUtils(host, port, username, password);
    }



}






你可能感兴趣的:(Java)