当前位置:网站首页>There are three ways of asynchronous RPC: asynchronous call, asynchronous listening and callback call
There are three ways of asynchronous RPC: asynchronous call, asynchronous listening and callback call
2022-04-21 07:18:00 【Iron rolling Daewoo】
Now mature RPC frame Will support asynchronous calls 、 Asynchronous monitoring 、callback call , Today, let's talk about the methods and precautions of these three asynchronous methods .
Asynchronous call
One function needs to call three interfaces to realize the business requirements , These three interfaces take time as follows :A The interface takes 400ms,B The interface takes 200ms,C The interface takes 700ms.
If you use the normal synchronous call method , Then the total time to complete this function is A+B+C=1300ms, If asynchronous call is adopted , Then the total time consumption is the time consumption of the longest interface , namely 700ms. The sample code is as follows :
String resultA = service.testStrA("A");// Make a call , Return at this time null
ResponseFuture<String> futureA = RpcContext.getContext().getFuture();
// Get the first call Future
String resultB = service.testStrA("B");// Initiate secondary call , Return at this time null
ResponseFuture<String> futureB = RpcContext.getContext().getFuture();
// Get the second call Future
String resultC = service.testStrA("C");// Initiate three calls , Return at this time null
ResponseFuture<String> futureC = RpcContext.getContext().getFuture();
// Get the third call Future
// Be sure to call get Method get results , Otherwise, the result will not be consumed, resulting in memory overflow
try {
resultA = futureA.get();// Exception may be thrown
}catch (Throwable e){
}
try {
resultB = futureB.get();// Exception may be thrown
}catch (Throwable e){
}
try {
resultC = futureC.get();// Exception may be thrown
}catch (Throwable e){
}
RPC The asynchronous call of refers to that after the client initiates the request , You don't have to wait for results , The second is to return to null value , Colleagues can get a Future object , And then from Future To get results , As the code shows . In this way, the client does not need to start multiple existing achievements when calling, and can call multiple remote service interfaces in parallel .
Asynchronous monitoring
Sometimes after we make a call request , I don't want to pass Future Of get To get the results , because get It's blocked when it's , Instead, you want to do something else after invoking the request , Monitor through a monitor , When a result is returned, get the result directly , And then logic processing . For example, the following code example , There is a listening class TestResponseListener, It's inside handleResult Method is responsible for processing the result data .
public class TestResponseListener implements ResponseListener {
public void handleResult(Object result){
// Callback result received , Conduct business processing
}
public void catchExceptio(Throwable e){
// Handling of exceptions
}
}
<bean id="ResponseListener" class="com.**.listenner.TestResponseListener"/>
<xx:consumer onreturn="responseListener" async="true"/><!-- Set asynchronous And there is a callback listener -->
After sending the request, you do not need to call getFuture Method . When a result is returned , Will automatically call the pre injected TestResponseListener Method , The sample code to initiate an asynchronous call is as follows :
String result1 = service.testStr("Test");// Make a call , Return at this time null
// You no longer need to call getFuture Method , Wait for the result to return , Will automatically transfer TestResponseListener The method in handleResult
Be careful : When we use asynchronous listening , It is recommended to limit the frequency of transmission , Sending too fast will cause problems such as memory overflow .
callback call
RPC In order to TCP Full duplex protocol for communication , Based on long connection , The server can “ call ” client callback Capability of a function . yes , we have RPC The framework will also support callback Call mode , It is used as follows :
step1, Define a callback function , Add one more Callback Method parameters of type .
public interface HelloService{
public void callBackString(String request, Callback<String> resultListener);
}
step2, When called by the client, inject a Callback Object instances .
Callback callback = new Callback<String>() {
public void notify(String result) {
System.out.println("notify callback string" + result);;
}
};
service.callBackString("hello", callback);// The client uses the same... Every time Callback object , The server also gets a Callback object
step3, In the server code , Realize to callback Call to .
public class HelloServiceImpl implements HelloService {
public void callBackString(String request, Callback resultListener) {
resultListener.notify("callback from server");
}
}
If a business logic function is completed in the server interface, there are 3 A process , So this 3 Each procedure can be called separately callback Method , formation “ One call , Notice many times ” The mechanism of , There is no way to achieve this in asynchronous listening , Asynchronous callbacks are more like “ A one-off sale ”. In high concurrency scenarios, it is recommended to use asynchronous listening , because callback The client will respond to Callback The number of instances is limited .
版权声明
本文为[Iron rolling Daewoo]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/04/202204210615136295.html
边栏推荐
猜你喜欢

高级系统设置点击无反应,打不开的解决办法

Swagger2生成Api文档

格式检查工具eslint

C语言版:二叉树的前序,中序,后序非递归遍历实现

Add parentheses to Boolean expressions for short circuit operators

C语言版:二叉树的动态建立

Reflection cannot find the class message classnotfound of UDF when executing flinksql code

Simultaneous access of computer intranet and extranet - solution

云计算中存储继承知识

图形学基础|基于LTC的面光源渲染
随机推荐
Analysis of distributed lock principle code using redis
【无标题】数据库——《限制返回行数》
IDE常用快捷键
好用的数据集和开源网络对比网站
数据异构方案
[LabVIEW] record some pits in LabVIEW project
Implement an array in the form of JS function prototype. forEach(),. map(),. filter()
Chapter 5 support vector machine (SVM)
2020牛客暑期多校训练营第二场 I Interval题解
「Tarjan」有向图强连通分量
dpdk 问题分析:dpdk-20.11 ice 100G 网卡 rss_hash 配置无效问题
图形学基础|基于LTC的面光源渲染
dpdk-16.04 监听 uio 文件检测中断的示例 demo 与内部实现解析
对个人与环境的几个观点
【LeetCode 67】二个进制数求和
udevd 检索内核模块并加载的 demo
[STM32] cubemx configuration diagram of 480mhz clock under 25MHz external crystal oscillator of h743
C语言版:循环链表的建立
C语言版:二叉树的静态建立
Reflection cannot find the class message classnotfound of UDF when executing flinksql code