定时器处理-txt文件数据解析处理(一)

涉及代码

在这里插入图片描述

yml配置

treeGorgesBank:
  fileSend:
    # 文件地址
    dirPath: E:\tree-sourece
    # 文件备用地址(定时器处理文件则转移到这)
    backupDirPath: E:\tree-back
  # 定时器定时执行时间
  jobCron: 0/5 * * * * ?

线程池定义

  • 线程池配置
@Configuration
public class FileThreadPoolConfig {


    /**
     * 解析文件数据发送短信线程池
     */
    @Bean(name = "fileSendThreadPool")
    public ThreadPoolExecutor fileThreadPool() {
        return new ThreadPoolExecutor(5, 10, 60, TimeUnit.SECONDS,
                new LinkedBlockingQueue<>(),
                ThreadUtil.threadFactory("fileSendJobThreadPool"),
                new ThreadPoolExecutor.AbortPolicy());
    }

}
  • 线程工具类
import com.google.common.collect.Lists;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.*;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.function.BiFunction;
import java.util.function.Function;


public class ThreadUtil {

    private final static Logger logger = LoggerFactory.getLogger(ThreadUtil.class);

    public static void sleepByWait(long timeout) throws InterruptedException {
        byte[] lock = new byte[0];
        synchronized (lock){
            lock.wait(timeout);
        }
        lock = null;
    }


    public static RejectedExecutionHandler blockExecuteRejectHandle(String name){
        return new BlockExecuteRejectHandle(name);
    }

    public static ThreadFactory threadFactory(String name){
        return new TFactory(name);
    }

    static class TFactory implements ThreadFactory {

        private final ThreadGroup group;
        //编号
        private final AtomicInteger threadNumber = new AtomicInteger(1);
        //池子名称
        private final String namePrefix;

        public TFactory(String name) {
            SecurityManager s = System.getSecurityManager();
            group = (s != null) ? s.getThreadGroup() :
                    Thread.currentThread().getThreadGroup();
            namePrefix = name.concat("-");
        }

        public Thread newThread(Runnable r) {
            Thread t = new Thread(group, r,
                    namePrefix + threadNumber.getAndIncrement(),
                    0);
            if (t.isDaemon())
                t.setDaemon(false);
            if (t.getPriority() != Thread.NORM_PRIORITY)
                t.setPriority(Thread.NORM_PRIORITY);
            return t;
        }
    }


    static class BlockExecuteRejectHandle implements RejectedExecutionHandler {

        final String name;

        public BlockExecuteRejectHandle(String name) {
            this.name = name.concat("RejectHandle");
        }

        @Override
        public void rejectedExecution(Runnable r, ThreadPoolExecutor e) {
            if (!e.isShutdown()) {
                try {
                    logger.warn("{} 阻塞加入 | pool:{}",name,e);
                    e.getQueue().put(r);
                } catch (Exception ex) {
                    logger.error("{} 阻塞加入异常",e,ex);
                }
            }
        }
    }

    /**
     * 每个线程任务需要处理的数据量
     * @param dataSize 数据个数
     * @param maxTaskNum 最多创建多少个线程任务
     * @return  返回<=1  就再当前线程执行    返回其他就在池子中执行
     */
    public static int avgCapacity(int dataSize, int maxTaskNum) {
        int _c = dataSize / maxTaskNum;
        if (_c == 0){
            return 0;//dataSize < maxTaskNum 就不使用线程池执行
        }
        return dataSize % maxTaskNum == 0 ? _c : _c + 1;
    }

    /**
     * 某个大集合切割为多个
     * @param ls 数据集
     * @param fun 需要并发执行的方法
     * @param executor 线程池
     * @param maxTaskNum 此集合最多拆为几个任务
     * @param <T>
     */
    public static <T> void concurrentExecuteAndBlockResult(ThreadPoolExecutor executor, int maxTaskNum,Function<List<T>, Void> fun,List<T> ls){
        if (ls.isEmpty()){
            return;
        }
        int avgCapacity = ThreadUtil.avgCapacity(ls.size(), maxTaskNum);
        if (avgCapacity <= 1){
            fun.apply(ls);
        }else {
            List<List<T>> lists = Lists.partition(ls, avgCapacity);
            CompletableFuture[] all = new CompletableFuture[lists.size()];
            for (int i = 0; i < lists.size(); i++) {
                List<T> tmp = lists.get(i);
                if (tmp.isEmpty()){
                    continue;
                }
                all[i] = CompletableFuture.runAsync(() -> fun.apply(tmp), executor);
            }
            CompletableFuture.allOf(all).join();
        }
    }

