DriverJob.java
5.16 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
package com.ruoyi.job;
import com.ruoyi.driver.domain.Driver;
import com.ruoyi.driver.service.IDriverService;
import com.ruoyi.utils.ListUtils;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.core.ParameterizedTypeReference;
import org.springframework.http.HttpMethod;
import org.springframework.stereotype.Component;
import org.springframework.web.client.RestTemplate;
import javax.annotation.Resource;
import java.security.MessageDigest;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
/**
* 该定时任务用户获取驾驶员信息
*
* @author 20412
*/
@Component("driverJob")
public class DriverJob implements InitializingBean {
@Resource
private RestTemplate restTemplate;
@Autowired
private IDriverService driverService;
@Value("${api.url.getDriverInfo}")
private String getDriverInfoUrl;
@Value("${api.config.password}")
private String password;
@Value("${api.config.nonce}")
private String nonce;
private static IDriverService DRIVER_SERVICE;
private static RestTemplate RESTTEMPLATE;
private static String GET_DRIVER_INFO_URL;
private static String PASSWORD;
private static String NONCE;
/**
* 通过该定时任务获取驾驶员信息 并保存到数据库
*/
public void getDriverInfo(String params) throws Exception {
try {
String format = String.format(GET_DRIVER_INFO_URL, params);
Map<String, String> configMap = new HashMap<>(5);
long timestamp = System.currentTimeMillis();
configMap.put("timestamp", String.valueOf(timestamp));
configMap.put("nonce", NONCE);
configMap.put("password", PASSWORD);
String sign = getSHA1(configMap);
String url = format
+ "?timestamp=" + timestamp
+ "&nonce=" + NONCE
+ "&password=" + PASSWORD
+ "&sign=" + sign;
//生成签名
List<Driver> drivers = RESTTEMPLATE.exchange(
url,
HttpMethod.GET,
null,
new ParameterizedTypeReference<List<Driver>>() {
}).getBody();
List<List<Driver>> splitList = ListUtils.splitList(drivers, 1000);
System.out.println("开始更新数据-----");
for (List<Driver> driverList : splitList) {
DRIVER_SERVICE.insertDrivers(driverList);
}
System.out.println("数据更新完毕-----");
} catch (Exception e) {
System.out.println("执行失败:" + e.getMessage());
}
System.out.println("执行结束");
}
/**
* 获取签名
*
* @param map
* @return
* @throws Exception
*/
public static String getSHA1(Map<String, String> map) throws Exception {
try {
String[] array = new String[map.size()];
map.values().toArray(array);
StringBuffer sb = new StringBuffer();
// 字符串排序
Arrays.sort(array);
for (int i = 0; i < array.length; i++) {
sb.append(array[i]);
}
String str = sb.toString();
// SHA1签名生成
MessageDigest md = MessageDigest.getInstance("SHA-1");
md.update(str.getBytes());
byte[] digest = md.digest();
StringBuffer hexstr = new StringBuffer();
String shaHex = "";
for (int i = 0; i < digest.length; i++) {
shaHex = Integer.toHexString(digest[i] & 0xFF);
if (shaHex.length() < 2) {
hexstr.append(0);
}
hexstr.append(shaHex);
}
return hexstr.toString();
} catch (Exception e) {
throw e;
}
}
// public static void main(String[] args) throws Exception{
// RestTemplate restTemplate1 = new RestTemplate();
// String url = "http://101.95.136.206:9089/webservice/rest/person/company/%s";
// String format = String.format(url,"99");
// Map<String, String> configMap = new HashMap<>(5);
// long timestamp = System.currentTimeMillis();
// configMap.put("timestamp", String.valueOf(timestamp));
// configMap.put("nonce", "NONCE");
// configMap.put("password", "c4dd3d8cb9a82f6d6a625818618b28ca7bebb464");
// String sign = getSHA1(configMap);
// String httpUrl = format
// + "?timestamp=" + timestamp
// + "&nonce=" + "NONCE"
// + "&password=" + "c4dd3d8cb9a82f6d6a625818618b28ca7bebb464"
// + "&sign=" + sign;
// Object forObject = restTemplate1.getForObject(httpUrl, Object.class);
// }
@Override
public void afterPropertiesSet() throws Exception {
GET_DRIVER_INFO_URL = getDriverInfoUrl;
NONCE = nonce;
PASSWORD = password;
RESTTEMPLATE = restTemplate;
DRIVER_SERVICE = driverService;
}
}