Bladeren bron

gzhWaitingPublishContent 待发布内容获取超时时间修改

wangyunpeng 4 weken geleden
bovenliggende
commit
e502495166

+ 29 - 26
long-article-recommend-service/src/main/java/com/tzld/longarticle/recommend/server/remote/aigc/AIGCWaitingPublishContentService.java

@@ -2,21 +2,15 @@ package com.tzld.longarticle.recommend.server.remote.aigc;
 
 import com.alibaba.fastjson.JSONArray;
 import com.alibaba.fastjson.JSONObject;
-import com.tzld.longarticle.recommend.server.common.HttpPoolFactory;
 import com.tzld.longarticle.recommend.server.model.dto.Content;
 import com.tzld.longarticle.recommend.server.service.recommend.recall.RecallParam;
 import lombok.extern.slf4j.Slf4j;
-import org.apache.http.HttpEntity;
-import org.apache.http.StatusLine;
-import org.apache.http.client.methods.CloseableHttpResponse;
-import org.apache.http.client.methods.HttpPost;
-import org.apache.http.entity.StringEntity;
-import org.apache.http.impl.client.CloseableHttpClient;
-import org.apache.http.util.EntityUtils;
+import okhttp3.*;
 import org.springframework.stereotype.Service;
 
-import java.nio.charset.StandardCharsets;
+import javax.annotation.PostConstruct;
 import java.util.*;
+import java.util.concurrent.TimeUnit;
 
 /**
  * @author dyp
@@ -24,7 +18,18 @@ import java.util.*;
 @Service
 @Slf4j
 public class AIGCWaitingPublishContentService {
-    private final CloseableHttpClient client = HttpPoolFactory.aigcPool();
+
+    private OkHttpClient client;
+
+    @PostConstruct
+    public void init() {
+        client = new OkHttpClient().newBuilder()
+                .connectTimeout(5, TimeUnit.MINUTES)
+                .readTimeout(5, TimeUnit.MINUTES)
+                .writeTimeout(5, TimeUnit.MINUTES)
+                .build();
+    }
+
     private static final String aigcGetAllContentUrl = "http://aigc-api.cybertogether.net/aigc/publish/content/gzhWaitingPublishContent";
 
 
@@ -48,22 +53,20 @@ public class AIGCWaitingPublishContentService {
         bodyParamParams.put("planId", param.getPlanId());
         bodyParam.put("params", bodyParamParams);
         try {
-            HttpPost httpPost = new HttpPost(aigcGetAllContentUrl);
-            StringEntity stringEntity = new StringEntity(bodyParam.toJSONString(), StandardCharsets.UTF_8);
-            httpPost.setHeader("Content-Type", "application/json;charset=UTF-8");
-            httpPost.setEntity(stringEntity);
-            CloseableHttpResponse response = client.execute(httpPost);
-            StatusLine statusLine = response.getStatusLine();
-            if (statusLine.getStatusCode() == 200) {
-                HttpEntity responseEntity = response.getEntity();
-                if (Objects.nonNull(responseEntity)) {
-                    String responseBody = EntityUtils.toString(responseEntity, "UTF-8");
-                    // log.info("getAllContent AIGC返回的数据:{}", responseBody);
-                    JSONObject jsonObject = JSONObject.parseObject(responseBody);
-                    if (jsonObject.getInteger("code") == 0) {
-                        JSONArray data = jsonObject.getJSONArray("data");
-                        result = JSONArray.parseArray(data.toJSONString(), Content.class);
-                    }
+            MediaType mediaType = MediaType.parse("application/json");
+            RequestBody body = RequestBody.create(mediaType, JSONObject.toJSONString(bodyParam));
+            Request request = new Request.Builder()
+                    .url(aigcGetAllContentUrl)
+                    .method("POST", body)
+                    .addHeader("Content-Type", "application/json;charset=UTF-8")
+                    .build();
+            Response response = client.newCall(request).execute();
+            if (response.isSuccessful()) {
+                String responseContent = response.body().string();
+                JSONObject jsonObject = JSONObject.parseObject(responseContent);
+                if (jsonObject.getInteger("code") == 0) {
+                    JSONArray data = jsonObject.getJSONArray("data");
+                    result = JSONArray.parseArray(data.toJSONString(), Content.class);
                 }
             }
         } catch (Exception e) {