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

加入future过期检测

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