|
|
|
@ -3,14 +3,19 @@ package com.qniao.zsh.application.service.paddle; |
|
|
|
import cn.hutool.core.util.StrUtil; |
|
|
|
import cn.hutool.json.JSONObject; |
|
|
|
import cn.hutool.json.JSONUtil; |
|
|
|
import com.google.common.util.concurrent.RateLimiter; |
|
|
|
import com.qniao.zsh.api.command.paddle.admin.request.PaddleDto; |
|
|
|
import com.qniao.zsh.api.command.paddle.admin.response.PaddleVo; |
|
|
|
import com.qniao.zsh.infrastructure.config.SpiderDirectoryConfig; |
|
|
|
import org.mc.ddd.infrastructure.util.ScriptRunnerHelper; |
|
|
|
import org.springframework.scheduling.annotation.Async; |
|
|
|
import org.springframework.stereotype.Service; |
|
|
|
|
|
|
|
import java.io.IOException; |
|
|
|
import java.util.List; |
|
|
|
import java.util.concurrent.LinkedBlockingQueue; |
|
|
|
import java.util.concurrent.ThreadPoolExecutor; |
|
|
|
import java.util.concurrent.TimeUnit; |
|
|
|
|
|
|
|
/** |
|
|
|
* @author wh |
|
|
|
@ -19,24 +24,69 @@ import java.util.List; |
|
|
|
@Service |
|
|
|
public class PaddleApplicationService { |
|
|
|
|
|
|
|
public PaddleVo runPaddle(PaddleDto paddleDto) throws IOException, InterruptedException { |
|
|
|
// 控制每10秒最多处理1个请求,平均速率为0.1个每秒 |
|
|
|
private static final RateLimiter rateLimiter = RateLimiter.create(0.1); |
|
|
|
|
|
|
|
/** |
|
|
|
* 请求队列 |
|
|
|
*/ |
|
|
|
private static final LinkedBlockingQueue<Runnable> queue = new LinkedBlockingQueue<>(); |
|
|
|
|
|
|
|
/** |
|
|
|
* 线程池 |
|
|
|
*/ |
|
|
|
private static final ThreadPoolExecutor executor = new ThreadPoolExecutor(1, 1, 0L, TimeUnit.MILLISECONDS, queue); |
|
|
|
|
|
|
|
// @Async |
|
|
|
public void runPaddle(PaddleDto paddleDto) throws IOException, InterruptedException { |
|
|
|
if (rateLimiter.tryAcquire(10, TimeUnit.SECONDS)) { |
|
|
|
// 如果当前令牌桶中有足够的令牌,则可以继续处理请求 |
|
|
|
// 十秒钟只处理一个请求 |
|
|
|
|
|
|
|
// 执行业务逻辑 |
|
|
|
executor.execute(() -> { |
|
|
|
// 执行耗时的业务逻辑 |
|
|
|
|
|
|
|
try { |
|
|
|
String result = String.valueOf(ScriptRunnerHelper.handle("python", |
|
|
|
SpiderDirectoryConfig.PADDLEDIR, |
|
|
|
paddleDto.getTitle(), |
|
|
|
paddleDto.getArticleId(), |
|
|
|
paddleDto.getPaperMillId(), |
|
|
|
paddleDto.getContent())); |
|
|
|
} catch (Exception e) { |
|
|
|
e.printStackTrace(); |
|
|
|
} |
|
|
|
}); |
|
|
|
} else { |
|
|
|
// 如果当前请求超出了速率限制,则将其排队等待处理 |
|
|
|
queue.add(() -> { |
|
|
|
try { |
|
|
|
runPaddle(paddleDto); |
|
|
|
} catch (IOException | InterruptedException e) { |
|
|
|
throw new RuntimeException(e); |
|
|
|
} |
|
|
|
}); |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
//linux |
|
|
|
// String result = ScriptRunnerHelper.handle("python3", |
|
|
|
//win |
|
|
|
String result = String.valueOf(ScriptRunnerHelper.handle("python", |
|
|
|
SpiderDirectoryConfig.PADDLEDIR, |
|
|
|
paddleDto.getTitle(), |
|
|
|
paddleDto.getArticleId(), |
|
|
|
paddleDto.getPaperMillId(), |
|
|
|
paddleDto.getContent())); |
|
|
|
String[] strings = result.split("\n"); |
|
|
|
result = strings[strings.length - 1]; |
|
|
|
JSONObject jsonResult = JSONUtil.parseObj(result); |
|
|
|
PaddleVo paddleVo = new PaddleVo(); |
|
|
|
paddleVo.setFactory(jsonResult.getStr("factory")); |
|
|
|
paddleVo.setPaperInfo((List<String>) jsonResult.getObj("paper_info")); |
|
|
|
paddleVo.setPaperPrice((List<String>) jsonResult.getObj("paper_price")); |
|
|
|
return paddleVo; |
|
|
|
// String result = String.valueOf(ScriptRunnerHelper.handle("python", |
|
|
|
// SpiderDirectoryConfig.PADDLEDIR, |
|
|
|
// paddleDto.getTitle(), |
|
|
|
// paddleDto.getArticleId(), |
|
|
|
// paddleDto.getPaperMillId(), |
|
|
|
// paddleDto.getContent())); |
|
|
|
// String[] strings = result.split("\n"); |
|
|
|
// result = strings[strings.length - 1]; |
|
|
|
// JSONObject jsonResult = JSONUtil.parseObj(result); |
|
|
|
// PaddleVo paddleVo = new PaddleVo(); |
|
|
|
// paddleVo.setFactory(jsonResult.getStr("factory")); |
|
|
|
// paddleVo.setPaperInfo((List<String>) jsonResult.getObj("paper_info")); |
|
|
|
// paddleVo.setPaperPrice((List<String>) jsonResult.getObj("paper_price")); |
|
|
|
// System.out.println(result); |
|
|
|
} |
|
|
|
|
|
|
|
} |