Browse Source

飞桨解析队列排队

wh-mc-dev
parent
commit
b6c02a7e00
2 changed files with 68 additions and 17 deletions
  1. 5
      src/main/java/com/qniao/zsh/api/command/paddle/admin/PaddleAdminCommandController.java
  2. 80
      src/main/java/com/qniao/zsh/application/service/paddle/PaddleApplicationService.java

5
src/main/java/com/qniao/zsh/api/command/paddle/admin/PaddleAdminCommandController.java

@ -25,8 +25,9 @@ public class PaddleAdminCommandController {
@ApiOperation("飞桨文本解析")
@PostMapping("/analysis")
public PaddleVo analysis(@RequestBody PaddleDto paddleDto) throws IOException, InterruptedException {
return paddleApplicationService.runPaddle(paddleDto);
public int analysis(@RequestBody PaddleDto paddleDto) throws IOException, InterruptedException {
paddleApplicationService.runPaddle(paddleDto);
return 0;
}

80
src/main/java/com/qniao/zsh/application/service/paddle/PaddleApplicationService.java

@ -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);
}
}
Loading…
Cancel
Save