StartCommand.java
4.28 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
package com.bsth;
import com.bsth.thread.PersonnelUpdateThread;
import com.bsth.util.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.CommandLineRunner;
import org.springframework.jdbc.core.BatchPreparedStatementSetter;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Component;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.regex.Pattern;
/**
* 随应用启动运行
*
*/
@Component
public class StartCommand implements CommandLineRunner{
public static ScheduledExecutorService mainServices = Executors.newScheduledThreadPool(2);
Logger log = LoggerFactory.getLogger(this.getClass());
private static long timeDiff;
@Autowired
PersonnelUpdateThread personnelUpdateThread;
@Autowired
private JdbcTemplate jdbcTemplate;
static {
// 中午12:00
timeDiff = (DateUtils.getTimestamp() + 1000 * 60 * 60 * 24) - System.currentTimeMillis();
}
@Override
public void run(String... arg0){
try {
log.info("在:"+timeDiff / 1000 / 60 + "分钟后开始同步数据");
personnelUpdateThread.run();
//mainServices.scheduleAtFixedRate(personnelUpdateThread, timeDiff / 1000, 60 * 60 * 24, TimeUnit.SECONDS);//timeDiff / 1000
//analyzeInterface();
} catch (Exception e) {
e.printStackTrace();
}
}
private Pattern pattern = Pattern.compile("");
private static class InterfaceRequest {
private String ip;
private String password;
private String agent;
private String uri;
private int time;
public InterfaceRequest(String ip, String password, String agent, String uri, int time) {
this.ip = ip;
this.password = password;
this.agent = agent;
this.uri = uri;
this.time = time;
}
public String getIp() {
return ip;
}
public void setIp(String ip) {
this.ip = ip;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public String getAgent() {
return agent;
}
public void setAgent(String agent) {
this.agent = agent;
}
public String getUri() {
return uri;
}
public void setUri(String uri) {
this.uri = uri;
}
public int getTime() {
return time;
}
public void setTime(int time) {
this.time = time;
}
}
public void analyzeInterface () throws IOException {
//String line = "INFO -[][180.167.126.126][Apache-HttpClient/4.5.5 (Java/1.8.0_241)][/webservice/rest/schedule_real/execs][{\"password\":[\"e126853c7f6f43b4857fa8dfe3b28b5d90be9e68\"],\"timestamp\":[\"1686192455081\"],\"nonce\":[\"2mycf6\"],\"sign\":[\"d945f2d522db4d458694a435e46a81c5ff9ee73f\"]}]<time:9462>";
final List<InterfaceRequest> requests = new ArrayList<>();
String path = "C:\\Users\\Hill\\Downloads\\main1.log";
BufferedReader reader = new BufferedReader(new FileReader(new File(path)));
String line = null;
while ((line = reader.readLine()) != null) {
int idx = line.indexOf("<time:"), idxPwdStart = line.indexOf("password\":[\""), idxPwdEnd = line.indexOf("\"]", idxPwdStart);
if (idx > 0) {
String[] array = line.split("\\]\\[");
String ip = array[1], agent = array[2], uri = array[3], time = line.substring(idx + 6, line.length() - 1), password = line.substring(idxPwdStart, idxPwdEnd);
InterfaceRequest request = new InterfaceRequest(ip, password, agent, uri, Integer.parseInt(time));
requests.add(request);
}
}
jdbcTemplate.batchUpdate("insert into bsth_c_interface_request (ip, password, agent, uri, `time`) values (?, ?, ?, ?, ?)", new BatchPreparedStatementSetter() {
@Override
public void setValues(PreparedStatement ps, int i) throws SQLException {
InterfaceRequest request = requests.get(i);
ps.setString(1, request.getIp());
ps.setString(2, request.getPassword());
ps.setString(3, request.getAgent());
ps.setString(4, request.getUri());
ps.setInt(5, request.getTime());
}
@Override
public int getBatchSize() {
return requests.size();
}
});
}
}