审查视图

src/main/java/com/tianbo/analysis/task/TaskAnalysis.java 4.4 KB
朱兆平 authored
1 2 3 4
package com.tianbo.analysis.task;

import com.tianbo.util.Date.DateUtil;
import com.tianbo.util.IO.FileTool;
朱兆平 authored
5
import com.tianbo.analysis.handle.CustomXmlHandleThread;
6
import lombok.extern.slf4j.Slf4j;
7
import org.apache.commons.io.FileUtils;
朱兆平 authored
8 9 10 11 12
import org.springframework.beans.factory.annotation.Value;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.io.File;
13 14 15
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
朱兆平 authored
16
import java.util.List;
17
import java.util.concurrent.*;
朱兆平 authored
18 19 20 21

/**
 * 回执解析定时任务
 */
22
@Slf4j
朱兆平 authored
23 24 25 26 27 28 29
@Component
public class TaskAnalysis {

    //回执读取目录
    @Value("${custom.receptDirectory}")
    private  String receptDir;
30 31 32 33
    //回执转发目录
    @Value("${custom.transmitDir}")
    private String transmitDir;
朱兆平 authored
34 35

36 37 38 39 40
    /**
     * 线程数量
     */
    private final static int theadamount = 50;
41
    @Scheduled(fixedRate = 5000)
朱兆平 authored
42
    public void startTask(){
43 44 45 46 47 48 49
        final SimpleDateFormat sdf = new SimpleDateFormat(
                "yyyy-MM-dd HH:mm:ss");

        final  String startTime = sdf.format(new Date());

        //设置转发文件夹
        String today = DateUtil.getTodayBy_yyyyMMdd();
朱兆平 authored
50
        String transToCfps =  transmitDir;
朱兆平 authored
51 52
        //回执目录
        String readDir = receptDir;
53 54 55 56 57

        //初始化线程池
        ThreadPoolExecutor threadPool = XMLThreadPoolFactory.instance();

        try {
朱兆平 authored
58
            File fileDirectory = new File(readDir);
59 60
            List<File> files = FileTool.readDirectoryFiles(fileDirectory);
            //文件数量大于50个,每次只解析前50个
61 62
            if (files!=null && !files.isEmpty() && files.size()>theadamount){
                CountDownLatch latch = new CountDownLatch(theadamount);
63
                log.trace("解析任务开始{},剩余处理文件数量:{}",startTime,files.size());
64
                for (int i=0;i<theadamount;i++){
65
                    threadJbob(files.get(i),latch,transToCfps,threadPool);
朱兆平 authored
66
                }
67 68 69
                latch.await();
            }
            //文件数量小于50个,全部一次解析完
70
            else if (files!=null && !files.isEmpty() && files.size()<theadamount){
71
                CountDownLatch latch = new CountDownLatch(files.size());
72
                log.trace("解析任务开始{},剩余处理文件数量文件数量:{}",startTime,files.size());
73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99
                for (int i=0;i<files.size();i++){
                    threadJbob(files.get(i),latch,transToCfps,threadPool);
                }
                latch.await();
            }

//            Iterator<File> it = files.iterator();
//            while (it.hasNext()) {
//                if (threadPool.getActiveCount() < 100) {
//                    File file = it.next();
//                    try {
//                        FileUtils.copyFileToDirectory(file, new File(transToCfps));
//                    } catch (IOException e) {
//                        e.printStackTrace();
//                        log.error("复制报文{}到回执转发目录失败", file.getName());
//                    }
//
//                    try {
//                        CustomXmlHandleThread customXmlHandleThread = new CustomXmlHandleThread();
//                        customXmlHandleThread.setXmlfile(file);
//                        threadPool.execute(customXmlHandleThread);
//                    } catch (Exception e) {
//                        e.printStackTrace();
//                        log.error("解析回执出错", e);
//                    }
//                }
//            }
100 101 102
        }catch (Exception e){
            e.printStackTrace();
            log.error("获取目录文件出错",e);
朱兆平 authored
103
        }
104
105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121
        log.info("解析任务结束{}",sdf.format(new Date()));

    }

    private void threadJbob(File file,CountDownLatch latch,String transToCfps,ThreadPoolExecutor threadPool){
        try{
            FileUtils.copyFileToDirectory(file, new File(transToCfps));
            CustomXmlHandleThread customXmlHandleThread = new CustomXmlHandleThread();
            customXmlHandleThread.setXmlfile(file);
            customXmlHandleThread.setLatch(latch);
            threadPool.execute(customXmlHandleThread);
        }catch (IOException e){
            log.error("备份文件{}出错,错误代码:{}",file,e);
        }catch (Exception e){
            log.error("线程解析出错{}",e);
        }
朱兆平 authored
122
    }
123
朱兆平 authored
124
}