Commit ef3b1124 authored by 郝彦辉's avatar 郝彦辉

百行每天的重新报送优化成定时任务3

parent 4aeb8643
......@@ -5,16 +5,11 @@ import cn.quantgroup.report.service.common.CommonQueryService;
import cn.quantgroup.report.service.manualTool.CleanningTransactionLogService;
import cn.quantgroup.report.service.manualTool.ManualToolService;
import com.alibaba.fastjson.JSON;
import com.google.common.collect.ImmutableMap;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import java.net.Inet4Address;
import java.net.InetAddress;
import java.net.NetworkInterface;
import java.util.Enumeration;
import java.util.Map;
@Slf4j
......@@ -197,57 +192,4 @@ public class ManualToolController {
return "deleteTidbCallRecordCF调度完成";
}
@RequestMapping("/deleteRedisKey")
public Map<String,Object> deleteRedisKey(String key1, String key2, String key3){
try{
manualToolService.deleteRedisKey(key1, key2, key3);
return ImmutableMap.of("code", 0, "msg", "手动删除redisKey成功");
}catch(Exception e){
return ImmutableMap.of("code", 1, "msg", "手动设置redisKey异常");
}
}
@RequestMapping("/getRedisVal")
public String getRedisVal(String key){
try{
return manualToolService.getRedisVal(key);
}catch(Exception e){
return "获取redis值异常";
}
}
@RequestMapping("/getIp")
public String getIp(){
try{
return getServerIp();
}catch(Exception e){
return "获取本地ip异常";
}
}
private String getServerIp() {
try {
Enumeration<NetworkInterface> allNetInterfaces = NetworkInterface.getNetworkInterfaces();
while (allNetInterfaces.hasMoreElements()) {
NetworkInterface netInterface = (NetworkInterface) allNetInterfaces.nextElement();
Enumeration<InetAddress> addresses = netInterface.getInetAddresses();
while (addresses.hasMoreElements()) {
InetAddress ip = (InetAddress) addresses.nextElement();
if (ip != null
&& ip instanceof Inet4Address
&& !ip.isLoopbackAddress() /* loopback地址即本机地址,IPv4的loopback范围是127.0.0.0 ~ 127.255.255.255 */
&& ip.getHostAddress().indexOf(":") == -1) {
System.out.println("本机的IP = " + ip.getHostAddress());
return ip.getHostAddress();
}
}
}
} catch (Exception e) {
log.error("获取本地ip异常", e);
}
return null;
}
}
......@@ -825,34 +825,42 @@ public class HttpRequestUtil {
}
}
//重新报送的工具使用,可能慢
public static String doPostTool(String apiUrl, Map<String, Object> params) {
String result = "";
httpClient = getHttpClient();
HttpPost httpPost = new HttpPost(apiUrl);
String response = "";
try {
List<NameValuePair> pairList = new ArrayList<>(params.size());
for (Map.Entry<String, Object> entry : params.entrySet()) {
NameValuePair pair = new BasicNameValuePair(entry.getKey(), entry
.getValue().toString());
pairList.add(pair);
}
httpPost.setEntity(new UrlEncodedFormEntity(pairList, Charset.forName("UTF-8")));
HttpResponse response = httpClient.execute(httpPost);
String bodyStr = JSON.toJSONString(params);
byte[] body = bodyStr.getBytes("UTF-8");
java.net.URL parsedUrl = new java.net.URL(apiUrl);
java.net.HttpURLConnection conn = (java.net.HttpURLConnection) parsedUrl.openConnection();
//int statusCode = response.getStatusLine().getStatusCode();
HttpEntity entity = response.getEntity();
conn.setConnectTimeout(6000);
conn.setReadTimeout(60000);//60秒
conn.setUseCaches(false);
result = EntityUtils.toString(entity);
conn.setRequestMethod("POST");
conn.setDoOutput(true);
conn.setRequestProperty("Connection", "keep-alive");
conn.setRequestProperty("Content-Type", "application/json;charset=UTF-8");
conn.setRequestProperty("Content-Length", String.valueOf(body.length));
OutputStream outStream = conn.getOutputStream();
outStream.write(body);
outStream.flush();
outStream.close();
EntityUtils.consume(entity);
if(conn.getResponseCode() == 200){
BufferedReader in = new BufferedReader(new InputStreamReader((InputStream) conn.getInputStream(), "UTF-8"));
response = in.readLine();
in.close();
}
conn.disconnect();
return response;
} catch (Exception e) {
log.error("doPostTool httpRequest, apiUrl: "+apiUrl+"请求失败", e);
throw new QGException(QGExceptionType.COMMON_THIRD_PART_CALL_EXCEPTION, e.toString()+";url="+apiUrl);
}finally {
} catch (IOException e) {
log.error("doPostTool请求apiUrl: " + apiUrl + "请求失败", e);
throw new QGException(QGExceptionType.COMMON_THIRD_PART_CALL_EXCEPTION, e.toString() + ";url:" + apiUrl);
} finally {
httpPost.releaseConnection();
}
return result;
}
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment