Springmvc using AOP to asynchronously update data in SOLR

Source: Internet
Author: User
Tags aop object object solr

How to continue using SOLR in your project

Share today, users in the new, modified article is, using AOP to update the data in SOLR, the original logic of the article and the SOLR logic decoupling

If there's no use of AOP,SOLR, it might be.

In this way, the logic of the article itself is tightly coupled to SOLR.

In this case, the idea of using AOP technology is very good.

After the article has been added or modified, there is a return value, which is the modified article

Use AOP to update the SOLR article after the article has been modified to add, modify

Package com.jcms.controller.admin;


/**
 * Article Controller
 * 
 * @author Cheng Gaowei
 * * *
/@Controller @RequestMapping ("/admin/article")
public class Articlecontroller {
    /**
     * Add, modify article
     * 
     * @param article
     * @return
     * @throws Exception
     *
    /@ResponseBody @RequestMapping (value = "/addarticle", method = Requestmethod.post)
    Public String addarticle (article article, Boolean Useimage,
            @RequestParam (required = false, value = "file") Multipar Tfile file, HttpServletRequest request)
            throws Exception {
        //various judgments
        Article = Articleservice.save ( article);
        The return value returned
        basereturn.response (errorcode.success, article) is used in AOP;}
}
article AOP
Package com.jcms.aspect; /** * Article section * * @author Cheng Gaowei * @time May 17, 2017 pm 4:57:36 */@Aspect @Component public class Articleaspect {privat
    E final Logger Logger = Loggerfactory.getlogger (This.getclass ());
    @Autowired private Articleservice Articleservice;

    @Autowired private Solrservice Solrservice;
    Update connection point @Pointcut ("Execution (* com.jcms.controller.admin.ArticleController.addArticle (..))") public void Update () {}/** * Article new, modified to update SOLR server data * * @param joinpoint connection point * @param objec T return value * @throws unsupportedencodingexception */@AfterReturning (returning = "Object", Pointcut = "Update ()" ) public void updateafterreturning (Joinpoint joinpoint, Object object) throws Unsupportedencodingexception {/
        /Request Parameter Logger.info ("args={}", Joinpoint.getargs ());
        method returns the value Logger.info ("response={}", object);
        Jsonobject jsonobj = new Jsonobject (object.tostring ()); String COde = (String) jsonobj.get ("code");  if (Stringutils.isnotblank (code)) {if (Code.equals ("200")) {//execution succeeds Jsonobject articlejsonobj
                = Jsonobj.getjsonobject ("result");
                    if (articlejsonobj! = null) {String id = (string) articlejsonobj.get ("id");
                        if (Stringutils.isnotblank (ID)) {//has ID article Article = Articleservice.findbyid (ID); if (Article.getneedreview () ==0| |
                            Article.getreview () ==1) {//Do not need to review articles and approve articles articlesolr ARTICLESOLR = new ARTICLESOLR ();
                            Beanutils.copyproperties (article, ARTICLESOLR);
                            Logger.info ("Update SOLR, updated content: articlesolr={}", ARTICLESOLR);
                            SYSTEM.OUT.PRINTLN ("Asynchronous invocation start");
                            Solrservice.updatearticle (ARTICLESOLR);
                        System.out.println ("End of Asynchronous call");

            }        }

                }
            }
        }
    }
} 

Also start in the XML configuration file for Springmvc

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

This will get the data when the method returns and then go to update SOLR

Because our SOLR is on a separate server, so in order to reduce latency, here

Solrservice.updatearticle (ARTICLESOLR);

Asynchronous execution of asynchronous methods

Asynchronous execution is also under spring management

So we had a wrapper over the solrutil to make it a spring bean.

@Service public
class Solrservice {
    @Async public
    void Updatearticle (Articlesolr article) {
        Solrutil.savesolrresource (article);
    }
}

We also want to turn on async and add the following code to the configuration file

<!--asynchronous--
<task:annotation-driven executor= "Asyncexecutor"/>  
<task:executor id= " Asyncexecutor "pool-size=" 100-10000 "queue-capacity="/>

This allows the operation of SOLR to be decoupled from the operation of the article itself.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.