    /**
     * 某个大集合切割为多个
     * @param ls 数据集
     * @param fun 需要并发执行的方法
     * @param executor 线程池
     * @param oneTaskDataSize 一个任务多少条数据
     * @param <T>
     */
    public static <T> void concurrentExecuteAndBlockResultVo(ThreadPoolExecutor executor, int oneTaskDataSize,Function<List<T>, Void> fun,List<T> ls){
        if (ls.isEmpty()){
            return;
        }
        if (ls.size() <= oneTaskDataSize){
            fun.apply(ls);
        }else {
            List<List<T>> lists = Lists.partition(ls, oneTaskDataSize);
            CompletableFuture[] all = new CompletableFuture[lists.size()];
            for (int i = 0; i < lists.size(); i++) {
                List<T> tmp = lists.get(i);
                if (tmp.isEmpty()){
                    continue;
                }
                all[i] = CompletableFuture.runAsync(() -> fun.apply(tmp), executor);
            }
            CompletableFuture.allOf(all).join();
        }
    }


    /**
     * 某个大集合切割为多个
     * @param ls 数据集
     * @param fun 需要并发执行的方法
     * @param executor 线程池
     * @param oneTaskDataSize 一个任务多少条数据
     * @param <T>
     */
    public static <T> void concurrentExecuteAndBlockResultVoForAbortPolicyReject(ThreadPoolExecutor executor, int oneTaskDataSize, Function<List<T>, Void> fun, List<T> ls){
        if (ls.isEmpty()){
            return;
        }
        if (ls.size() <= oneTaskDataSize){
            fun.apply(ls);
        }else {
            List<List<T>> lists = Lists.partition(ls, oneTaskDataSize);
            CompletableFuture[] all = new CompletableFuture[lists.size()];
            for (int i = 0; i < lists.size(); i++) {
                List<T> tmp = lists.get(i);
                if (tmp.isEmpty()){
                    continue;
                }
                int reNum = 0;
                while (all[i] == null){
                    try {
                        all[i] = CompletableFuture.runAsync(() -> fun.apply(tmp), executor);
                    } catch (RejectedExecutionException e) {
                        if (reNum == 0){
                            logger.warn("线程池处理任务繁忙:{}",e.getMessage());
                        }
                        reNum++;
                        try {Thread.sleep(3);} catch (Exception e1) {}
                    }catch (Exception e){
                        logger.error("线程池处理任务异常",e);
                        break;
                    }
                }

                if (reNum>0){
                    logger.warn("线程池处理任务繁忙 重试次数:{}",reNum);
                }
            }
            CompletableFuture.allOf(all).join();
        }
    }

    /**
     * 某个大集合切割为多个
     * @param ls 数据集
     * @param fun 需要并发执行的方法
     * @param executor 线程池
     * @param maxTaskNum 此集合最多拆为几个任务
     * @param <T>
     */
    public static <T> void concurrentExecuteAndBlockResult(ThreadPoolExecutor executor, int maxTaskNum,BiFunction<List<T>, Object, Void> fun, List<T> ls,Object p0){
        if (ls.isEmpty()){
            return;
        }
        int avgCapacity = ThreadUtil.avgCapacity(ls.size(), maxTaskNum);
        if (avgCapacity <= 1){
            fun.apply(ls,p0);
        }else {
            List<List<T>> lists = Lists.partition(ls, avgCapacity);
            CompletableFuture[] all = new CompletableFuture[lists.size()];
            for (int i = 0; i < lists.size(); i++) {
                List<T> tmp = lists.get(i);
                if (tmp.isEmpty()){
                    continue;
                }
                all[i] = CompletableFuture.runAsync(() -> fun.apply(tmp,p0), executor);
            }
            CompletableFuture.allOf(all).join();
        }
    }

    /**
     * 某个大集合切割为多个
     * @param ls 数据集
     * @param fun 需要并发执行的方法
     * @param executor 线程池
     * @param maxTaskNum 此集合最多拆为几个任务
     * @param <T>
     */
    public static <T> void concurrentExecuteAndBlockResult(ThreadPoolExecutor executor, int maxTaskNum,BiFunction<List<T>, Object[], Void> fun, List<T> ls,Object... oArr){
        if (ls.isEmpty()){
            return;
        }
        int avgCapacity = ThreadUtil.avgCapacity(ls.size(), maxTaskNum);
        if (avgCapacity <= 1){
            fun.apply(ls,oArr);
        }else {
            List<List<T>> lists = Lists.partition(ls, avgCapacity);
            CompletableFuture[] all = new CompletableFuture[lists.size()];
            for (int i = 0; i < lists.size(); i++) {
                List<T> tmp = lists.get(i);
                if (tmp.isEmpty()){
                    continue;
                }
                all[i] = CompletableFuture.runAsync(() -> fun.apply(tmp,oArr), executor);
            }
            CompletableFuture.allOf(all).join();
        }
    }



    public static <T,R> List<R> exec(ThreadPoolExecutor executor, int maxTaskNum,
                                     Function<List<T>, List<R>> fun,
                                     List<T> dataLs){
        if (dataLs.isEmpty()){
            return new ArrayList<>();
        }
        int avgCapacity = avgCapacity(dataLs.size(), maxTaskNum);
        if (avgCapacity <= 1){
            return fun.apply(dataLs);
        }else {
            List<R> ret = new CopyOnWriteArrayList<>();
            List<List<T>> lists = Lists.partition(dataLs, avgCapacity);
            CompletableFuture<? extends List<? extends R>>[] all = new CompletableFuture[lists.size()];
            for (int i = 0; i < lists.size(); i++) {
                List<T> tmp = lists.get(i);
                if (tmp.isEmpty()){
                    continue;
                }
                all[i] = CompletableFuture.supplyAsync(() -> fun.apply(tmp), executor).whenCompleteAsync((rv, ex) -> {
                    if (ex != null) {
                        ex.printStackTrace();
                    }
                    if (rv != null) {
                        ret.addAll(rv);
                    }
                });
            }
            CompletableFuture.allOf(all).join();
            return ret;
        }
    }

}

定时器

import com.xyc.sms.api.threeGorgesBank.fileSend.FileSendFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.text.ParseException;

/**
 * @author zeki
 */
@Component
@EnableScheduling
public class FileSendJob {

        @Autowired
        private FileSendFactory fileSendFactory;

        @Scheduled(cron = "${treeGorgesBank.jobCron:0 20 * * * ?}")
        public void fileSend() throws ParseException {
                fileSendFactory.moveFileToSend();
        }

}

定时任务跑业务(重点)

  • 主线程跑定时扫码指定目录是否有符合要求的文件,若有则进行转移到备用目录,并把转移后的文件的地址放进线程池中让线程解析该文件数据

转移文件

import cn.hutool.core.util.ArrayUtil;
import cn.hutool.core.util.IdUtil;
import cn.hutool.core.util.StrUtil;
import cn.hutool.extra.spring.SpringUtil;
import cn.hutool.log.Log;
import cn.hutool.log.LogFactory;
import com.xyc.sms.api.service.SmsTypeService;
import com.xyc.sms.api.service.UserService;
import com.xyc.sms.api.service.impl.SmsTypeServiceImpl;
import com.xyc.sms.api.service.impl.UserServiceImpl;
import com.xyc.sms.boss.entity.User;
import com.xyc.sms.common.base.utils.DateUtil;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

import java.io.File;
import java.text.ParseException;
import java.util.Calendar;
import java.util.GregorianCalendar;
import java.util.Objects;
import java.util.concurrent.ThreadPoolExecutor;

/**
 * @author zeki豪
 */
@Component
public class FileSendFactory {
    private static final Log logger = LogFactory.get();
    //    文件原目录
    @Value("${treeGorgesBank.fileSend.dirPath}")
    private String dirPath;

    //   备份目录
    @Value("${treeGorgesBank.fileSend.backupDirPath}")
    private String backupDirPath;


    @Autowired
    @Qualifier("fileSendThreadPool")
    private ThreadPoolExecutor executor;


