|
@@ -1,7 +1,9 @@
|
|
package com.tzld.piaoquan.recommend.server.service;
|
|
package com.tzld.piaoquan.recommend.server.service;
|
|
|
|
|
|
|
|
+import com.google.common.base.Stopwatch;
|
|
import com.google.common.collect.Lists;
|
|
import com.google.common.collect.Lists;
|
|
import com.google.common.reflect.TypeToken;
|
|
import com.google.common.reflect.TypeToken;
|
|
|
|
+import com.tzld.piaoquan.recommend.server.util.HttpClientFactory;
|
|
import com.tzld.piaoquan.recommend.server.util.JSONUtils;
|
|
import com.tzld.piaoquan.recommend.server.util.JSONUtils;
|
|
import lombok.extern.slf4j.Slf4j;
|
|
import lombok.extern.slf4j.Slf4j;
|
|
import org.apache.http.HttpEntity;
|
|
import org.apache.http.HttpEntity;
|
|
@@ -9,7 +11,6 @@ import org.apache.http.client.methods.CloseableHttpResponse;
|
|
import org.apache.http.client.methods.HttpPost;
|
|
import org.apache.http.client.methods.HttpPost;
|
|
import org.apache.http.entity.StringEntity;
|
|
import org.apache.http.entity.StringEntity;
|
|
import org.apache.http.impl.client.CloseableHttpClient;
|
|
import org.apache.http.impl.client.CloseableHttpClient;
|
|
-import org.apache.http.impl.client.HttpClients;
|
|
|
|
import org.apache.http.util.EntityUtils;
|
|
import org.apache.http.util.EntityUtils;
|
|
import org.springframework.beans.factory.annotation.Value;
|
|
import org.springframework.beans.factory.annotation.Value;
|
|
import org.springframework.stereotype.Service;
|
|
import org.springframework.stereotype.Service;
|
|
@@ -19,6 +20,7 @@ import java.util.Collections;
|
|
import java.util.HashMap;
|
|
import java.util.HashMap;
|
|
import java.util.List;
|
|
import java.util.List;
|
|
import java.util.Map;
|
|
import java.util.Map;
|
|
|
|
+import java.util.concurrent.TimeUnit;
|
|
|
|
|
|
|
|
|
|
* @author dyp
|
|
* @author dyp
|
|
@@ -32,7 +34,8 @@ public class ViewedService {
|
|
@Value("${video.filter.url:}")
|
|
@Value("${video.filter.url:}")
|
|
private String videoFilterUrl;
|
|
private String videoFilterUrl;
|
|
|
|
|
|
- private CloseableHttpClient clients = HttpClients.createDefault();
|
|
+ private CloseableHttpClient client = HttpClientFactory.create(1000, 3000, 200, 1000, 0, 500);
|
|
|
|
+
|
|
|
|
|
|
@PostConstruct
|
|
@PostConstruct
|
|
public void init() {
|
|
public void init() {
|
|
@@ -41,18 +44,17 @@ public class ViewedService {
|
|
viewedTypesMap.put(13, Lists.newArrayList(1));
|
|
viewedTypesMap.put(13, Lists.newArrayList(1));
|
|
}
|
|
}
|
|
|
|
|
|
|
|
+
|
|
|
|
|
|
public List<Long> filterViewedVideo(int appType, String mid, String uid, List<Long> videoIds) {
|
|
public List<Long> filterViewedVideo(int appType, String mid, String uid, List<Long> videoIds) {
|
|
- List<Integer> viewedTypes = viewedTypesMap.getOrDefault(appType, defaultViewedTypes);
|
|
|
|
|
|
|
|
|
|
+ Stopwatch stopwatch = Stopwatch.createStarted();
|
|
|
|
+ List<Integer> viewedTypes = viewedTypesMap.getOrDefault(appType, defaultViewedTypes);
|
|
CloseableHttpResponse chr = null;
|
|
CloseableHttpResponse chr = null;
|
|
-
|
|
|
|
try {
|
|
try {
|
|
-
|
|
|
|
HttpPost post = new HttpPost(videoFilterUrl);
|
|
HttpPost post = new HttpPost(videoFilterUrl);
|
|
post.addHeader("Connection", "close");
|
|
post.addHeader("Connection", "close");
|
|
post.addHeader("content-type", "application/json");
|
|
post.addHeader("content-type", "application/json");
|
|
-
|
|
|
|
Map<String, Object> param = new HashMap<>();
|
|
Map<String, Object> param = new HashMap<>();
|
|
param.put("appType", appType);
|
|
param.put("appType", appType);
|
|
param.put("mid", mid);
|
|
param.put("mid", mid);
|
|
@@ -61,13 +63,13 @@ public class ViewedService {
|
|
param.put("videoIds", videoIds);
|
|
param.put("videoIds", videoIds);
|
|
post.setEntity(new StringEntity(JSONUtils.toJson(param)));
|
|
post.setEntity(new StringEntity(JSONUtils.toJson(param)));
|
|
|
|
|
|
- log.info("request param={}", JSONUtils.toJson(param));
|
|
+ log.info("request={}", JSONUtils.toJson(param));
|
|
- chr = clients.execute(post);
|
|
+ chr = client.execute(post);
|
|
|
|
+ log.info("response={}", JSONUtils.toJson(chr));
|
|
if (chr == null
|
|
if (chr == null
|
|
|| chr.getStatusLine() == null
|
|
|| chr.getStatusLine() == null
|
|
|| chr.getStatusLine().getStatusCode() != 200) {
|
|
|| chr.getStatusLine().getStatusCode() != 200) {
|
|
- log.error("filterViewedVideo failed http status exception! videoFilterUrl={}, CloseableHttpResponse={}",
|
|
+ log.error("filterViewedVideo failed http status exception!");
|
|
- videoFilterUrl, chr);
|
|
|
|
return Collections.emptyList();
|
|
return Collections.emptyList();
|
|
}
|
|
}
|
|
HttpEntity entity = chr.getEntity();
|
|
HttpEntity entity = chr.getEntity();
|
|
@@ -93,6 +95,7 @@ public class ViewedService {
|
|
} catch (Exception e) {
|
|
} catch (Exception e) {
|
|
log.error("invoke http filterViewedVideo error", e);
|
|
log.error("invoke http filterViewedVideo error", e);
|
|
} finally {
|
|
} finally {
|
|
|
|
+ log.info("filterViewedVideo cost={}", stopwatch.stop().elapsed(TimeUnit.MILLISECONDS));
|
|
try {
|
|
try {
|
|
if (chr != null) {
|
|
if (chr != null) {
|
|
chr.close();
|
|
chr.close();
|