Disclosure of Invention
The invention aims to provide a data request method, electronic equipment and a readable storage medium, wherein the data request method is high in request efficiency and difficult to lose data.
In order to achieve the above object, the present invention provides a data request method, including the steps of:
S1, initiating a network request to call an interface to acquire data;
S2, acquiring an interface calling result, if the interface calling result is successfully called, entering a step S5, otherwise, entering a step S3;
S3, determining an exception type corresponding to the network request, if the exception type is I/O exception, adopting an HttpClient retry machine to initiate a retry request, if the exception type is business logic exception, adopting a SPRING RETRY retry mechanism to initiate a retry request, and entering a step S4 after the HttpClient retry and/or SPRING RETRY retry is not successfully invoked and reaches a predefined retry threshold, or the exception type is I/O exception and other exceptions except business logic exception;
S4, packaging the network request into objects and storing;
s5, ending.
Preferably, the retry threshold includes a first retry threshold and a second retry threshold, in step S3, it is first determined whether the anomaly type is an I/O anomaly, if so, the httptest retry mechanism is adopted to initiate a retry request until the first retry threshold is successfully invoked and entered step S5 or exceeded; if the first retry threshold is exceeded or the abnormality type is not an I/O abnormality, judging whether the abnormality type is a business logic abnormality, if so, initiating a retry request by adopting the SPRING RETRY retry mechanism until the step S5 is successfully invoked or the second retry threshold is exceeded; if the second retry threshold is exceeded or the anomaly type is not a business logic anomaly, step S4 is entered.
Specifically, before step S1, the method further includes the steps of: s0, constructing HttpClient, and defining HttpRequestRetryHandler interfaces; and defining SPRING RETRY retry templates, and setting corresponding retry strategies and backoff strategies.
Preferably, httplient is structured in a single instance mode and timeout time is set and Http connection pool is enabled using PoolingHttpClientConnectionManager to implement connection multiplexing.
Preferably, a single-threaded pool task that scans for empty connections periodically is started, and abnormal or empty connections are closed.
Preferably, execution addShutdownHook instructs to close the httpfile object and to close the single-threaded pool for scanning for empty connections when the JVM is closed.
Preferably, before step S5 after step S4, the method further comprises: the network request is re-executed based on a Java reflection mechanism.
To achieve the above object, the present invention provides an electronic device including a processor, a memory, and a computer program stored in the memory and configured to be executed by the processor, the processor executing the computer program to perform the data requesting method as described above.
To achieve the above object, the present invention provides a computer-readable storage medium storing a computer program executable by a processor to perform the data request method as described above.
Compared with the prior art, the invention is divided into I/O abnormality, business logic abnormality, other abnormality (non-procedural abnormality) and other multi-level dimensions such as I/O abnormality, business logic abnormality, etc. aiming at the abnormality processing aspect, SPRING RETRY is used for processing the business logic abnormality, and the bottom abnormality such as the I/O abnormality is processed by adopting an HttpClient retry mechanism, and the problem of network level abnormality can be solved more bottom and more efficiently by utilizing HttpRequestRetryHandler abnormality retry interfaces, thereby reducing program loss. And, in the case that the successful call and the non-procedural exception are not realized through the httpfile retry and SPRING RETRY retry, the network request is packaged into the object store so as to complete the network request by the next retrying, thereby avoiding the loss of data.
Detailed Description
For a detailed description of the contents, construction features, achieved objects and effects of the present invention, a technical solution of the embodiments of the present invention will be clearly and completely described with reference to the accompanying drawings in the embodiments of the present invention, and it is apparent that the described embodiments are only some embodiments of the present invention, not all embodiments of the present invention. All other embodiments, which can be made by those skilled in the art based on the embodiments of the invention without making any inventive effort, are intended to be within the scope of the invention.
The data request method provided by the invention comprises the following steps:
S0, constructing HttpClient, and defining HttpRequestRetryHandler interfaces; and defining SPRING RETRY retry templates, and setting corresponding retry strategies and backoff strategies. Among other things, how to construct httpfile and implement HttpRequestRetryHandler interfaces, and the definition and setup of SPRING RETRY are implemented by the prior art.
S1, initiating a network request to call an interface to acquire data.
S2, obtaining an interface calling result, if the interface calling result is successfully called, entering a step S5, otherwise, entering a step S3.
S3, determining an exception type corresponding to the network request, if the exception type is I/O exception (exception caused by I/O error), adopting an HttpClient retry machine to initiate a retry request, if the exception type is business logic exception (exception not conforming to business rules), adopting a SPRING RETRY retry mechanism to initiate a retry request, and if the retry request is not successfully invoked and reaches a predefined retry threshold through HttpClient retry and/or SPRING RETRY retry, or if the exception type is I/O exception and other exceptions except the business logic exception, entering step S4.
S4, packaging the network request into objects and storing the objects in a storage medium such as a database or a message queue.
S5, ending.
How to determine the exception type corresponding to the network request is the prior art may be implemented by, for example, instanceof operators, which are not described herein.
The data request method of the present invention will be described in detail below by taking an embodiment as an example with reference to fig. 1 of the accompanying drawings.
Referring to fig. 1, fig. 1 shows an embodiment of a data request method of the present invention. As shown in fig. 1, in step S3, it is first determined whether the exception type is an I/O exception, if so, the httpcalient retry mechanism is adopted to initiate a retry request, after each retry, it is determined again whether the call is successful, if so, step S5 can be directly entered to end the current program; if the call is not successful, further judging whether the first retry threshold (retry times) is exceeded, if the first retry threshold is not exceeded, initiating a retry request again by adopting the httpgent retry mechanism until the call is successful, and entering step S5 or exceeding the first retry threshold. If the abnormal type is not the I/O abnormality or exceeds the first retry threshold, further judging whether the abnormal type is the business logic abnormality, if so, adopting SPRING RETRY retry machine to initiate a retry request, and after each retry, determining whether to call successfully again, if so, directly entering step S5 to end the current program; if the call is not successful, further judging whether the second retry threshold (retry times) is exceeded, if the second retry threshold is not exceeded, initiating a retry request again by adopting SPRING RETRY retry mechanism until the call is successful, and entering step S5 or exceeding the second retry threshold. If the anomaly type is not a business logic anomaly or exceeds the second retry threshold, the process proceeds directly to step S4 to make a persistent network request (the network request is packaged as an object and stored).
In this embodiment, before step S5 after step S4, further comprising: the network request is re-executed based on the Java reflection mechanism. Since the network request is persistently stored in step S4, the network request can be executed again after the abnormality cause is checked and the abnormality is eliminated, without losing any piece of data. The persistent information in step S4 includes basic information required by Java reflection, such as parameters, parameter types, method names, class names, and the like, and further includes status and exception information of the network request, so that whether the network request is successfully executed or not and reasons of execution failure can be obtained in the later stage, and the later stage investigation is facilitated.
Further, in the process of executing the data request method, a single-thread pool task for periodically scanning the empty connection is also started, and the abnormal or idle connection is periodically scanned to clear the abnormal or idle connection, so that more available connections are vacated. When the JVM (Java Virtual Machine ) is turned off, a addShutdownHook instruction (adding a closing hook) is executed to close the HttpClient object and to close the method for scanning the single thread pool of the empty connection, so as to implement memory cleaning, resource reclamation, and the like, and make room.
In this embodiment, when httpllient is used to send post and get requests, httpllient instances need to be instantiated, and then the httpllient. Execution () method needs to be called, each time httpllient is instantiated, the httpllient instances cannot be shared, which is disadvantageous for a large number of request processes, and httpllient class thread security is considered, in this embodiment httpllient is constructed in a single instance mode. Because the Http connection requires three handshakes, the overhead is high, in the embodiment, the timeout time is set when the Http connection is constructed, the PoolingHttpClientConnectionManager is used for starting the Http connection pool to realize connection multiplexing, the overhead of creating the TCP connection is reduced, the performance is improved, and the efficiency of network request is further improved. Meanwhile, connection activity detection is forbidden when the connection is fetched each time, and Keep-Alive mode, so that the overhead of establishing TCP connection is reduced, the performance is improved, and the efficiency of network request is improved.
Referring next to fig. 2, an electronic device 100 according to the present invention includes a processor 10, a memory 20, and a computer program 30. The memory 20 stores one or more computer programs 30, such as a data request program, which when executed by the processor 10 causes the processor 10 to implement the data request method described above.
The memory 20 may be any form of random access memory, read only memory, flash memory or other similar device integrated into an electronic device. The processor 10 may be a central processing unit or other programmable general purpose or special purpose microprocessor, digital signal processor, programmable controller, special integrated circuit or other similar device or combination of such devices.
Accordingly, the present invention also relates to a computer readable storage medium storing a computer program which, when executed by the processor 10, performs the data requesting method in the above-described embodiment. Wherein the computer program comprises computer program code, which may be in the form of source code, object code, executable files or in some intermediate form, etc. The computer readable storage medium may include: any entity or device capable of carrying computer program code, a recording medium, a U disk, a removable hard disk, a magnetic disk, an optical disk, a computer memory, a Read Only Memory (ROM), a Random Access Memory (RAM), and so forth.
In summary, the invention is divided into I/O exception, business logic exception, and other exception (non-procedural exception) except I/O exception and business logic exception, etc. multi-level dimension, for business logic exception, SPRING RETRY is used to process, for I/O exception, the bottom layer exception is processed by using httpClient retry mechanism, httpRequestRetryHandler exception retry interface is used to solve problem of network layer exception more bottom layer and more high efficiency, and program loss is reduced. And, in the case that the successful call and the non-procedural exception are not realized through the httpfile retry and SPRING RETRY retry, the network request is packaged into the object store so as to complete the network request by the next retrying, thereby avoiding the loss of data. That is, the retry and spam are initiated with as little loss as possible, ensuring that each network request can be performed with trace and with efficiency.
The foregoing disclosure is only illustrative of the preferred embodiments of the present invention and is not to be construed as limiting the scope of the invention, which is defined by the appended claims.