提交 dfdf1001 编写于 作者: 智布道's avatar 智布道 👁

🐛 修复【文章搬运工】抓取文章失败的问题

上级 922f81ec
......@@ -199,36 +199,6 @@
<groupId>me.zhyd.hunter</groupId>
<artifactId>blog-hunter</artifactId>
<version>${blog-hunter.version}</version>
<exclusions>
<exclusion>
<groupId>org.hibernate.validator</groupId>
<artifactId>hibernate-validator</artifactId>
</exclusion>
<exclusion>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
</exclusion>
<exclusion>
<groupId>org.jsoup</groupId>
<artifactId>jsoup</artifactId>
</exclusion>
<exclusion>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
</exclusion>
<exclusion>
<artifactId>lombok</artifactId>
<groupId>org.projectlombok</groupId>
</exclusion>
<exclusion>
<artifactId>commons-collections</artifactId>
<groupId>commons-collections</groupId>
</exclusion>
<exclusion>
<artifactId>commons-io</artifactId>
<groupId>commons-io</groupId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>com.zyd</groupId>
......
......@@ -89,7 +89,8 @@ public class RemoverServiceImpl implements RemoverService {
private void saveArticles(Long typeId, HunterConfig config, HunterPrintWriter writerUtil, CopyOnWriteArrayList<VirtualArticle> list) {
// 获取数据库中的标签列表
Map<String, Long> originalTags = tagsService.listAll().stream().collect(Collectors.toMap(tag -> tag.getName().toUpperCase(), Tags::getId));
List<Tags> tags = tagsService.listAll();
Map<String, Long> originalTags = tags.stream().collect(Collectors.toMap(tag -> tag.getName().toUpperCase(), Tags::getId));
User user = SessionUtil.getUser();
// 添加文章到数据库
......@@ -115,6 +116,7 @@ public class RemoverServiceImpl implements RemoverService {
article.setIsMarkdown(false);
article.setDescription(virtualArticle.getDescription());
article.setKeywords(virtualArticle.getKeywords());
article.setEditorType("we");
article = articleService.insert(article);
writerUtil.print(String.format("[ save ] Succeed! <a href=\"%s\" target=\"_blank\">%s</a>", virtualArticle.getSource(), article.getTitle()));
return article;
......
package com.zyd.blog.framework.config;
import lombok.extern.slf4j.Slf4j;
import org.springframework.aop.interceptor.AsyncUncaughtExceptionHandler;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.annotation.AsyncConfigurer;
import org.springframework.scheduling.annotation.EnableAsync;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;
import org.springframework.util.concurrent.ListenableFuture;
import org.springframework.web.context.request.RequestAttributes;
import org.springframework.web.context.request.RequestContextHolder;
import java.lang.reflect.Method;
import java.util.concurrent.Callable;
import java.util.concurrent.Executor;
import java.util.concurrent.Future;
/**
* 异步线程配置
......@@ -28,58 +16,5 @@ import java.util.concurrent.Future;
@Slf4j
@Configuration
@EnableAsync
public class AsyncConfig implements AsyncConfigurer {
@Override
@Bean
public Executor getAsyncExecutor() {
return new ContextAwarePoolExecutor();
}
@Override
public AsyncUncaughtExceptionHandler getAsyncUncaughtExceptionHandler() {
return new SpringAsyncExceptionHandler();
}
class SpringAsyncExceptionHandler implements AsyncUncaughtExceptionHandler {
@Override
public void handleUncaughtException(Throwable throwable, Method method, Object... objects) {
log.error("异步线程发生异常!Method [{}],Error Message [{}]", method.getName(), throwable.getMessage());
}
}
public class ContextAwarePoolExecutor extends ThreadPoolTaskExecutor {
@Override
public <T> Future<T> submit(Callable<T> task) {
return super.submit(new ContextAwareCallable(task, RequestContextHolder.currentRequestAttributes()));
}
@Override
public <T> ListenableFuture<T> submitListenable(Callable<T> task) {
return super.submitListenable(new ContextAwareCallable(task, RequestContextHolder.currentRequestAttributes()));
}
}
public class ContextAwareCallable<T> implements Callable<T> {
private Callable<T> task;
private RequestAttributes context;
public ContextAwareCallable(Callable<T> task, RequestAttributes context) {
this.task = task;
this.context = context;
}
@Override
public T call() throws Exception {
if (context != null) {
RequestContextHolder.setRequestAttributes(context);
}
try {
return task.call();
} finally {
RequestContextHolder.resetRequestAttributes();
}
}
}
public class AsyncConfig {
}
......@@ -31,7 +31,7 @@
<java.version>1.8</java.version>
<maven.test.skip>true</maven.test.skip>
<webmagic.version>0.7.3</webmagic.version>
<jsoup.version>1.14.2</jsoup.version>
<jsoup.version>1.10.2</jsoup.version>
<javax.validation.version>2.0.1.Final</javax.validation.version>
<hibernate.validator.version>7.0.1.Final</hibernate.validator.version>
<excel4j.version>2.1.4-Final</excel4j.version>
......@@ -48,7 +48,7 @@
<useragentutils.version>1.21</useragentutils.version>
<braum.version>1.0.0-alpha</braum.version>
<hutool.version>5.5.7</hutool.version>
<spring.web.version>5.1.4.RELEASE</spring.web.version>
<spring.web.version>5.1.17.RELEASE</spring.web.version>
<aliyun.oss.version>2.8.3</aliyun.oss.version>
<blog-hunter.version>1.0.4</blog-hunter.version>
<jap.version>1.0.4</jap.version>
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册