(1)在xxl-job-admin中新建执行器和任务
新建执行器:leadnews-hot-article-executor
新建任务:路由策略为轮询,Cron表达式:0 0 2 * * ? 每天凌晨2点
(2)leadnews-article中集成xxl-job
导入依赖、config、修改nacos配置文件
(3)创建定时任务
package com.heima.article.job; import com.heima.article.service.HotArticleService; import com.xxl.job.core.handler.annotation.XxlJob; import lombok.extern.slf4j.Slf4j; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Component; @Component @Slf4j public class ComputeHotArticleJob { @Autowired private HotArticleService hotArticleService; @XxlJob("computeHotArticleJob") public void handle(){ log.info("热文章分值计算任务调度开始执行"); hotArticleService.computeHotArticle(); log.info("热文章分值计算任务调度任务结束"); } }
查询文章接口改造:
在aparticleservie中添加从缓存中加载热文章数据的load2方法,在controller的加载首页改成调用load2方法。
/** * 加载文章列表 * * @param dto * @param type 1:加载更多 2:加载最新 * @param firstPage * @return * @Param firstPage 是否是首页,true加载redis,false调用load加载 */ @Override public ResponseResult load2(ArticleHomeDto dto, Short type, boolean firstPage) { if(firstPage){ //是首页,从redis中获取数据 String jsonStr = cacheService.get(ArticleConstants.HOT_ARTICLE_FIRST_PAGE + dto.getTag()); if(StringUtils.isNotBlank(jsonStr)){ List<HotArticleVo> hotArticleVoList = JSON.parseArray(jsonStr, HotArticleVo.class); ResponseResult responseResult = ResponseResult.okResult(hotArticleVoList); return responseResult; } } //不是首页,调用load从数据库加载 return load(dto, type); }
@Autowired private ApArticleService apArticleService; /** * 加载首页 * @param dto * @return */ @PostMapping("/load") public ResponseResult load(@RequestBody ArticleHomeDto dto){ // return apArticleService.load(dto, ArticleConstants.LOADTYPE_LOAD_MORE); return apArticleService.load2(dto, ArticleConstants.LOADTYPE_LOAD_MORE, true); }
标签:load,return,import,任务,首页,文章,dto,定时,加载 From: https://www.cnblogs.com/fxzm/p/17551499.html