springmvc使用aop异步更新solr中的数据

继续项目中如何使用solr

今天分享一下,用户在新增,修改文章是,采用aop来更新solr中的数据,对文章本来的逻辑和solr逻辑进行解耦

如果没有aop,solr的使用情况可能是这样的

这里写图片描述

这样就会把文章自身的逻辑和solr紧紧地耦合在一起。

这种情况下就非常适合用aop技术了

思路

在文章新增或修改完成之后,有一个返回值,就是修改的文章

采用aop在文章修改后去更新solr

文章新增,修改

package com.jcms.controller.admin;


/**
 * 文章控制器
 * 
 * @author 程高伟
 *
 */
@Controller
@RequestMapping("/admin/article")
public class ArticleController {
    /**
     * 添加,修改文章
     * 
     * @param article
     * @return
     * @throws Exception
     */
    @ResponseBody
    @RequestMapping(value = "/addArticle", method = RequestMethod.POST)
    public String addArticle(Article article, Boolean useImage,
            @RequestParam(required = false, value = "file") MultipartFile file, HttpServletRequest request)
            throws Exception {
        // 各种判断
        article = articleService.save(article);
        // aop中会用到这里的返回值
        return BaseReturn.response(ErrorCode.SUCCESS, article);
    }
}

文章aop

package com.jcms.aspect;


/**
 * 文章切面
 * 
 * @author 程高伟
 * @time 2017年5月17日下午4:57:36
 */
@Aspect
@Component
public class ArticleAspect {
    private final Logger logger = LoggerFactory.getLogger(this.getClass());
    @Autowired
    private ArticleService articleService;
    @Autowired
    private SolrService solrService;

    // 更新连接点
    @Pointcut("execution (* com.jcms.controller.admin.ArticleController.addArticle(..))")
    public void update() {
    }

    /**
     * 文章新增、修改都要更新solr服务器的数据
     * 
     * @param joinPoint 连接点
     * @param object 返回值
     * @throws UnsupportedEncodingException
     */

    @AfterReturning(returning = "object", pointcut = "update()")
    public void updateAfterReturning(JoinPoint joinPoint, Object object) throws UnsupportedEncodingException {
        // 请求参数
        logger.info("args={}", joinPoint.getArgs());
        // 方法返回值
        logger.info("response={}", object);
        JSONObject jsonObj = new JSONObject(object.toString());
        String code = (String) jsonObj.get("code");
        if (StringUtils.isNotBlank(code)) {
            if (code.equals("200")) {// 执行成功
                JSONObject articleJsonObj = jsonObj.getJSONObject("result");
                if (articleJsonObj != null) {
                    String id = (String) articleJsonObj.get("id");
                    if(StringUtils.isNotBlank(id)){// 有id
                        Article article = articleService.findById(id);
                        if(article.getNeedReview()==0||article.getReview()==1){// 不需要审核的文章和审核通过的文章
                            ArticleSolr articleSolr = new ArticleSolr();
                            BeanUtils.copyProperties(article, articleSolr);
                            logger.info("更新solr,更新的内容:articleSolr={}", articleSolr);
                            System.out.println("异步调用开始");
                            solrService.updateArticle(articleSolr);
                            System.out.println("异步调用结束");
                        }

                    }

                }
            }
        }
    }
}

还要在springmvc的xml配置文件中开始

<!-- aop -->
<aop:aspectj-autoproxy proxy-target-class="true"/>

这样在方法返回的时候就能拿到数据,然后去更新solr

这里写图片描述

因为我们的solr在单独的服务器,所以为了减少延迟,这里

solrService.updateArticle(articleSolr);

采用异步的方式执行

异步

异步执行也是在spring管理之下

所以我们对SolrUtil进行了一次包装,让它作为spring的bean

@Service
public class SolrService {
    @Async
    public void updateArticle(ArticleSolr article) {
        SolrUtil.saveSolrResource(article);
    }
}

我们还要开启异步,在配置文件中添加如下代码

<!-- 异步 -->
<task:annotation-driven executor="asyncExecutor" />  
<task:executor id="asyncExecutor" pool-size="100-10000" queue-capacity="10" />

这里写图片描述

这样对solr的操作就和文章本身的操作解耦了。

猜你喜欢

转载自blog.csdn.net/frankcheng5143/article/details/72478940
今日推荐