    //    1.检查是否含有当天或之前时间段要发送的文件,2.将该文件`原目录的文件`转移到`备份目录`去且改名标记为唯一命名文件,3.获取该转移后的文件地址放进线程池里执行线程任务解析文件数据存入队列中去发送
    public void moveFileToSend() throws ParseException {
        try {

//       1.1判断原目录下的文件是否以.txt结尾的才处理
            File dir = new File(dirPath);
            if (dir.exists()) {
                File[] files = dir.listFiles();
                if (ArrayUtil.isEmpty(files)) {
//                    没有文件不执行
                    return;
                }
                for (File txt : files) {
//                        不符合条件的文件名 不处理
                    if (!txt.isFile() || !StrUtil.endWithIgnoreCase(txt.getName(), ".txt")) {
                        continue;
                    }

                    if (!StringUtils.equals(txt.getName().substring(0, 3), "mas") && !StringUtils.equals(txt.getName().substring(0, 3), "sms")) {
                        continue;
                    }

//                    校验账号
                    //            获取账号信息
                    String fileAccount = txt.getName().substring(0, 3);
                    UserService userService = SpringUtil.getBean(UserServiceImpl.class);
                    User user = userService.loginByAccount(fileAccount);
                    // 用户不存在不处理
                    if (user == null) {
                        logger.error("[moveFileToSend]用户不存在,文件名:{}",txt.getName());
                        continue;
                    }


                    String smsTypeIdById = userService.getSmsTypeIdById(user.getId());
                    if (StringUtils.isBlank(smsTypeIdById)){
                        logger.error("[moveFileToSend]用户短信类型配置不存在,文件名:{}",txt.getName());
                        continue;
                    }

                    SmsTypeService smsTypeService = SpringUtil.getBean(SmsTypeServiceImpl.class);
                    Integer smsTypeId = Integer.valueOf(smsTypeIdById.split(",")[0]);
                    Integer priority = smsTypeService.getPriority(smsTypeId);
                    if (Objects.isNull(priority)){
                        logger.error("[moveFileToSend]用户的短信类型未配置优先级,文件名:{}",txt.getName());
                        continue;
                    }

//                    1.2    转入备份目录,避免有文件重复命名 将文件改名成唯一命名并转移 保证文件命名唯一性
                    String dirResult = "";
                    File oldFile = new File(txt.getParent() + "/" + txt.getName());
                    String toBackStr = "";

//                            获取文件预约时间,若大于当前时间则不做处理
                    String[] split = txt.getName().split("\\.");
                    String timeStr = split[4];
//       将该文件预约发送时间数据 转化成时间字符串格式xx:xx:xx
                    String fileTime = timeStr.substring(0, 2) + ":" + timeStr.substring(2, 4) + ":" + timeStr.substring(4, 6);
                    if (timeStr.length() != 6) {
                        logger.error("[moveFileToSend]文件时间格式命名有误:{}", txt.getName());
                    }
                    //当前时间   时分秒
                    GregorianCalendar calendar = new GregorianCalendar();
                    int hour = calendar.get(Calendar.HOUR_OF_DAY);
                    int minute = calendar.get(Calendar.MINUTE);
                    int second = calendar.get(Calendar.SECOND);

                    String hour_str = String.valueOf(hour);
                    String minute_str = String.valueOf(minute);
                    String second_str = String.valueOf(second);

                    String nowTime = hour_str + ":" + minute_str + ":" + second_str;

                    boolean startT = compTime(nowTime, fileTime, txt.getName());

                    if (!startT) {
//                                预约时间没到,不做处理
                        return;
                    }

                    toBackStr = backupDirPath + "/" + DateUtil.getCurrentDate().replace("-", "");
                    File newFile = new File(toBackStr + "/" + txt.getName());
//                          2.改名并转移文件至备份目录 代表该文件已被处理
                    if (!new File(toBackStr).exists()) {
                        (new File(toBackStr)).mkdirs(); // 如果文件夹不存在 则建立新文件夹
                    }

                    if (oldFile.renameTo(newFile)) {
                        logger.info("[moveFileToSend]文件移动成功至:" + newFile.getParent() + "/" + newFile.getName());
                        dirResult = newFile.getParent() + "/" + newFile.getName();
                    } else {
                        logger.error("[moveFileToSend]文件移动失败:" + txt.getParent() + "/" + txt.getName());
                    }


//                              将转移后的文件 放进线程池处理解析数据
                    if (StringUtils.isNotBlank(dirResult)) {
                        executor.execute(new FileSendRunnable(dirResult));
                    }

                }

            }


        } catch (Exception e) {
            logger.error(e);
        }

    }

