提交 74030ed5 编写于 作者: 孔冲

内置队列代替redis队列

上级 0ca8ffc3
...@@ -3,14 +3,14 @@ package com.zyd.blog.core.schedule; ...@@ -3,14 +3,14 @@ package com.zyd.blog.core.schedule;
import com.zyd.blog.business.entity.ArticleLook; import com.zyd.blog.business.entity.ArticleLook;
import com.zyd.blog.business.service.BizArticleLookService; import com.zyd.blog.business.service.BizArticleLookService;
import com.zyd.blog.business.service.BizArticleService; import com.zyd.blog.business.service.BizArticleService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j; import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.BoundListOperations;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component; import org.springframework.stereotype.Component;
import javax.annotation.PostConstruct; import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.BlockingQueue;
/** /**
* @author yadong.zhang (yadong.zhang0415(a)gmail.com) * @author yadong.zhang (yadong.zhang0415(a)gmail.com)
...@@ -21,20 +21,14 @@ import javax.annotation.PostConstruct; ...@@ -21,20 +21,14 @@ import javax.annotation.PostConstruct;
*/ */
@Slf4j @Slf4j
@Component @Component
@RequiredArgsConstructor
public class ArticleLookTask { public class ArticleLookTask {
@Autowired
private BizArticleService bizArticleService;
@Autowired
private BizArticleLookService articleLookService;
@Autowired
private RedisTemplate redisTemplate;
private BoundListOperations<String, ArticleLook> listOperations; private final BizArticleService bizArticleService;
@PostConstruct private final BizArticleLookService articleLookService;
public void init() {
this.listOperations = redisTemplate.boundListOps("article-look-cache"); private BlockingQueue<ArticleLook> queue = new ArrayBlockingQueue<>(1024);
}
/** /**
* 保存文章的浏览记录,先进先出 * 保存文章的浏览记录,先进先出
...@@ -43,22 +37,32 @@ public class ArticleLookTask { ...@@ -43,22 +37,32 @@ public class ArticleLookTask {
if (null == articleLook) { if (null == articleLook) {
return; return;
} }
listOperations.rightPush(articleLook); queue.offer(articleLook);
} }
/**
* 每分钟保存一次文章的浏览记录
*/
@Scheduled(cron = "0 0/1 * * * ? ")
// @Scheduled(cron = "0/5 * * * * ? ")
public void save() { public void save() {
ArticleLook articleLook = null; List<ArticleLook> bufferList = new ArrayList<>();
while (null != (articleLook = listOperations.leftPop())) { while (true) {
if (!bizArticleService.isExist(articleLook.getArticleId())) { try {
log.warn("{}-该文章不存在!", articleLook.getArticleId()); bufferList.add(queue.take());
return; for (ArticleLook articleLook : bufferList) {
if (!bizArticleService.isExist(articleLook.getArticleId())) {
log.warn("{}-该文章不存在!", articleLook.getArticleId());
continue;
}
articleLookService.insert(articleLook);
}
} catch (InterruptedException e) {
log.error("保存文章浏览记录失败--->[{}]", e.getMessage());
// 防止缓冲队列填充数据出现异常时不断刷屏
try {
Thread.sleep(1000);
} catch (Exception err) {
log.error(err.getMessage());
}
} finally {
bufferList.clear();
} }
articleLookService.insert(articleLook);
} }
} }
......
package com.zyd.blog.runner;
import com.zyd.blog.core.schedule.ArticleLookTask;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.ApplicationArguments;
import org.springframework.boot.ApplicationRunner;
import org.springframework.stereotype.Component;
/**
* 执行保存文章浏览记录任务
*
* @author kongchong
* date: 2019-07-10 11:17
*/
@Component
public class TaskRunner implements ApplicationRunner {
@Autowired
private ArticleLookTask articleLookTask;
@Override
public void run(ApplicationArguments args) {
articleLookTask.save();
}
}
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册