这个异步调用方法中传入一个final 回调对象。

    public void invokeAsyncImpl(final Channel channel, final RemotingCommand request, final long timeoutMillis,
final InvokeCallback invokeCallback)
throws InterruptedException, RemotingTooMuchRequestException, RemotingTimeoutException, RemotingSendRequestException {
System.out.println("NettyRemotingAbstract.invokeAsyncImpl()****************");
final int opaque = request.getOpaque();
//超时信号量锁
boolean acquired = this.semaphoreAsync.tryAcquire(timeoutMillis, TimeUnit.MILLISECONDS);
if (acquired) {
final SemaphoreReleaseOnlyOnce once = new SemaphoreReleaseOnlyOnce(this.semaphoreAsync); //这个ResonseFuture的封装的思路。
final ResponseFuture responseFuture = new ResponseFuture(opaque, timeoutMillis, invokeCallback, once);
this.responseTable.put(opaque, responseFuture);
try {
//----》执行io请求,添加channelfuture监听器
channel.writeAndFlush(request).addListener(new ChannelFutureListener() {
@Override
public void operationComplete(ChannelFuture f) throws Exception {
//这里只是发送成功!!!!!!!!!!!!!!!!!!!!!!!!!
if (f.isSuccess()) {
responseFuture.setSendRequestOK(true);
return;
} else {
responseFuture.setSendRequestOK(false);
}
//这里只是发送成功,返回设为nul。putResponse -> NULL
responseFuture.putResponse(null);
responseTable.remove(opaque);
try {
//发送成功后还没有返回消息时的,调用回调方法。参见:MQClientAPIImpl.sendMessageAsync(..)
System.out.println("**************NettyRemotingAbstract.invokeAsyncImpl().callback()");
responseFuture.executeInvokeCallback();
} catch (Throwable e) {
plog.warn("excute callback in writeAndFlush addListener, and callback throw", e);
} finally {
responseFuture.release();
} plog.warn("send a request command to channel <{}> failed.", RemotingHelper.parseChannelRemoteAddr(channel));
}
});
} catch (Exception e) {
responseFuture.release();
plog.warn("send a request command to channel <" + RemotingHelper.parseChannelRemoteAddr(channel) + "> Exception", e);
throw new RemotingSendRequestException(RemotingHelper.parseChannelRemoteAddr(channel), e);
}
} else {
String info =
String.format("invokeAsyncImpl tryAcquire semaphore timeout, %dms, waiting thread nums: %d semaphoreAsyncValue: %d", //
timeoutMillis, //
this.semaphoreAsync.getQueueLength(), //
this.semaphoreAsync.availablePermits()//
);
plog.warn(info);
throw new RemotingTooMuchRequestException(info);
}
}

我们往上面看看这个回调对象的回调方法:

RocketMQ的异步调用的更多相关文章

  1. SpringBoot异步调用--@Async详解

    1. 概述   在日常开发中,为了提高主线程的效率,往往需要采用异步调用处理,例如系统日志等.在实际业务场景中,可以使用消息中间件如RabbitMQ.RocketMQ.Kafka等来解决.假如对高可用 ...

  2. C#委托异步调用

    参考页面: http://www.yuanjiaocheng.net/webapi/mvc-consume-webapi-get.html http://www.yuanjiaocheng.net/w ...

  3. Direct3D Draw函数 异步调用原理解析

    概述 在D3D10中,一个基本的渲染流程可分为以下步骤: 清理帧缓存: 执行若干次的绘制: 通过Device API创建所需Buffer: 通过Map/Unmap填充数据到Buffer中: 将Buff ...

  4. 一个简单的webservice的demo(下)winform异步调用webservice

    绕了一大圈,又开始接触winform的项目来了,虽然很小吧.写一个winform的异步调用webservice的demo,还是简单的. 一个简单的Webservice的demo,简单模拟服务 一个简单 ...

  5. 浅析jquery ajax异步调用方法中不能给全局变量赋值的原因及解决方法(转载)

    在调用一个jquery的ajax方法时我们有时会需要该方法返回一个值或者给某个全局变量赋值,可是我们发现程序执行完后并没有获取到我们想要的值,这时很有可能是因为你用的是ajax的异步调用async:t ...

  6. tornado 异步调用系统命令和非阻塞线程池

    项目中异步调用 ping 和 nmap 实现对目标 ip 和所在网关的探测 Subprocess.STREAM 不用担心进程返回数据过大造成的死锁, Subprocess.PIPE 会有这个问题. i ...

  7. .Net组件程序设计之异步调用

    .Net组件程序设计之异步调用 说到异步调用,在脑海中首先想到就是BeginInvoke(),在一些常用对象中我们也会常常见到Invoke()和BeginInvoke(), 要想让自己的组件可以被客户 ...

  8. 谈谈RPC中的异步调用设计

    RPC(远过程调用)在分布式系统中是很常用的基础通讯手段,核心思想是将不同进程之间的通讯抽象为函数调用,基本的过程是调用端通过将参数序列化到流中并发送给服务端,服务端从流中反序列化出参数并完成实际的处 ...

  9. (转)spring boot注解 --@EnableAsync 异步调用

    原文:http://www.cnblogs.com/azhqiang/p/5609615.html EnableAsync注解的意思是可以异步执行,就是开启多线程的意思.可以标注在方法.类上. @Co ...

随机推荐

  1. 网络知识===TCP/UDP的区别

    TCP(传输控制协议,Transmission Control Protocol): 1)提供IP环境下的数据可靠传输(一台计算机发出的字节流会无差错的发往网络上的其他计算机,而且计算机A接收数据包的 ...

  2. pressmuSpiderr

    #!/usr/bin/env python # encoding: utf-8 import requests from random import choice from lxml import h ...

  3. gdrive无限网盘挂载systemd文件

    我的博客新地址:www.liuquanhao.com --------------------------------------------------------------------- 首先应 ...

  4. 某dp题

    [NOI联考by ysy]庆典 2016年6月17日1,1040 [题目描述] 战狂在昌和帝国的首都法法城召开了庆典,向一万名最杰出的士兵分发了用魔法猪做的猪肉饺子,士兵们吃了猪肉饺子后,战斗力大幅提 ...

  5. JAVA的线程学习、并发线程的学习

    http://blog.csdn.net/column/details/multithreading.html http://blog.csdn.net/fhd001/article/details/ ...

  6. Gitlab,这也就O了???

    最简单配置也是一句话搞了... rpm -i gitlab-ce--ce..el7.x86_64.rpm vim /etc/gitlab/gitlab.rb gitlab-ctl reconfigur ...

  7. (1) Flutter android studio安装

    Flutter由两部分组成 Flutter引擎和Flutter框架 Flutter引擎由C++编写,在android上通过NDK编译,在ios上通过llvm编译 Flutter框架由dart编写 1. ...

  8. Codeforces 702A Maximum Increase(dp)

    题目链接:http://codeforces.com/problemset/problem/702/A 题意: 给你N个数,a[0], a[1], a[2], ....., a[n-1],让你找出最长 ...

  9. java开发3~5年工作经验面试题

    关于java基础 String,StringBuilder,StringBuffer区别是什么?底层数据结构是什么?分别是如何实现的? HashSet的底层实现是什么?它与HashMap有什么关系? ...

  10. centos7下mail邮件的查看删除、禁止部分应用发邮件

    查看与删除 mail命令进入 & p                  #显示当前邮件& 2                  #显示标号为2的文件 & d 1-100     ...