|
|
1
|
+package com.sy.task;
|
|
|
2
|
+
|
|
|
3
|
+import com.sy.bwAnalysis.AnalysisRoute;
|
|
|
4
|
+import com.sy.logic.LiftBar;
|
|
|
5
|
+import com.sy.utils.XMLThreadPoolFactory;
|
|
|
6
|
+import com.tianbo.util.Date.DateUtil;
|
|
|
7
|
+import com.tianbo.util.IO.FileTool;
|
|
|
8
|
+import org.apache.commons.io.FileUtils;
|
|
|
9
|
+import org.springframework.beans.factory.annotation.Value;
|
|
|
10
|
+import org.springframework.scheduling.annotation.Scheduled;
|
|
|
11
|
+import org.springframework.stereotype.Component;
|
|
|
12
|
+
|
|
|
13
|
+import java.io.File;
|
|
|
14
|
+import java.text.SimpleDateFormat;
|
|
|
15
|
+import java.util.Date;
|
|
|
16
|
+import java.util.List;
|
|
|
17
|
+import java.util.concurrent.CountDownLatch;
|
|
|
18
|
+import java.util.concurrent.ThreadPoolExecutor;
|
|
|
19
|
+
|
|
|
20
|
+/**
|
|
|
21
|
+ * 回执解析定时任务
|
|
|
22
|
+ */
|
|
|
23
|
+@Component
|
|
|
24
|
+
|
|
|
25
|
+public class TaskAnalysis {
|
|
|
26
|
+ private static final org.slf4j.Logger log = org.slf4j.LoggerFactory.getLogger(TaskAnalysis.class);
|
|
|
27
|
+ //回执读取目录
|
|
|
28
|
+ @Value("${custom.receptDirectory}")
|
|
|
29
|
+ private String receptDir;
|
|
|
30
|
+
|
|
|
31
|
+ /**
|
|
|
32
|
+ * 线程数量
|
|
|
33
|
+ */
|
|
|
34
|
+ private final static int theadamount = 16;
|
|
|
35
|
+
|
|
|
36
|
+ @Scheduled(fixedRate = 5000)
|
|
|
37
|
+ public void startTask(){
|
|
|
38
|
+ final SimpleDateFormat sdf = new SimpleDateFormat(
|
|
|
39
|
+ "yyyy-MM-dd HH:mm:ss");
|
|
|
40
|
+
|
|
|
41
|
+ final String startTime = sdf.format(new Date());
|
|
|
42
|
+
|
|
|
43
|
+ //读取目录
|
|
|
44
|
+ String readDir = receptDir;
|
|
|
45
|
+
|
|
|
46
|
+ //初始化线程池
|
|
|
47
|
+ ThreadPoolExecutor threadPool = XMLThreadPoolFactory.instance("file-read");
|
|
|
48
|
+
|
|
|
49
|
+ try {
|
|
|
50
|
+ File fileDirectory = new File(readDir);
|
|
|
51
|
+ List<File> files = FileTool.readDirectoryFiles(fileDirectory);
|
|
|
52
|
+ //文件数量大于50个,每次只解析前50个
|
|
|
53
|
+ if (files!=null && !files.isEmpty() && files.size()>theadamount){
|
|
|
54
|
+ CountDownLatch latch = new CountDownLatch(theadamount);
|
|
|
55
|
+ log.trace("本地解析报文任务开始{},剩余处理文件数量:{}",startTime,files.size());
|
|
|
56
|
+ for (int i=0;i<theadamount;i++){
|
|
|
57
|
+ threadJbob(files.get(i),latch,"",threadPool);
|
|
|
58
|
+ }
|
|
|
59
|
+ latch.await();
|
|
|
60
|
+ }
|
|
|
61
|
+ //文件数量小于50个,全部一次解析完
|
|
|
62
|
+ else if (files!=null && !files.isEmpty() && files.size()<theadamount){
|
|
|
63
|
+ CountDownLatch latch = new CountDownLatch(files.size());
|
|
|
64
|
+ log.trace("本地解析报文任务开始{},剩余处理文件数量文件数量:{}",startTime,files.size());
|
|
|
65
|
+ for (int i=0;i<files.size();i++){
|
|
|
66
|
+ threadJbob(files.get(i),latch,"",threadPool);
|
|
|
67
|
+ }
|
|
|
68
|
+ latch.await();
|
|
|
69
|
+ }
|
|
|
70
|
+
|
|
|
71
|
+ }catch (Exception e){
|
|
|
72
|
+ e.printStackTrace();
|
|
|
73
|
+ log.error("获取目录文件出错",e);
|
|
|
74
|
+ }
|
|
|
75
|
+
|
|
|
76
|
+ log.info("本地解析报文任务结束{}",sdf.format(new Date()));
|
|
|
77
|
+
|
|
|
78
|
+ }
|
|
|
79
|
+
|
|
|
80
|
+ private void threadJbob(File file,CountDownLatch latch,String transToCfps,ThreadPoolExecutor threadPool){
|
|
|
81
|
+ try{
|
|
|
82
|
+ AnalysisRoute analysisRoute=new AnalysisRoute();
|
|
|
83
|
+ String message = com.sy.utils.FileTool.readfile(file,"UTF-8");
|
|
|
84
|
+ analysisRoute.setMessage(message);
|
|
|
85
|
+ threadPool.execute(analysisRoute);
|
|
|
86
|
+ FileUtils.moveFileToDirectory(file,new File("bw/handled/"),true);
|
|
|
87
|
+ }catch (Exception e){
|
|
|
88
|
+ log.error("线程解析出错{}",e.toString());
|
|
|
89
|
+ }
|
|
|
90
|
+
|
|
|
91
|
+ }
|
|
|
92
|
+
|
|
|
93
|
+} |