首页
学习
活动
专区
工具
TVP
发布
精选内容/技术社群/优惠产品,尽在小程序
立即前往
您找到你想要的搜索结果了吗?
是的
没有找到

阿里巴巴为什么不建议直接使用Async注解?

异步:异步调用则是只是发送了调用的指令,调用者无需等待被调用的方法完全执行完毕;而是继续执行下面的流程。例如, 在某个调用中,需要顺序调用 A, B, C三个过程方法;如他们都是同步调用,则需要将他们都顺序执行完毕之后,方算作过程执行完毕;如B为一个异步的调用方法,则在执行完A之后,调用B,并不等待B完成,而是执行开始调用C,待C执行完毕之后,就意味着这个过程执行完毕了。在Java中,一般在处理类似的场景之时,都是基于创建独立的线程去完成相应的异步调用逻辑,通过主线程和不同的业务子线程之间的执行流程,从而在启动独立的线程之后,主线程继续执行而不会产生停滞等待的情况。

01

motan之异步调用

一、什么是异步调用?  1.同步调用 方法间的调用,假设A方法调用B方法,A方法等待B方法执行完毕后才执行本身,这个同步调用,是具有阻塞式的调用,如果B方法非常耗时,那么整个方法的执行效率将会非常低; 2.异步调用 同样是方法间的调用,假设A方法调用B方法,不同的是A方法调用B方法后,B方法很快的返回给A方法个答复(这个答复不是执行完整个B方法的答复),A方法收到答复后就执行本身,这个是异步调用,不管B方法是否耗时,整体的效率都提升。 二、motan的异步调用入门 1.首先,以入门案例为基础案例改造:http://www.cnblogs.com/Json1208/p/8784906.html 2.motan-api工程HelloWorldService添加注解@MotanAsync 复制代码 package com.motan.service; import com.weibo.api.motan.transport.async.MotanAsync; @MotanAsync public interface HelloWorldService {     String hello(String name); } 复制代码 3.motan-api添加maven插件build-helper-maven-plugin,用来把自动生成类的目录设置为source path 复制代码 <build>         <plugins>             <plugin>                 <groupId>org.codehaus.mojo</groupId>                 <artifactId>build-helper-maven-plugin</artifactId>                 <version>1.10</version>                 <executions>                     <execution>                         <phase>generate-sources</phase>                         <goals>                             <goal>add-source</goal>                         </goals>                         <configuration>                             <sources>                                 <source>${project.build.directory}/generated-sources/annotations</source>                             </sources>                         </configuration>                     </execution>                 </executions>             </plugin>         </plugins>     </build> 复制代码 编译时,Motan自动生成异步service类,生成路径为target/generated-sources/annotations/,生成的类名为service名加上Async,例如service类名为HelloWorldService.java,则自动生成的类名为HelloWorldServiceAsync.java。 另外,需要将motan自动生产类文件的路径配置为项目source path,可以使用maven plugin或手动配置,以上使用maven plugin方式。 这样,我们就能在eclipse中的source folder 中生成HelloWorldServiceAsync.java。 4.motan-client.xml配置的motan:referer标签中配置interface为自动生成的以Async为后缀的对应service类 <motan:referer id="helloWorldReferer" interface="com.motan.service.HelloWorldServiceAsync" directUrl="localhost:8002"/> 5.测试,先启动server,再启动client 复制代码 public class Server {     @SuppressWarnings({ "unused", "resource" })

01
领券