dubbo 异步回调
dubbo 异步回调的使用
业务接口:
public interface HelloService {
String sayHello();
void sayHi(String name);
}
回调服务类:
public class CbService {
// onreturn 函数的参数是有限定的,细节下面提及。
public void onreturn(String str) {
System.out.println("onreturn:" + str);
}
public void onthrow(Throwable ex) {
System.out.println("onthrow");
}
public String oninvoke() {
System.out.println("oninvoke");
return null;
}
}
xml 配置:(oninvoke 暂时没有配置好)
<bean id="cbService" class="com.zhang.CbService"></bean>
<dubbo:reference id="hello"
loadbalance="roundrobin" check="false" interface="com.zhang.HelloService" >
<dubbo:method name="sayHello"
async="true"
onreturn="cbService.onreturn"
onthrow="cbService.onthrow">
</dubbo:method>
</dubbo:reference>
1. 异步回调的源头从 FutureFilter 发起,这是一个 consumer 端的 filter,它为 Future 设置回调函数。
@Activate(group = Constants.CONSUMER)
public class FutureFilter implements Filter { public Result invoke(final Invoker<?> invoker, final Invocation invocation) throws RpcException {
final boolean isAsync = RpcUtils.isAsync(invoker.getUrl(), invocation); fireInvokeCallback(invoker, invocation);
// 发起调用请求
Result result = invoker.invoke(invocation); if (isAsync) {
// 注册异步回调函数
asyncCallback(invoker, invocation);
} else {
syncCallback(invoker, invocation, result);
}
return result;
} // 为 Future 设置 callback
private void asyncCallback(final Invoker<?> invoker, final Invocation invocation) {
Future<?> f = RpcContext.getContext().getFuture();
if (f instanceof FutureAdapter) {
ResponseFuture future = ((FutureAdapter<?>)f).getFuture();
future.setCallback(new ResponseCallback() {
public void done(Object rpcResult) {
if (rpcResult == null){
logger.error(new IllegalStateException("invalid result value : null, expected "+Result.class.getName()));
return;
}
///must be rpcResult
if (! (rpcResult instanceof Result)){
logger.error(new IllegalStateException("invalid result type :" + rpcResult.getClass() + ", expected "+Result.class.getName()));
return;
}
Result result = (Result) rpcResult;
if (result.hasException()) {
fireThrowCallback(invoker, invocation, result.getException());
} else {
fireReturnCallback(invoker, invocation, result.getValue());
}
}
public void caught(Throwable exception) {
fireThrowCallback(invoker, invocation, exception);
}
});
}
} private void fireReturnCallback(final Invoker<?> invoker, final Invocation invocation, final Object result) {
final Method onReturnMethod = (Method)StaticContext.getSystemContext().get(StaticContext.getKey(invoker.getUrl(), invocation.getMethodName(), Constants.ON_RETURN_METHOD_KEY));
final Object onReturnInst = StaticContext.getSystemContext().get(StaticContext.getKey(invoker.getUrl(), invocation.getMethodName(), Constants.ON_RETURN_INSTANCE_KEY)); //not set onreturn callback
if (onReturnMethod == null && onReturnInst == null ){
return ;
} if (onReturnMethod == null || onReturnInst == null ){
throw new IllegalStateException("service:" + invoker.getUrl().getServiceKey() +" has a onreturn callback config , but no such "+(onReturnMethod == null ? "method" : "instance")+" found. url:"+invoker.getUrl());
}
if (onReturnMethod != null && ! onReturnMethod.isAccessible()) {
onReturnMethod.setAccessible(true);
} Object[] args = invocation.getArguments();
Object[] params ;
Class<?>[] rParaTypes = onReturnMethod.getParameterTypes() ;
if (rParaTypes.length >1 ) {
if (rParaTypes.length == 2 && rParaTypes[1].isAssignableFrom(Object[].class)){
params = new Object[2];
params[0] = result;
params[1] = args ;
}else {
params = new Object[args.length + 1];
params[0] = result;
System.arraycopy(args, 0, params, 1, args.length);
}
} else {
params = new Object[] { result };
}
// 经过前面的 if else 后,必定有 params[0] = result;
// result 是 RpcResult 的 value,即 provider 接口的返回值。
// 所以,CbService 的 onreturn 函数的第一个参数必须是接口的返回值。
try {
onReturnMethod.invoke(onReturnInst, params);
} catch (InvocationTargetException e) {
fireThrowCallback(invoker, invocation, e.getTargetException());
} catch (Throwable e) {
fireThrowCallback(invoker, invocation, e);
}
}
}
2. 当 consumer 收到服务端的响应时,触发回调函数,调用栈如下:
dubbo 异步回调的更多相关文章
- 抓到Dubbo异步调用的小BUG,再送你一个贡献开源代码的机会
hello,大家好呀,我是小楼. 最近一个技术群有同学at我,问我是否熟悉Dubbo,这我熟啊~ 他说遇到了一个Dubbo异步调用的问题,怀疑是个BUG,提到BUG我可就不困了,说不定可以水,哦不.. ...
- node 异步回调解决方法之yield
先看如何使用 使用的npm包为genny,npm 安装genny,使用 node -harmony 文件(-harmony 为使用es6属性启动参数) 启动项目 var genny= require( ...
- c#线程之异步委托begininvoke、invoke、AsyncWaitHandle.WaitOne 、异步回调
单靠自己看书学总是会走很多弯路,任何人也不列外,有些时候自己遇到的很多问题,其它别人在很久之前也可能遇到过,上网查查可以走很大捷径,对自己的学习有很大帮助,刚开始弄线程这块,一开始只是看书,很多东西都 ...
- Android异步回调中的UI同步性问题
Android程序编码过程中,回调无处不在.从最常见的Activity生命周期回调开始,到BroadcastReceiver.Service以及Sqlite等.Activity.BroadcastRe ...
- java 中的异步回调
异步回调,本来在c#中是一件极为简单和优雅的事情,想不到在java的世界里,却如此烦琐,先看下类图: 先定义了一个CallBackTask,做为外层的面子工程,其主要工作为start 开始一个异步操作 ...
- C“中断” 与 JS“异步回调” 横向对比
在底层C语言中,有一个非常重要而特别的概念,叫做“中断”.用比喻来说,我正在写着博客,突然我妈打个电话过来,我就离开了键盘去接电话了,然后写博客就中断了,我聊完电话回来再继续写.乍一听似乎并没有什么大 ...
- State Threads——异步回调的线性实现
State Threads——异步回调的线性实现 原文链接:http://coolshell.cn/articles/12012.html 本文的标题看起来有点拗口,其实State Threads库就 ...
- 委托(delegate)的三种调用方式:同步调用,异步调用,异步回调(转载)
下面为即将被调用的方法: public delegate int AddHandler(int a,int b); public class 加法类 { public static int Add(i ...
- 协程 & 用户级(内核级)线程 & 切换开销 & 协程与异步回调的差异
今天先是看到多线程级别的内容,然后又看到协程的内容. 基本的领会是,协程是对异步回调方式的一种变换,同样是在一个线程内,协程通过主动放弃时间片交由其他协程执行来协作,故名协程. 而协程很早就有了,那时 ...
随机推荐
- 使用Chrome远程调试GenyMotion上的WebView程序
WebView让我们方便的使用熟悉的Html/JS/Css来开发APP.但是,当出现问题时,却没有PC上那么方便的排查问题.PC上,前端的问题我们可以使用Chrome的开发者工具方便的调试.Andro ...
- #WEB安全基础 : HTTP协议 | 0x5 URI和URL
URI(统一资源标识符)和URL(统一资源定位符)相信大家都知道URL吧,我们看看它们有什么区别 URI 长得就像这样 /images/hackr.jepg URL 长得像这样 http://hack ...
- POJ 2533 裸的LIS
A numeric sequence of ai is ordered if a1 < a2 < ... < aN. Let the subsequence of the given ...
- Python实现链表
1.1实现单向链表 #链表结构分成2部分 head,tail #('a',('b',('c',none))) #迭代时候 Head is a ;;;; tail is ('b',('c',none)) ...
- html5 javascript 事件练习2
<!DOCTYPE html><html lang="en"><head> <meta charset="UTF-8&qu ...
- STM32的型号的命名规则
每种STM32的产品都由16个字母或数字构成的编号标示,用户向ST订货时必须使用这个编号指定需要的产品.这16个字符分为8个部分,下面通过一个例子说明它们的意义: STM32 F C T xxx ST ...
- JavaScript 原型链学习(二)原型的动态性
由于在原型中查找值的过程是一次搜索,因此我们对原型对象所做的任何修改都能够立即从实例上反映出来,即使是先创建了实例后修改原型也照样如此.如下示例: var friend = new Person(); ...
- python 猜数字游戏
import random print('==============学无止境==========') secret=random.randint(1,10) print('sec:',secret) ...
- 5、Kafka生产过程分析
1.写入方式 producer采用推(push)模式将消息发布到broker, 每条消息都被追加(append)到分区(patition)中,属于顺序写磁盘(顺序写磁盘效率比随机写内存要高,保障kaf ...
- Docker Kubernetes 介绍 or 工作原理
Kubernetes 介绍 Kubernetes是Google在2014年6月开源的一个容器集群管理系统,使用Go语言开发,Kubernetes也叫K8S. K8S是Google内部一个叫Borg的容 ...