|  |  | package com.sy.task; | 
|  |  |  | 
|  |  | import com.sy.bwAnalysis.AnalysisRoute; | 
|  |  | import com.sy.logic.LiftBar; | 
|  |  | import com.sy.utils.XMLThreadPoolFactory; | 
|  |  | import com.tianbo.util.Date.DateUtil; | 
|  |  | import com.tianbo.util.IO.FileTool; | 
|  |  | import org.apache.commons.io.FileUtils; | 
|  |  | import org.springframework.beans.factory.annotation.Value; | 
|  |  | import org.springframework.scheduling.annotation.Scheduled; | 
|  |  | import org.springframework.stereotype.Component; | 
|  |  |  | 
|  |  | import java.io.File; | 
|  |  | import java.text.SimpleDateFormat; | 
|  |  | import java.util.Date; | 
|  |  | import java.util.List; | 
|  |  | import java.util.concurrent.CountDownLatch; | 
|  |  | import java.util.concurrent.ThreadPoolExecutor; | 
|  |  |  | 
|  |  | /** | 
|  |  | * 回执解析定时任务 | 
|  |  | */ | 
|  |  | @Component | 
|  |  |  | 
|  |  | public class TaskAnalysis { | 
|  |  | private static final org.slf4j.Logger log = org.slf4j.LoggerFactory.getLogger(TaskAnalysis.class); | 
|  |  | //回执读取目录 | 
|  |  | @Value("${custom.receptDirectory}") | 
|  |  | private  String receptDir; | 
|  |  |  | 
|  |  | /** | 
|  |  | * 线程数量 | 
|  |  | */ | 
|  |  | private final static int theadamount = 16; | 
|  |  |  | 
|  |  | @Scheduled(fixedRate = 5000) | 
|  |  | public void startTask(){ | 
|  |  | final SimpleDateFormat sdf = new SimpleDateFormat( | 
|  |  | "yyyy-MM-dd HH:mm:ss"); | 
|  |  |  | 
|  |  | final  String startTime = sdf.format(new Date()); | 
|  |  |  | 
|  |  | //读取目录 | 
|  |  | String readDir = receptDir; | 
|  |  |  | 
|  |  | //初始化线程池 | 
|  |  | ThreadPoolExecutor threadPool = XMLThreadPoolFactory.instance("file-read"); | 
|  |  |  | 
|  |  | try { | 
|  |  | File fileDirectory = new File(readDir); | 
|  |  | List<File> files = FileTool.readDirectoryFiles(fileDirectory); | 
|  |  | //文件数量大于50个,每次只解析前50个 | 
|  |  | if (files!=null && !files.isEmpty() && files.size()>theadamount){ | 
|  |  | CountDownLatch latch = new CountDownLatch(theadamount); | 
|  |  | log.trace("本地解析报文任务开始{},剩余处理文件数量:{}",startTime,files.size()); | 
|  |  | for (int i=0;i<theadamount;i++){ | 
|  |  | threadJbob(files.get(i),latch,"",threadPool); | 
|  |  | } | 
|  |  | latch.await(); | 
|  |  | } | 
|  |  | //文件数量小于50个,全部一次解析完 | 
|  |  | else if (files!=null && !files.isEmpty() && files.size()<theadamount){ | 
|  |  | CountDownLatch latch = new CountDownLatch(files.size()); | 
|  |  | log.trace("本地解析报文任务开始{},剩余处理文件数量文件数量:{}",startTime,files.size()); | 
|  |  | for (int i=0;i<files.size();i++){ | 
|  |  | threadJbob(files.get(i),latch,"",threadPool); | 
|  |  | } | 
|  |  | latch.await(); | 
|  |  | } | 
|  |  |  | 
|  |  | }catch (Exception e){ | 
|  |  | e.printStackTrace(); | 
|  |  | log.error("获取目录文件出错",e); | 
|  |  | } | 
|  |  |  | 
|  |  | log.info("本地解析报文任务结束{}",sdf.format(new Date())); | 
|  |  |  | 
|  |  | } | 
|  |  |  | 
|  |  | private void threadJbob(File file,CountDownLatch latch,String transToCfps,ThreadPoolExecutor threadPool){ | 
|  |  | try{ | 
|  |  | AnalysisRoute analysisRoute=new AnalysisRoute(); | 
|  |  | String message = com.sy.utils.FileTool.readfile(file,"UTF-8"); | 
|  |  | analysisRoute.setMessage(message); | 
|  |  | threadPool.execute(analysisRoute); | 
|  |  | FileUtils.moveFileToDirectory(file,new File("bw/handled/"),true); | 
|  |  | }catch (Exception e){ | 
|  |  | log.error("线程解析出错{}",e.toString()); | 
|  |  | } | 
|  |  |  | 
|  |  | } | 
|  |  |  | 
|  |  | } | 
... | ... |  |