In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-04-13 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >
Share
Shulou(Shulou.com)05/31 Report--
This article mainly introduces the relevant knowledge of "what is the implementation method of API". The editor shows you the operation process through the actual case. The operation method is simple, fast and practical. I hope this article "what is the implementation method of API" can help you solve the problem.
1 Design criteria of external interface
The basic principles of SDK interface design are easy to use, easy to understand, easy to expand and easy to monitor. Expansion can be summarized into the following features:
API is classified according to business functions, but all services have a unified invocation style.
API does not contain a method implementation, and the implementation of the interface is hidden from the caller.
API calls are traceable.
In terms of functional form, SDK needs to provide the following types of API:
Function interface: actively call and use the function provided by it
Synchronization interface: the function call is completed in the calling thread and the result is returned immediately.
Ordinary asynchronous interface: complete the function call in the background thread, and you can add callback functions.
Interruptible asynchronous interface: complete the function call in the background thread, you can add callback function, you can interrupt the call.
Callback API: listens for data and status changes.
2 classification of business
SDK includes a variety of services, one is basic services, the other is optional services. For example, user authentication services and group services are basic services, while super group services and third-party push services are optional services. In addition, different businesses inevitably have similar functions, such as adding group members and pulling group messages in group services and super-large group services. Therefore, it is necessary to isolate different services, on the one hand, to facilitate the expansion and adjustment of business functions, on the other hand, to facilitate the naming of functions and the understanding of users.
There are multiple API under the same service, which are divided into callback API and function interface according to the initiative of the call, which are placed in the one-to-one corresponding interface classes Observer and Service respectively. For example, all callback interfaces and function interfaces under the user authentication service are located in AuthServiceObserver and AuthService. The function interfaces called actively by users are in AuthService, such as login, logout, etc., while the callback interfaces for registration callback are in AuthServiceObserver, such as monitoring online status, monitoring data synchronization, and so on.
According to the nature of execution, the functional interfaces in each Service are divided into three types: synchronous interface, ordinary asynchronous interface and interruptible asynchronous interface. The synchronous interface executes immediately in the calling thread; the asynchronous interface executes in the background thread, and the InvocationFuture type that can be set callback is returned in the calling thread, and the final result is callback in the main thread; the interruptible asynchronous interface is similar to the ordinary asynchronous interface, but the AbortableFuture type inherited from InvocationFuture is returned, which supports interrupt operation. Users can interrupt the execution of the functional interface through active calls. The thread switching process called by the interface is shown in figure 2.1, and the classification of business functions and interfaces is shown in figure 2.2.
Figure 2.1 Thread switching process for interface calls
Figure 2.2 Classification of business functions and interfaces
3 implementation of API 3.1 implementation of API
In order to achieve these goals, and considering the simplicity of implementation, we choose Java's dynamic proxy class model. When the external caller calls API, it gets a dynamic Proxy object and transfers all the calls of the functional interface to a class ProxyHandler that implements the InvocationHandler interface through the Proxy object. Then, according to the calling method, execute the registration / logout callback or dispatch the call request to the real implementation class, and finally make the return or callback according to the return type of the interface, as shown in figure 3.1.
Figure 3.1 function call process
Like the interface classes AuthService and AuthServiceObserver for user services, SDK defines interface classes for all other services. There is an one-to-one correspondence between all interface classes and implementation classes, so it is easy to find the corresponding implementation of API.
3.2Methods to get Proxy objects
SDK provides a static method NimClient.getService (Class clazz) to obtain the dynamic proxy class corresponding to the business interface class. Enter the corresponding API class as the parameter. For example, the way to obtain the interface class of user authentication service is NimClient.getService (AuthService.class), and the way to obtain the interface class of user authentication service observer is NimClient.getService (AuthServiceObserver.class).
The NimClient.getService method returns a Proxy object synchronously. The object is constructed by lazy loading, and the corresponding instance is constructed only when the business is called. As shown in figure 3.2.
Figure 3.2 get the business Proxy object process
The generation of Proxy objects is based on the Proxy.newProxyInstance method, and all generated Proxy objects are managed by special container classes. Take the user authentication service as an example, when NimClient.getService (AuthService.class) is called for the first time to obtain the Proxy object of the user authentication service, the container management class creates a corresponding Proxy object and caches it. When the user authentication service Proxy object is obtained again, the container class will cache and return directly.
3.3 transaction tracking class
There is an important transaction tracking class (Transaction) in the invoke function, which records the synchronous and asynchronous properties of the method, the method body, the return value requirements, and so on. The Transaction object corresponds to its implementation method one by one. Each time the invoke method is executed, an instance of Transaction is created and transferred to the actual execution point for execution
After execution, the invoke method determines the return result of the function based on the synchronous and asynchronous characteristics and the return value. If it is a synchronous function, it returns the result directly; if it is an asynchronous function, it returns InvocationFuture or AbortableFuture according to the business requirements.
3.4 implementation of API
NimClient.getService returns the dynamic proxy class ProxyHandler, which implements the Object invoke (Object who, Method method, Object [] args) method of the InvocationHandler interface, which is used to proxy all functional interfaces.
API execution, that is, how ProxyHandler implements the invoke method, the general steps are to load the transaction, initialize the judgment, execute the transaction, and return, as shown in figure 3.3.
Figure 3.3 brief process executed by the agent
The execution of the transaction can also be subdivided into the execution of the callback interface and the execution of the function interface. If the callback interface is executed, it is determined whether the current status needs to be called back, and if so, the callback immediately.
The execution function of Transaction is TransactionExecutor. The execute method, after sending the Transaction, the invoke method will decide whether to execute in the current thread or in the background thread based on the synchronous and asynchronous properties. The background thread used to execute the Transaction asynchronously is unique and will block if there are multiple Transaction that need to be executed asynchronously.
Each method of the interface class is stored in a Map, and SDK implements the overloading of the function with the same name with the method signature. When executing Transaction, you can find the corresponding implementation class through the interface class where the method is located, and then call the corresponding invoke method. For asynchronous methods, the return value of the invoke method is not returned to the upper layer, so the implementation of the asynchronous API function does not care about the return value. The detailed execution process of the agent is shown in figure 3.4
Figure 3.4 detailed flow of the agent
3.5 callback of asynchronous methods
For asynchronous methods, in TransactionExecutor. Before execute execution, the corresponding AbortableFuture implementation class TransactionFuture is generated based on Transaction, and then cached for later callback. After the asynchronous method is executed, the subclass is returned to the call layer.
After calling the interruptible asynchronous interface, the AbortableFuture instance is returned synchronously. The caller can directly call the abort method to abort execution. For example, after calling the downloadAttachment function for downloading message attachments, you can call the abort method to terminate the download when you need to cancel the download due to the large file and poor network condition. The method of calling is shown in figure 3.5. The abort method corresponding to the interruptible asynchronous interface is implemented within SDK, and the caller does not need to care about its implementation.
Figure 3.5 example of abort function call
The basic type of TransactionFuture callback is RequestCallback,SDK. In this basic type, onSuccess, onFailed and onException functions are defined, which are used to call back to different functions according to the execution result in the case of success, failure and exception, respectively. This is the end of the whole process.
This is the end of the content about "what is the implementation of API". Thank you for your reading. If you want to know more about the industry, you can follow the industry information channel. The editor will update different knowledge points for you every day.
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 239
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.