wanghc vor 3 Jahren
Ursprung
Commit
3e0c7ef729

+ 14 - 3
ipfcst-console/src/main/java/com/jiayue/ipfcst/console/service/ReceiveDataService.java

@@ -10,6 +10,7 @@ import com.jiayue.ipfcst.common.data.entity.*;
 import com.jiayue.ipfcst.common.data.repository.*;
 import com.jiayue.ipfcst.console.util.RedisUtils;
 import org.apache.http.HttpEntity;
+import org.apache.http.client.config.RequestConfig;
 import org.apache.http.client.methods.CloseableHttpResponse;
 import org.apache.http.client.methods.HttpPost;
 import org.apache.http.entity.StringEntity;
@@ -21,6 +22,7 @@ import lombok.extern.slf4j.Slf4j;
 import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.stereotype.Service;
 
+import java.io.IOException;
 import java.math.BigDecimal;
 import java.nio.charset.StandardCharsets;
 import java.text.SimpleDateFormat;
@@ -69,8 +71,14 @@ public class ReceiveDataService {
   private  String port;
   @Value("${receive.path}")
   private  String path;
+  private  final String Active = "activePower";
+
+  //请求超时时间,这个时间定义了socket读数据的超时时间,也就是连接到服务器之后到从服务器获取响应数据需要等待的时间,发生超时,会抛出SocketTimeoutException异常。
+  private static final int SOCKET_TIME_OUT = 5000;
+  //连接超时时间,这个时间定义了通过网络与服务器建立连接的超时时间,也就是取得了连接池中的某个连接之后到接通目标url的连接等待时间。发生超时,会抛出ConnectionTimeoutException异常
+  private static final int CONNECT_TIME_OUT = 3000;
+
 
-  private  String Active = "activePower";
   public void receive() {
     List<ElectricField> electricFieldList = electricFieldService.getAll();
     //每个场站请求一次
@@ -234,7 +242,7 @@ public class ReceiveDataService {
       }
 
     } catch (Exception e) {
-      log.info("接数程序异常");
+      log.info(electricField.getName()+equipmentType.getMessage()+"接数程序异常");
       e.printStackTrace();
     }
   }
@@ -251,12 +259,15 @@ public class ReceiveDataService {
       HttpPost httpPost = new HttpPost("http://"+ip+":"+port+path);
       StringEntity entity = new StringEntity(JSON.toJSONString(paramMap),"UTF-8");
       httpPost.setEntity(entity);
+      //设置请求超时时间,链接超时时间
+      RequestConfig reqConfig = RequestConfig.custom().setSocketTimeout(SOCKET_TIME_OUT).setConnectTimeout(CONNECT_TIME_OUT).build();
+      httpPost.setConfig(reqConfig);
       httpPost.setHeader("Content-Type", "application/json; charset=utf-8");
       CloseableHttpResponse response;
       response = httpClient.execute(httpPost);
       HttpEntity responseEntity = response.getEntity();
       body = EntityUtils.toString(responseEntity, StandardCharsets.UTF_8);
-    }catch (Exception e){
+    }catch (RuntimeException | IOException e){
       e.printStackTrace();
     }
     return body;