    /**
     * 比较两个时间 时分秒 大小
     *
     * @param s1
     * @param s2
     * @return
     */
    public static boolean compTime(String s1, String s2, String fileName) {
        try {
            if (s1.indexOf(":") < 0 || s1.indexOf(":") < 0) {
                logger.error("文件命名格式有误,文件名{}:,当前时间为:{},文件格式时间数据为:{}", fileName, s1, s2);
            } else {
                String[] array1 = s1.split(":");
                int total1 = Integer.valueOf(array1[0]) * 3600 + Integer.valueOf(array1[1]) * 60 + Integer.valueOf(array1[2]);
                String[] array2 = s2.split(":");
                int total2 = Integer.valueOf(array2[0]) * 3600 + Integer.valueOf(array2[1]) * 60 + Integer.valueOf(array2[2]);
                return total1 - total2 > 0 ? true : false;
            }
        } catch (NumberFormatException e) {
            logger.error("[moveFileToSend]解析文件命名有误,文件名:{},参数一{},参数二{},报错{}" + fileName, s1, s2, e);
        }
        return false;

    }

}

解析文件数据

public class FileSendRunnable implements Runnable {

    private static final Logger logger = LoggerFactory.getLogger(FileSendRunnable.class);
    
    public static final EncodingDetect ENCODING_DETECT = new EncodingDetect();//获取文件编码格式的工具类


    private String filePath;

    public FileSendRunnable(String filePath) {
        this.filePath = filePath;
    }

    @Override
    public void run() {
        BufferedReader reader = null;
        try {
//          计数未能解析的条数
            int failCount = 0;

            File file = new File(filePath);
            if (Objects.isNull(file)) {
                logger.error("[fileSendRunnable]寻找不到该文件" + filePath);
                return;
            }

            String fileRealName = file.getName().contains("--") ? file.getName().split("--")[1] : file.getName();

            // 防止ftp文件未上传完毕 或者文件移动renameTo没结束,导致后面读取不完整
            blockCheckFtpFileComplete(file, 20 * 1000);

            String[] splitSys = fileRealName.split("\\.");
            String sysId = ""; //系统id
//            获取文件名中字段值
            try {
                sysId = splitSys[5];
            } catch (Exception e) {

            }

            String charset = EncodingDetect.javaname[ENCODING_DETECT.detectEncoding(file)];
            if (StringUtils.isNotEmpty(charset) && charset.toUpperCase().startsWith("GB")) {
                charset = "GBK";
            }

            reader = IoUtil.getReader(IoUtil.toStream(file), charset);


            logger.info("[fileSendRunnable] - 地址 : {}, 大小 :{} ", file.getCanonicalPath(), FileUtil.size(file));
            String str = null;

//            获取账号信息
            String fileAccount = fileRealName.substring(0, 3);
            UserService userService = SpringUtil.getBean(UserServiceImpl.class);
            User user = userService.loginByAccount(fileAccount);
            // 用户不存在不处理
            if (user == null) {
                logger.error("[fileSendRunnable]用户不存在,文件名:{}", filePath);
                return;
            }

//         校验 用户短信类型
            SmsTypeEntity type = null;
            ApiSender u = ApiSymbol.API_SENDER_MAP_STA0.get(user.getUserAccount());
            String typeId;
            if (StringUtils.isNotEmpty(u.getuExt1()) && StringUtils.isNotEmpty((typeId = u.getuExt1().split(",")[0]))) {
                type = ApiSymbol.SMS_TYPE_ENTITY_MAP.get(Integer.valueOf(typeId));
                if (type == null) {
                    type = ApiSymbol.SMS_TYPE_ENTITY_MAP.putIfAbsent(Integer.valueOf(typeId), userService.findByTypeId(Integer.valueOf(typeId)));
                    if (type == null) {
                        type = ApiSymbol.SMS_TYPE_ENTITY_MAP.get(Integer.valueOf(typeId));
                    }
                }
            }
            if (type == null) {
                logger.error("[fileSendRunnable]用户短信类型为空,文件名:{}", filePath);
                return;
            }
            SmsConfig smsConfig = userService.getSmsConfigByUserId(user.getId());
            if (smsConfig == null) {
                logger.error("[fileSendRunnable]使用userId:{} 查询b_sms_config中的短信配置找不到,文件名:{}", user.getId(), filePath);
                return;
            }

            SmsTypeService smsTypeService = SpringUtil.getBean(SmsTypeServiceImpl.class);
            String smsTypeIdById = userService.getSmsTypeIdById(user.getId());
            Integer smsTypeId = Integer.valueOf(smsTypeIdById.split(",")[0]);
            Integer priority = smsTypeService.getPriority(smsTypeId);


            //key短信类型  value审批表对象
            HashMap<Integer, Approval> approvalMap = new HashMap<>();

            while ((str = reader.readLine()) != null) {
//                开始遍历 每行 短信数据
                String[] split = str.split("\\|");
//                判断该条数据 字段数是否符合要求
                int count = 0;
                String strCopy = str;
                while (strCopy.contains("|")) {
                    strCopy = strCopy.substring(strCopy.indexOf("|") + 1);
                    ++count;
                }
//                  需要5或11条数据 才符合要求
                if (count != 4 && count != 10) {
                    logger.warn("[fileSendRunnable] 参数数量不符 | {} | {}", file.getCanonicalPath(), str);
                    failCount++;
                    continue;
                }

//                    获取文件数据信息
                String customerNo = "";//客户号
                String account = "";//账号 无数据
                String mobile = "";//手机号
                String content = "";//短信内容
                String serialNo = "";//序列号
                String typeStr = ""; //短信类型
                String keyCode = ""; //核心摘要码
                String serialNum = ""; //全局流水号
                String organization = ""; //组织机构
                String templatecode = ""; //短信模板编码
                String remark = ""; //备注信息
                try {
                    customerNo = split[0];//客户号
                    account = split[1];//账号 无数据
                    mobile = split[2];//手机号
                    content = split[3];//短信内容
                    serialNo = split[4];//序列号
                    typeStr = split[5];//短信类型
                    keyCode = split[6];//核心摘要码
                    serialNum = split[7];//全局流水号
                    organization = split[8];//组织机构
                    templatecode = split[9];//短信模板编码
                    remark = split[10];//备注信息
                } catch (Exception e) {

                }


                if (StrUtil.isBlank(content)) {
                    logger.warn("[fileSendRunnable] 短信内容为空 | {} | {}", file.getCanonicalPath(), str);
                    failCount++;
                    continue;
                }
                int mobileType = MobileUtilNew.getMobileType(mobile);
                if (mobileType == -1) {
                    logger.warn("[fileSendRunnable] 非法号码 | {} | {}", file.getCanonicalPath(), str);
                    failCount++;
                    continue;
                }


                Integer typeInteger = null;
//                    判断短信数据, 若有设置短信类型则用短信数据的短信类型, 若无则使用用户的短信类型
                if (StringUtils.isNotBlank(typeStr)) {
                    String userType = ApiSymbol.SMS_TYPE_USER_MAP.get(fileAccount);
                    typeInteger = ApiSymbol.SMS_TYPE_STR_ENTITY_MAP.get(typeStr).getTypeId();
                    if (Objects.isNull(typeInteger) || StringUtils.isBlank(userType) || !Arrays.asList(userType.split(",")).contains(String.valueOf(typeInteger))) {
//                            若数据里的 短信类型配置不在 用户所配置的里 则过滤不处理该条数据
                        logger.warn("[fileSendRunnable] 短信类型不存在 | {} | {}", file.getCanonicalPath(), str);
                        failCount++;
                        continue;
                    }
                } else {
                    typeInteger = type.getTypeId();
                }

                Approval resultApproval = approvalMap.get(typeInteger);

//                    初始化map
                if (Objects.isNull(resultApproval)) {
                    resultApproval = new Approval();
                    String batchNo = BatchNoUtil.nextForOpenApi(FromTypeEnum.FILE_LOAD,IdMangerConfig.serverNodeId);//批次号
                    resultApproval.setBatchNo(batchNo);
                    String[] arr = SignTextUtil.separateSignAndContent(content, u.getSignType(),u.getSignLocation(),u.getZhSign(),u.getEnSign(), Symbols.defaultGnSign);
                    resultApproval.setContent(arr[1]);
                    resultApproval.setSmsSign(arr[0]);
                    resultApproval.setSmsCreateType(0);
                    resultApproval.setSmsTypeId(typeInteger);
                    resultApproval.setPriority(priority);
                    resultApproval.setUserId(user.getId());
                    resultApproval.setUserAccount(user.getUserAccount());
                    resultApproval.setSendType(0);
                    resultApproval.setSendTime(new Date());
                    resultApproval.setMsgFromType(FromTypeEnum.FILE_LOAD.getType());
                    resultApproval.setFileName(fileRealName);
                    resultApproval.setCreateTime(new Date());
                    resultApproval.setUpdateTime(new Date());
                    resultApproval.setTitle(fileRealName);
                    resultApproval.setSendNum(0);
                    approvalMap.put(typeInteger, resultApproval);
                }

                resultApproval.setSendNum(resultApproval.getSendNum() + 1);


//                  approvalDetail详情数据封装
                if (StrUtil.isBlank(serialNo)) {
                    serialNo = String.valueOf(ApiSymbol.SMS_ID_Worker.nextId());
                }
                ChongqingThreeGorgesBankOptField f = new ChongqingThreeGorgesBankOptField();
                f.setOptCustno(customerNo);
                f.setOptSrvaccno(account);
                f.setOptSerialnumber(serialNo);
                f.setOptSysid(sysId);
                f.setOptSrvid(sysId);
                f.setOptTypeStr(typeStr);
                f.setOptCoreSummaryCode(keyCode);
                f.setOptGlobalSeq(serialNum);
                f.setOptOrganization(organization);
                f.setOptTemplateCode(templatecode);
                f.setOptRemark(remark);
                List<ApprovalDetail> details = new ArrayList<>();
                ApprovalDetail detail = new ApprovalDetail();
                detail.setBatchNo(resultApproval.getBatchNo());
                detail.setMobile(mobile);
                detail.setMobileType(mobileType);
                String[] arr = SignTextUtil.separateSignAndContent(content, u.getSignType(),u.getSignLocation(),u.getZhSign(),u.getEnSign(), Symbols.defaultGnSign);
                detail.setContent(arr[2]);
                detail.setAccountManagerId("");
                detail.setStatus(0);
                detail.setCreateTime(new Date());
                detail.setUpdateTime(new Date());
                detail.setJsonStr(f.optFieldToJSONString());
                details.add(detail);
                if (CollectionUtil.isEmpty(resultApproval.getDetails())) {
                    resultApproval.setDetails(details);
                } else {
                    resultApproval.getDetails().addAll(details);
                }
            }

            // 将数据放入到内存队列中
            final int[] successCount = {0};//成功条数
            for (Map.Entry<Integer, Approval> entry : approvalMap.entrySet()) {
                QueueMgr.instance.putApprovalCacheQueue(new ApprovalQueue() {{
                    successCount[0] = successCount[0] + entry.getValue().getDetails().size();
                    setQueue(entry.getValue());
                    setQueueDetails(entry.getValue().getDetails());
                }});
            }

            logger.info("[fileSendRunnable] - " + file.getCanonicalPath() + ",解析失败:" + failCount + "条" + " , 解析成功: " + successCount[0] + "条");
        } catch (Exception e) {
            logger.error("[fileSendRunnable]文件处理有误", e);
        } finally {
            if (reader != null) {
                try {
                    reader.close();
                } catch (IOException e) {
                    logger.error("[fileSendRunnable]", e);
                }
            }
        }


    }


