当前位置:网站首页>Reflection, an implementation of automatic repeated call interface
Reflection, an implementation of automatic repeated call interface
2022-07-26 12:57:00 【Thorn in the moonlight tonight】
problem
Recently, I met a need in my work , You need to implement repeated calls to the interface in the background , The specific needs are :
- The background needs to cache the results of interface calls ;
- Automatically repeat the call every other period of time , Refresh the result data in the cache ;
- When the front end calls the interface , It will directly return cached data , Instead of actually calling the interface , get data .
The specific implementation plan thought of is :
- Caching result data is simple , Direct use AOP Just do interface enhancement ;
- Save the state of the interface call , So that the interface can be automatically called again later ;
- Use scheduled tasks or delay queues to achieve automatic invocation .
Realization
AOP Do interface enhancement
Because this is not the focus of this article , So don't post specific implementation , The general idea is : Create a pointcut , Then, before and after the method is run, enhance the operations of fetching results from the cache and storing results in the cache .
Save interface call status
This is the focus of this paper , We know that the defining elements of an interface are : class 、 Method name and parameters , So the call information we want to store is this information , The specific implementation steps are as follows :
- Create a class with interface call information
public class RequestObject implements Serializable {
// Class name
private String clazzName;
// Method name
private String methodName;
// Parameter object
private Object[] params;
// Parameter type
private String[] paramTypeNames;
public String getClazzName() {
return clazzName;
}
public void setClazzName(String clazzName) {
this.clazzName = clazzName;
}
public String getMethodName() {
return methodName;
}
public void setMethodName(String methodName) {
this.methodName = methodName;
}
public Object[] getParams() {
return params;
}
public void setParams(Object[] params) {
this.params = params;
}
public String[] getParamTypeNames() {
return paramTypeNames;
}
public void setParamTypeNames(String[] paramTypeNames) {
this.paramTypeNames = paramTypeNames;
}
}
- stay AOP Enhance the code to save the calling information of the calling interface , As for where this object is stored, you can choose .
// Use reflection to obtain various information of the interface
MethodSignature signature = (MethodSignature) joinPoint.getSignature();
Method method = signature.getMethod();
RequestObject requestObject = new RequestObject();
// Class name
Object target = joinPoint.getTarget();
requestObject.setClazzName(target.getClass().getName());
// Method name
requestObject.setMethodName(method.getName());
// Parameter values
Object[] args = joinPoint.getArgs();
requestObject.setParams(args);
// Parameter type
if (args != null && args.length > 0){
String[] typeNames = new String[args.length];
for (int i = 0; i < args.length; i++){
typeNames[i] = args[i].getClass().getName();
}
requestObject.setParamTypeNames(typeNames);
}
- Use timed tasks 、mq Or delay the queue to realize the automatic call to the interface : Take out the call information object , Then execute the interface call
public String test() throws Throwable{
RequestObject requestObject = (RequestObject) RedisUtils.get("key");
// Load class objects
Class c = Class.forName(requestObject.getClazzName());
// Parameter type
String[] paramTypeNames = requestObject.getParamTypeNames();
// Generate method objects
Method method;
if (paramTypeNames == null || paramTypeNames.length <= 0){
method = c.getMethod(requestObject.getMethodName());
}else {
Class[] paramTypeClass = new Class[paramTypeNames.length];
for (int i = 0; i < paramTypeNames.length; i++){
paramTypeClass[i] = Class.forName(paramTypeNames[i]);
}
method = c.getMethod(requestObject.getMethodName(), paramTypeClass);
}
// Method call , A parameter is an object and parameter value of a class ;
// Here the object is from spring From , You can also generate objects in other ways ,
// After all, class objects already exist , It shouldn't be difficult to generate objects with classes
Object result = method.invoke(ApplicationContextHolder.getBean(c), requestObject.getParams());
return result.toString();
}
Come here , This function has been roughly realized , Although it's not very difficult , But it's interesting to use reflection to implement repeated calls of interfaces , Make a note of , Happy ~
边栏推荐
- SLAM 02.整体框架
- 论文阅读-MLPD:Multi-Label Pedestrian Detector in Multispectral Domain(海康威视研究院实习项目)
- The programmed navigation route jumps to the current route (the parameters remain unchanged), and if it is executed multiple times, it will throw a navigationduplicated warning error?
- RMII, smii, gmii, rgmii interfaces of Ethernet Driver
- Data query of MySQL (aggregate function)
- Flutter prevents scientific counting and removes mantissa invalid 0
- 虚拟偶像代言产品出问题谁负责?且听律师分析
- Knowledge points of C language documents
- 关于自动重复调用接口的一种实现方式-反射
- 2022 年要了解的新兴安全供应商
猜你喜欢

VS code 设置Ctrl+S保存,自动格式化的方法

食品安全 | 这些常见食物小心有毒!速查自家餐桌

UE5 官方案例Lyra 全特性详解 7.资源管理

Paper reading MLPD: multi label pedestrian detector in multispectral domain (Internship Program of Hikvision Research Institute)

Kubernetes----高级存储之PV和PVC简介

Knowledge points of C language documents

华为超融合FusionCube解决方案笔记

Huawei ultra fusion fusioncube solution notes

笔记。。。。

PXE原理与配置
随机推荐
Knowledge points of C language documents
PXE principle and configuration
数据查询函数
Kuzaobao: summary of Web3 encryption industry news on July 25
维度灾难 维数灾难 暂记
Industry case | how does the index help the sustainable development of Inclusive Finance in the banking industry
Azure synapse analytics Performance Optimization Guide (2) -- optimize performance using materialized views (Part 1)
The.Net webapi uses groupname to group controllers to render the swagger UI
SLAM 02.整体框架
VS code 设置Ctrl+S保存,自动格式化的方法
Does anyone know where the retract of flinksql can be specified? Only api code settings can be seen in online materials
Guys, please ask me, I have configured CDC to connect to Oracle according to the document, and I always run error reports and can't find the class validstione
Food safety | can you eat any fruit?
Remote IP debugger (Practical dry goods)
火山引擎云上增长方案全景:30+方案齐出,兵发优势领域
Shell variables and references
Redis realizes single sign on -- system framework construction (I)
Commonly used list Why does isempty() suddenly report null?
Flutter integrated Aurora push
mqtt send receive