提交 15f5304f authored 作者: zhengfg's avatar zhengfg

加入future过期检测

上级 299d967d
...@@ -6,6 +6,7 @@ import java.io.IOException; ...@@ -6,6 +6,7 @@ import java.io.IOException;
import java.net.URISyntaxException; import java.net.URISyntaxException;
import java.util.Map; import java.util.Map;
import java.util.concurrent.ExecutionException; import java.util.concurrent.ExecutionException;
import java.util.concurrent.TimeoutException;
/** /**
* 商品爬虫接口 * 商品爬虫接口
...@@ -17,6 +18,6 @@ public interface IItemSpider { ...@@ -17,6 +18,6 @@ public interface IItemSpider {
* @param targetUrl * @param targetUrl
* @return * @return
*/ */
JSONObject captureItem(String targetUrl) throws URISyntaxException, IOException, ExecutionException, InterruptedException; JSONObject captureItem(String targetUrl) throws URISyntaxException, IOException, ExecutionException, InterruptedException, TimeoutException;
} }
...@@ -34,6 +34,7 @@ import java.util.List; ...@@ -34,6 +34,7 @@ import java.util.List;
import java.util.Map; import java.util.Map;
import java.util.concurrent.ExecutionException; import java.util.concurrent.ExecutionException;
import java.util.concurrent.Future; import java.util.concurrent.Future;
import java.util.concurrent.TimeoutException;
/** /**
* 淘宝网数据爬虫 * 淘宝网数据爬虫
...@@ -46,7 +47,7 @@ public class TbItemSpider implements IItemSpider { ...@@ -46,7 +47,7 @@ public class TbItemSpider implements IItemSpider {
private static final String taobaoUrl="https://item.taobao.com/item.htm?"; private static final String taobaoUrl="https://item.taobao.com/item.htm?";
@Override @Override
public JSONObject captureItem(String targetUrl) throws URISyntaxException, IOException, ExecutionException, InterruptedException { public JSONObject captureItem(String targetUrl) throws URISyntaxException, IOException, ExecutionException, InterruptedException, TimeoutException {
//主要是判断淘宝的url是手机端还是PC端,如果是手机端,那么还需要提取相关参数,组成新的url;还有去除链接中一些会引起请求错误的参数 //主要是判断淘宝的url是手机端还是PC端,如果是手机端,那么还需要提取相关参数,组成新的url;还有去除链接中一些会引起请求错误的参数
targetUrl=processUrl(targetUrl); targetUrl=processUrl(targetUrl);
List<Map<String, Object>> futureList= new ArrayList<>(); List<Map<String, Object>> futureList= new ArrayList<>();
......
...@@ -19,6 +19,7 @@ import java.util.ArrayList; ...@@ -19,6 +19,7 @@ import java.util.ArrayList;
import java.util.List; import java.util.List;
import java.util.Map; import java.util.Map;
import java.util.concurrent.ExecutionException; import java.util.concurrent.ExecutionException;
import java.util.concurrent.TimeoutException;
/** /**
* 天猫数据爬虫 * 天猫数据爬虫
...@@ -31,7 +32,7 @@ public class TmItemSpider implements IItemSpider { ...@@ -31,7 +32,7 @@ public class TmItemSpider implements IItemSpider {
private static final String tmallUrl="https://detail.m.tmall.com/item.htm?"; private static final String tmallUrl="https://detail.m.tmall.com/item.htm?";
@Override @Override
public JSONObject captureItem(String targetUrl) throws URISyntaxException, IOException, ExecutionException, InterruptedException { public JSONObject captureItem(String targetUrl) throws URISyntaxException, IOException, ExecutionException, InterruptedException, TimeoutException {
//主要获取id参数,组成新链接 //主要获取id参数,组成新链接
//targetUrl=processUrl(targetUrl); //targetUrl=processUrl(targetUrl);
List<Map<String, Object>> futureList= new ArrayList<>(); List<Map<String, Object>> futureList= new ArrayList<>();
......
...@@ -16,6 +16,7 @@ import java.net.URISyntaxException; ...@@ -16,6 +16,7 @@ import java.net.URISyntaxException;
import java.net.URLDecoder; import java.net.URLDecoder;
import java.util.Map; import java.util.Map;
import java.util.concurrent.ExecutionException; import java.util.concurrent.ExecutionException;
import java.util.concurrent.TimeoutException;
/** /**
* 爬取网络相关数据Controller * 爬取网络相关数据Controller
...@@ -34,7 +35,7 @@ public class SpiderController { ...@@ -34,7 +35,7 @@ public class SpiderController {
@ApiOperation("获取商品详情") @ApiOperation("获取商品详情")
@PostMapping("/item/detail") @PostMapping("/item/detail")
@ResponseBody @ResponseBody
public Result getItemDetail(@ApiParam("targetUrl 需要先进行url编码") @RequestBody DetailParamVo detailParamVo) throws InterruptedException, ExecutionException, URISyntaxException, IOException { public Result getItemDetail(@ApiParam("targetUrl 需要先进行url编码") @RequestBody DetailParamVo detailParamVo) throws InterruptedException, ExecutionException, URISyntaxException, IOException, TimeoutException {
String targetUrl= URLDecoder.decode(detailParamVo.getTargetUrl(),"UTF-8"); String targetUrl= URLDecoder.decode(detailParamVo.getTargetUrl(),"UTF-8");
JSONObject itemMap=spiderService.getItemDetail(targetUrl); JSONObject itemMap=spiderService.getItemDetail(targetUrl);
Result result=new Result(itemMap,"商品规格信息"); Result result=new Result(itemMap,"商品规格信息");
......
...@@ -7,6 +7,7 @@ import java.math.BigDecimal; ...@@ -7,6 +7,7 @@ import java.math.BigDecimal;
import java.net.URISyntaxException; import java.net.URISyntaxException;
import java.util.Map; import java.util.Map;
import java.util.concurrent.ExecutionException; import java.util.concurrent.ExecutionException;
import java.util.concurrent.TimeoutException;
public interface SpiderService { public interface SpiderService {
/** /**
...@@ -14,7 +15,7 @@ public interface SpiderService { ...@@ -14,7 +15,7 @@ public interface SpiderService {
* @param targetUrl * @param targetUrl
* @return * @return
*/ */
JSONObject getItemDetail(String targetUrl) throws InterruptedException, IOException, ExecutionException, URISyntaxException; JSONObject getItemDetail(String targetUrl) throws InterruptedException, IOException, ExecutionException, URISyntaxException, TimeoutException;
/** /**
......
...@@ -17,12 +17,13 @@ import java.net.URISyntaxException; ...@@ -17,12 +17,13 @@ import java.net.URISyntaxException;
import java.nio.charset.Charset; import java.nio.charset.Charset;
import java.util.*; import java.util.*;
import java.util.concurrent.ExecutionException; import java.util.concurrent.ExecutionException;
import java.util.concurrent.TimeoutException;
@Service("spiderService") @Service("spiderService")
public class SpiderServiceImpl implements SpiderService { public class SpiderServiceImpl implements SpiderService {
@Override @Override
public JSONObject getItemDetail(String targetUrl) throws InterruptedException, IOException, ExecutionException, URISyntaxException { public JSONObject getItemDetail(String targetUrl) throws InterruptedException, IOException, ExecutionException, URISyntaxException, TimeoutException {
//判断链接属于哪个平台 //判断链接属于哪个平台
PlatformEnum platformEnum=judgeUrlType(targetUrl); PlatformEnum platformEnum=judgeUrlType(targetUrl);
......
...@@ -11,6 +11,8 @@ import java.util.List; ...@@ -11,6 +11,8 @@ import java.util.List;
import java.util.Map; import java.util.Map;
import java.util.concurrent.ExecutionException; import java.util.concurrent.ExecutionException;
import java.util.concurrent.Future; import java.util.concurrent.Future;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.TimeoutException;
/** /**
* 腾讯翻译 * 腾讯翻译
...@@ -44,20 +46,20 @@ public class TranslateHelper { ...@@ -44,20 +46,20 @@ public class TranslateHelper {
* 等待翻译结果 * 等待翻译结果
* @param futureList * @param futureList
*/ */
public static void waitForResult(List<Map<String, Object>> futureList) throws ExecutionException, InterruptedException { public static void waitForResult(List<Map<String, Object>> futureList) throws ExecutionException, InterruptedException, TimeoutException {
for(Map<String,Object> futureMap:futureList) { for(Map<String,Object> futureMap:futureList) {
Future<Map<String,Object>> future= (Future<Map<String, Object>>) futureMap.get("future"); Future<Map<String,Object>> future= (Future<Map<String, Object>>) futureMap.get("future");
Map<String,Object> valeMap = (Map<String,Object> ) futureMap.get("value"); Map<String,Object> valeMap = (Map<String,Object> ) futureMap.get("value");
while(!future.isDone());//Future返回如果没有完成,则一直循环等待,直到Future返回完成 while(!future.isDone());//Future返回如果没有完成,则一直循环等待,直到Future返回完成
Map<String,Object> resultMap=future.get(); Map<String,Object> resultMap=future.get(10000, TimeUnit.MILLISECONDS);
String targetText="unknow"; String targetText="unknow";
if(resultMap!=null&&(int)resultMap.get("ret")==0) { if(resultMap!=null&&(int)resultMap.get("ret")==0) {
Map<String,Object> dataMap=(Map<String,Object>)resultMap.get("data"); Map<String,Object> dataMap=(Map<String,Object>)resultMap.get("data");
targetText= (String) dataMap.get("target_text"); targetText= (String) dataMap.get("target_text");
valeMap.put("translate",targetText);
} else { } else {
logger.error("翻译出错"); logger.error("翻译出错");
} }
valeMap.put("translate",targetText);
} }
} }
} }
...@@ -14,6 +14,8 @@ import java.util.List; ...@@ -14,6 +14,8 @@ import java.util.List;
import java.util.Map; import java.util.Map;
import java.util.concurrent.ExecutionException; import java.util.concurrent.ExecutionException;
import java.util.concurrent.Future; import java.util.concurrent.Future;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.TimeoutException;
/** /**
* 分词 * 分词
...@@ -48,13 +50,12 @@ public class WordposHelper { ...@@ -48,13 +50,12 @@ public class WordposHelper {
* 等待翻译结果 * 等待翻译结果
* @param futureList * @param futureList
*/ */
public static void waitForResult(List<Map<String, Object>> futureList) throws ExecutionException, InterruptedException { public static void waitForResult(List<Map<String, Object>> futureList) throws ExecutionException, InterruptedException, TimeoutException {
for(Map<String,Object> futureMap:futureList) { for(Map<String,Object> futureMap:futureList) {
Future<Map<String,Object>> future= (Future<Map<String, Object>>) futureMap.get("future"); Future<Map<String,Object>> future= (Future<Map<String, Object>>) futureMap.get("future");
Map<String,Object> valeMap = (Map<String,Object> ) futureMap.get("value"); Map<String,Object> valeMap = (Map<String,Object> ) futureMap.get("value");
while(!future.isDone());//Future返回如果没有完成,则一直循环等待,直到Future返回完成 while(!future.isDone());//Future返回如果没有完成,则一直循环等待,直到Future返回完成
Map<String,Object> resultMap=future.get(); Map<String,Object> resultMap=future.get(10000, TimeUnit.MILLISECONDS);
//logger.info(resultMap.toString());
if(resultMap!=null&&(int)resultMap.get("ret")==0) { if(resultMap!=null&&(int)resultMap.get("ret")==0) {
Map<String,Object> dataMap=(Map<String,Object>)resultMap.get("data"); Map<String,Object> dataMap=(Map<String,Object>)resultMap.get("data");
JSONArray tokens = (JSONArray) dataMap.get("base_tokens"); JSONArray tokens = (JSONArray) dataMap.get("base_tokens");
......
...@@ -42,7 +42,7 @@ public class ZionApplicationTests { ...@@ -42,7 +42,7 @@ public class ZionApplicationTests {
//private List<Map<String, Object>> futureList = new ArrayList<Map<String, Object>>(); //private List<Map<String, Object>> futureList = new ArrayList<Map<String, Object>>();
@Test @Test
public void contextLoads() throws InterruptedException, URISyntaxException, IOException, ExecutionException, StripeException { public void contextLoads() throws InterruptedException, URISyntaxException, IOException, ExecutionException, StripeException, TimeoutException {
/*Template t = config.getTemplate("email-template.ftl"); /*Template t = config.getTemplate("email-template.ftl");
Map<String,String> model=new HashMap<>(); Map<String,String> model=new HashMap<>();
model.put("Name","yonghuming"); model.put("Name","yonghuming");
......
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论