    private void blockCheckFtpFileComplete(File file, int timeOutMillis) {
        logger.info("[fileSendRunnable] 检测文件移动是否完整 | START | path:{}", filePath);
        long firstCheckTime = System.currentTimeMillis();
        String lastHex = "";
        while (true) {
            try {
                TimeUnit.MILLISECONDS.sleep(50);
            } catch (Exception ignored) {
            }

            try (FileInputStream fis = new FileInputStream(file)) {
                String tmpHex = DigestUtils.md5DigestAsHex(fis);//此方法遇到大文件比较耗时
                if (lastHex.equals(tmpHex)) {
                    logger.info("[fileSendRunnable] 检测文件移动是否完整 | END | 【sta:正常完毕】path:{} | currentLen:{} | hex:{}", filePath, file.length(), lastHex);
                    break;
                }
                if (System.currentTimeMillis() - firstCheckTime >= timeOutMillis) {
                    logger.info("[fileSendRunnable] 检测文件移动是否完整 | END | 【sta:超时完毕】path:{} | currentLen:{} | hex:{}", filePath, file.length(), lastHex);
                    break;
                }
                lastHex = tmpHex;
            } catch (Exception e) {
                logger.info("[fileSendRunnable] 检测文件移动是否完整 | END | 【sta:异常完毕】path:{} | currentLen:{} | hex:{}", filePath, file.length(), lastHex);
                try {
                    TimeUnit.MILLISECONDS.sleep(1000);
                } catch (Exception ignored) {
                }
                break;
            }
        }
    }

}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值