A service response method and apparatus, an electronic device, and a storage medium
By establishing a global table binding business module identifier and object memory address when the server-side program framework is initialized, the cross-operating system applicability problem is solved, a cross-platform business response method is implemented, the difficulty of writing and maintenance is reduced, and the scalability and stability of the business module are improved.
Patent Information
- Application Number
- CN202510896652.6
- Authority / Receiving Office
- CN · China
- Patent Type
- Patents(China)
- Current Assignee / Owner
- Filing Date
- 2025-07-01
- Publication Date
- 2025-10-14
- Estimated Expiration
- 2045-07-01
AI Technical Summary
In the existing technology, the method of loading business modules through function calls between separate program frameworks and business modules is difficult to apply to different operating systems, resulting in poor applicability, and requires setting configuration files and modifying function signatures, which increases the difficulty of writing and maintenance.
When the program framework on the server is initialized, the correspondence between the business module's business identifier, the code unit object memory address, and the event callback function is established. The global table is bound, and the corresponding event callback function is called when a business request is received to implement business response and avoid relying on operating system-specific functions to load modules.
It improves the applicability of business responses under different operating systems, reduces the difficulty of writing and maintenance, enhances scalability and stability, avoids address forced type conversion and unsafe operations, and improves the accuracy and stability of business responses.
Smart Images

Figure CN120407043B_ABST
Abstract
Description
TECHNICAL FIELD
[0001] The present application relates to the technical field of data processing, and in particular to a service response method and device, electronic equipment and storage medium. BACKGROUND
[0002] A service platform integrates a server, and the server as an application program can provide various services for users. The current server generally includes a program framework and a service module, each service module can provide an external service, and the service module works under the program framework. The structure of the server is that the program framework and the service module are separated, which can decouple the program framework and each service module, thereby reducing the direct dependence of the service module on the program framework and reducing the mutual influence of the technical iteration of the program framework and the change of the service module.
[0003] The separated program framework and service module provide services externally through function calls. The program framework usually relies on a preset function to load the service module, and then the program framework and the service module can perform function calls to provide services externally. In the prior art, for different operating systems, the program framework needs to load the service module through different functions, for example, for a Linux (a kind of computer operating system) system, the program framework needs to load the service module through dlopen (a function of a programming language) or dlsym (a function of a programming language), and for a Windows (a kind of computer operating system) system, the program framework needs to load the service module through LoadLibrary (a function of a programming language) or FreeLibrary (a function of a programming language). The separated program framework and service module rely on a preset function to load the service module, so that the way of providing services externally is difficult to adapt to different operating systems, and the applicability is poor. SUMMARY
[0004] The purpose of the embodiments of the present application is to provide a service response method, device, electronic equipment and storage medium to improve the applicability of the way of providing services externally under different operating systems. The specific technical solutions are as follows:
[0005] According to an aspect of the embodiments of the present application, a service response method is provided, and the method comprises:
[0006] In response to the initialization of the application startup on the server side, the object of the first code unit is loaded into the heap memory, a correspondence between the business identifier of each business module, the memory address of the object of the first code unit of the business module, and the event callback function is established, and a global table containing the established correspondence is obtained, wherein the application includes a program framework and business modules, the first code unit is: a code unit constructed in each business module that inherits the second code unit, the second code unit is: a code unit constructed in the program framework, and the event callback function is: a callback function registered in the second code unit for calling the business callback function in each business module;
[0007] Before the main function of the application is executed, the global table is traversed to bind the service identifier of the same service module, the memory address of the object of the first code unit, and the event callback function;
[0008] In response to receiving a service request, if the identifier of the service requested by the service request is the same as the first service identifier among the service identifiers, the object of the first code unit corresponding to the first service identifier is called by calling the event callback function corresponding to the first service identifier.
[0009] In one embodiment of the present invention, the first code unit is constructed in the following manner:
[0010] Based on an initialization expression including an integer static constant, the first code unit is constructed under a preset path.
[0011] In one embodiment of the present invention, calling the object of the first code unit corresponding to the first service identifier by calling the event callback function corresponding to the first service identifier includes:
[0012] According to the memory address of the object of the code unit corresponding to the first service identifier, copy the object of the first code unit corresponding to the first service identifier to create a copy object;
[0013] The business request is used as a member variable to call the copy object.
[0014] In one embodiment of the present invention, copying the object of the first code unit corresponding to the first service identifier according to the memory address of the object of the code unit corresponding to the first service identifier to create the copy object includes:
[0015] Taking the second code unit as a base class, constructing an intermediate inheritance code unit that inherits the second code unit in the framework;
[0016] In the business module, an indirect inheritance relationship between the first code unit and the second code unit is established through the intermediate inheritance code unit;
[0017] Based on the indirect inheritance relationship between the first code unit and the second code unit and the memory address of the object of the code unit corresponding to the first service identifier, a copy object is created under the intermediate inheritance code unit.
[0018] In one embodiment of the present application, the establishing of the correspondence among the service identifiers of the service modules, the memory addresses of the objects of the first code units of the service modules and the event callback functions comprises:
[0019] generating an initial table;
[0020] binding the string of the service identifier of the service module, the event callback function and the callback parameter, taking the memory address of the object of the first code unit of the service module as the callback parameter, and establishing the correspondence among the service identifier of the service code, the memory address of the object of the first code unit of the service code and the event callback function in the initial table, wherein the callback parameter is the memory address of the object of the first code unit of the service module.
[0021] According to another aspect of the embodiment of the present application, a service response device is provided, which comprises:
[0022] a table construction module, configured to load the object of the first code unit into the heap memory in response to the application program of the service end being started to initialize, and to establish the correspondence among the service identifiers of the service modules, the memory addresses of the objects of the first code units of the service modules and the event callback functions, so as to obtain a global table containing the established correspondence, wherein the application program comprises a program framework and service modules, the first code unit is a code unit constructed in each service module and inherited from a second code unit, the second code unit is a code unit constructed in the program framework, and the event callback function is a callback function registered in the second code unit and used to call the service callback function in each service module;
[0023] a binding module, configured to traverse the global table before the main function of the application program is executed, and to bind the service identifiers of the same service module, the memory addresses of the objects of the first code units and the event callback functions;
[0024] a service response module, configured to, in response to a service request being received, call the object of the first code unit corresponding to the first service identifier by calling the event callback function corresponding to the first service identifier, if the identifier of the service requested by the service request is the same as the first service identifier among the service identifiers.
[0025] In one embodiment of the present application, the first code unit is constructed in the following manner:
[0026] The first code unit is constructed in a preset path based on an initialization expression including an integer static constant.
[0027] In one embodiment of the present application, the service response module is specifically configured to copy the object of the first code unit corresponding to the first service identifier according to the memory address of the object of the first code unit corresponding to the first service identifier, and create a copy object; and take the service request as a member variable and invoke the copy object.
[0028] In one embodiment of the present application, the service response module is specifically configured to take the second code unit as a base class, construct an intermediate inheritance code unit which inherits the second code unit in a framework, establish an indirect inheritance relationship between the first code unit and the second code unit through the intermediate inheritance code unit in a service module, and create a copy object under the intermediate inheritance code unit based on the indirect inheritance relationship between the first code unit and the second code unit and the memory address of the object of the first code unit corresponding to the first service identifier.
[0029] In one embodiment of the present application, the table construction module is specifically configured to generate an initial table, bind a string of a service identifier of a service module, an event callback function and a callback parameter, take the memory address of the object of the first code unit of the service module as the callback parameter, and establish a corresponding relationship among the service identifier of the service code, the memory address of the object of the first code unit of the service code and the event callback function in the initial table, wherein the callback parameter is the memory address of the object of the first code unit of the service module.
[0030] According to still another aspect of the embodiment of the present application, an electronic device is provided, which comprises a processor, a communication interface, a memory and a communication bus, wherein the processor, the communication interface and the memory complete communication with each other through the communication bus.
[0031] The memory is used for storing a computer program.
[0032] The processor is used for executing the program stored on the memory, and realizes the service response method described above.
[0033] According to still another aspect of the embodiment of the present application, a computer readable storage medium is provided, which stores a computer program, and the computer program is executed by a processor to realize the service response method described above.
[0034] According to still another aspect of the embodiment of the present application, a computer program product containing instructions is provided, which, when running on a computer, causes the computer to execute the service response method described above.
[0035] The embodiment of the present application has the following beneficial effects:
[0036] As can be seen from the above, in the service response method provided by the embodiment of the present application, when the program framework of the service end is started to initialize, the object of the first code unit is loaded into the heap memory, then a global table including the corresponding relationship among the service identifiers of each service module, the memory address of the object of the first code unit of the service module and the event callback function is constructed, and then when the program framework of the service end is started to execute, the service identifier, the memory address of the object of the first code unit and the event callback function of the same service module are bound, so that when responding to the service request, when the identifier of the service requested by the service request is the same as the first service identifier among the service identifiers, the event callback function bound with the first service identifier can be called according to the binding relationship, the object of the first code unit corresponding to the first service identifier in the memory is called to realize the calling of the object of the first code unit corresponding to the first service identifier, and in the above process, the function under the operating system does not need to be relied on to load the service module, and the above-mentioned service providing mode can be applied to different operating systems, and the applicability of the service response under different operating systems is improved.
[0037] Of course, implementing any product or method of the present application does not necessarily require achieving all the advantages described above at the same time. BRIEF DESCRIPTION OF DRAWINGS
[0038] In order to more clearly illustrate the technical solutions in the embodiments of the present application or the prior art, the following will briefly introduce the drawings needed to be used in the embodiments or prior art description. Obviously, the drawings in the following description only some embodiments of the present application, and other embodiments can also be obtained by those skilled in the art based on these drawings.
[0039] Figure 1 The flowchart of the service response method provided by the embodiment of the present application;
[0040] Figure 2 The flowchart of the corresponding relationship establishing method provided by the embodiment of the present application;
[0041] Figure 3 The structural diagram of the service response device provided by the embodiment of the present application;
[0042] Figure 4 The structural diagram of the electronic device provided by the embodiment of the present application. DETAILED DESCRIPTION
[0043] The following will clearly and completely describe the technical solutions in the embodiments of the present invention in conjunction with the accompanying drawings. Obviously, the described embodiments are only part of the embodiments of the present invention, not all of the embodiments. Based on the embodiments of the present invention, all other embodiments obtained by ordinary technicians in this field based on the present invention are within the scope of protection of the present invention.
[0044] The prior art is described below.
[0045] In the prior art, program frameworks for different operating systems require different functions to load business modules. For example, for Linux (a computer operating system), system program frameworks require dlopen (a programming language function) or dlsym (a programming language function) to load business modules, while for Windows (a computer operating system), system program frameworks require LoadLibrary (a programming language function) or FreeLibrary (a programming language function) to load business modules. For example, see the following example of a program framework loading a business module through a function:
[0046] void handle = dlopen("filename.so", RTLD_NOW); ...
[0048] BusinessHandler handler = dlsym(handle, “functionname”); ...
[0050] handler->(para1, para2, ...);
[0051] As can be seen from the above example, the dlopen function requires the business module's filename "filename.so," and the dlsym function requires the function name "functionname." This requires setting up a configuration file to record the filenames and function names of each business module. Obviously, setting up a configuration file is cumbersome, and any changes to a business module require updating the configuration file.
[0052] Moreover, the above-mentioned program framework's method of loading business modules through functions is a purely process-oriented method. The program framework and business modules can only communicate by passing parameters. If it is necessary to increase the parameters passed between the program framework and a business module, the function signature needs to be modified, and the modification of the function signature will affect other business modules.
[0053] In summary, the separated program framework depends on preset functions to load the business module, so that the way of providing the business externally is not only difficult to adapt to different operating systems, poor in applicability, but also needs to set and update the corresponding configuration file, resulting in great difficulty in writing and maintaining the server application program, and the parameters transmitted between the program framework and the business module are difficult to increase, and the scalability is poor.
[0054] The execution subject of the embodiment of the application is described below.
[0055] The scheme provided by the embodiment of the application can be applied to electronic devices such as desktop computers, notebook computers, tablet computers and servers, and can also be applied to a server that responds to a business request sent by a client. For ease of description, the execution subject of the business response method provided by the embodiment of the application is collectively referred to as a server.
[0056] The business response method provided by the embodiment of the application is described in detail below.
[0057] In one embodiment of the application, referring to Figure 1 A flowchart of a business response method is provided, and the method includes the following steps S101-S103.
[0058] Step S101: In response to the application program of the server starting initialization, the object of the first code unit is loaded into the heap memory, the correspondence between the business identifier of each business module, the memory address of the object of the first code unit of the business module and the event callback function is established, and a global table containing the established correspondence is obtained.
[0059] The application program of the server includes a program framework and a business module. The program framework is the core program of the server, and the program framework contains the global data structure of the core program. The number of business modules included in the application program can be multiple, and each business module corresponds to a business provided by the server. The business module is a set of executable codes set for the business provided by the server, for example, the business module can be a dynamic library corresponding to the business.
[0060] The event callback function is a callback function registered in the second code unit for calling the business callback function in each business module. When the business request sent by the user end is received, the server can implement the preset business by calling the business callback function. The business callback function is used to implement the preset business.
[0061] The object of the first code unit can include member variables and member functions, wherein the member functions can include the business callback function.
[0062] The first code unit is a code unit constructed in each business module and inherits the second code unit. The second code unit is a code unit constructed in the program framework.
[0063] For example, the server can construct the second code unit based on the following code:
[0064] class HttpHandler
[0065] {
[0066] virtual void handler() = 0;
[0067] };
[0068] The server can construct the first code unit based on the following code:
[0069] class EchoRequestHandler : public HttpHandler
[0070] {
[0071] void handler() override { ...};
[0072] };
[0073] In this way, the process-oriented processing functions of the business module can be converted into class member functions, and such class member functions can use the data members of the base class in the program framework.
[0074] The following describes a method for loading the object of the first code unit into the heap memory.
[0075] In one implementation, a preset compiler may be used to set the object of the first code unit as a pre-constructed object based on preset construction attributes. The server may determine that the object of the first code unit belongs to the pre-constructed object based on the preset construction attributes, and load the object of the first code unit determined to be the pre-constructed object into heap memory.
[0076] In another implementation, the first code unit is constructed in the following manner: based on an initialization expression including an integer static constant, the first code unit is constructed in a preset path.
[0077] The integer static constant can be any integer constant, and the initialization expression can be an integer static constant declaration expression for the first code unit. In this way, when the program framework of the server is started and initialized, the server first constructs the first code unit that declares the integer static constant, and loads the object of the first code unit into the heap memory. This implementation can be applied to a program programmed in a preset programming language, for example, a program programmed in C (a programming language) or C++ (a programming language).
[0078] For example, the first code unit that inherits the second code unit can be constructed in each service module based on the following code:
[0079] class EchoRequestHandler : public HttpHandler ...
[0081] static const int usless = (new EchoRequestHandler(" / echo"), 0);
[0082] The static const int usless = (new EchoRequestHandler(" / echo"), 0); is an initialization expression including an integer static constant, and 0 is the integer static constant.
[0083] As can be seen, the server can determine whether to construct the code unit when the program framework of the server is started and initialized according to whether the code unit declares an integer static constant, so that the object of the first code unit is loaded into the heap memory in priority, the accuracy of constructing the global table is ensured, and the object of the first code unit can be accurately called, and the applicability of the way of providing services to the outside can be further improved without relying on a preset compiler. In this way, using the initialization expression including the integer static constant can give the first code unit a high-priority execution opportunity, and the first code unit is inherited from the second code unit, and such inheritance relationship can trigger the base class constructor in the program framework to execute, thereby constructing the first code unit, and then loading the object of the first code unit into the heap memory.
[0084] In addition, using this way, the first code unit is constructed through a high-priority execution opportunity, and then the object of the first code unit is loaded into the heap memory, which can avoid the situation that the data structure that has been filled is reinitialized due to the undefined initialization order of global or static objects in different code units, and avoid the situation that the object content is empty.
[0085] Step S101 establishes a corresponding relationship, and a manner of obtaining the global table is described in the following embodiment, which is not described here in detail.
[0086] Step S102: Before the main function of the application program is executed, the global table is traversed, and the business identifier of the same business module, the memory address of the object of the first code unit, and the preset event callback function are bound.
[0087] The global table can be a hash table. The pre-main function stage is the initialization stage before the execution of the main function of the application program.
[0088] In an implementation manner, the server can use a preset global table traversal loop structure to traverse the global table, and use the event callback function setting function to bind the business identifier of the same business module, the memory address of the object of the first code unit, and the event callback function. Wherein, after the object of the first code unit is loaded into the heap memory, the inheritance relationship between the first code and the second code unit is inherited, and the base class constructor in the program framework can be triggered when the first code unit is constructed, and then the memory address of the object of the first code unit can be registered into the global data structure of the program framework.
[0089] For example, the server can traverse the global table based on the following code: for (hent = HT_START(htp_handlers, &g_htp_handlers); hent; ). The server can bind based on the following code: evhttp_set_cb(ta._http_base, ent->_uri, ent->_cb, ent->_cbarg);.
[0090] Step S103: In response to receiving a business request, if the identifier of the business requested by the business request is the same as the first business identifier in each business identifier, the object of the first code unit corresponding to the first business identifier is called by calling the event callback function corresponding to the first business identifier.
[0091] The business request can carry the identifier of the business requested by the business request.
[0092] The server can traverse to determine each business identifier in the global table according to the identifier of the above-mentioned requested business, and determine the first business identifier in each business identifier that is the same as the identifier of the above-mentioned requested business.
[0093] In an implementation manner, the server can directly call the object of the first code unit corresponding to the first business identifier by calling the event callback function. The event callback function can be: calling the object of the first code unit corresponding to the first business identifier according to the memory address of the object of the first code unit of the business module.
[0094] In another implementation, the server can copy the object of the first code unit corresponding to the first business identifier according to the memory address of the object of the code unit corresponding to the first business identifier by calling the event callback function, create a copy object, take the business request as a member variable, and call the copy object. As can be seen, for each business request, the server can copy the object of the first code unit, create a copy object corresponding to the business request, and achieve the response to the business request by calling the copy object. In this way, the mutual influence between different requests can be reduced.
[0095] In addition, if it is necessary to call, copy, or the like, the object of the first code unit in the memory, the object in the memory cannot be directly operated. When the application is in the main function stage, the server can encapsulate the member function of the object of the first code unit into a function object, and then take the function object as a template parameter of a preset template code unit. The preset template code unit has a static function declared therein, and the static function and the function object are functions of the same type. When it is necessary to operate the object of the first code unit in the memory, the server can call the function object in the static function of the above-mentioned template code unit. In this way, the function object carries the memory address of the object of the first code unit corresponding to the function object, and the operation on the object of the first code unit in the memory can be achieved based on the memory address. For example, the object can be copied by a C++ type copy constructor according to the memory address of the object of the first code unit carried in the function object.
[0096] For example, the server can call the copy object based on the following code:
[0097] virtual void handler() = 0;
[0098] virtual HttpHandler clone() const = 0;
[0099] static void process(struct evhttp_request req, void arg) {
[0100] HttpHandler hdler = ((HttpHandler )arg)->clone();
[0101] _req = req; ...
[0103] hdler->handler();
[0104] }
[0105] HttpRequest _req;
[0106] Further, the server can perform object replication according to the following steps A-C.
[0107] Step A: taking the second code unit as a base class, constructing an intermediate inheritance code unit inheriting the second code unit in the framework.
[0108] Step B: in the business module, establishing an indirect inheritance relationship between the first code unit and the second code unit through the intermediate inheritance code unit.
[0109] Step C: based on the indirect inheritance relationship between the first code unit and the second code unit and the memory address of the object of the code unit corresponding to the first business identifier, creating a replication object under the intermediate inheritance code unit.
[0110] In an implementation manner, the server can construct an intermediate inheritance code unit inheriting the second code unit in the framework based on the CRTP (Curiously Recurring Template Pattern) mode, establish the indirect inheritance relationship, and then create a replication object under the intermediate inheritance code unit. By establishing the indirect inheritance relationship between the first code unit and the second code unit through the intermediate inheritance code unit and creating a replication object under the intermediate inheritance code unit, the created replication object can inherit the request member of the second code unit and can facilitate parameter processing, thereby improving the business response accuracy. In addition, the intermediate inheritance code unit constructed in the business module can inherit each feature of the second code unit, thereby facilitating maintenance and expansion of the business module and improving the maintainability and expansibility of the business module.
[0111] For example, the server can construct the intermediate inheritance code unit based on the code in the following program framework:
[0112] template<typename Base, typename Derived>
[0113] class HttpHandlerCloneable : public HttpHandler {
[0114] using Base::Base;
[0115] virtual Base clone() const
[0116] {
[0117] return new Derived(static_cast<Derived const &>( this));
[0118] }
[0119] };
[0120] The server can establish an indirect inheritance relationship based on the following code:
[0121] class EchoRequestHandler : public HttpHandlerCloneable<HttpHandler, EchoRequestHandler> ...
[0123] static const int usless = (new EchoRequestHandler(" / echo"), 0);
[0124] In this way, the object of the first code unit corresponding to the first business identifier can be copied when the object of the first code unit corresponding to the first business identifier is copied.
[0125] As can be seen from the above, in the business response method provided by the embodiments of the application, when the program framework of the server is started and initialized, the object of the first code unit is loaded into the heap memory, and then a global table including the correspondence between the business identifier of each business module, the memory address of the object of the first code unit of the business module, and the preset event callback function is constructed, and then when the program framework of the server is started and executed, the business identifier, the memory address of the object of the first code unit, and the preset event callback function of the same business module are bound, so that when a business request is responded to, when the identifier of the business requested by the business request is the same as the first business identifier in each business identifier, the event callback function bound with the first business identifier can be called according to the binding relationship, the object of the first code unit corresponding to the first business identifier is called based on the object of the first code unit corresponding to the first business identifier in the memory, and the object of the first code unit corresponding to the first business identifier is called in the above process without relying on the function under the operating system to load the business module, so that the way of providing the business to the outside can be suitable for different operating systems, and the applicability under different operating systems is improved.
[0126] And, the service response method provided by the embodiment of the present application can be applied to different operating systems, so that a cross-system code does not need to be written by a developer, the service response method provided by the embodiment of the present application belongs to passive registration, and the file name, function name and the like of the service module are not needed, an additional configuration file does not need to be set, and the signature of a function does not need to be modified, so that the service response method calls the object of the first code unit of the service module in an object-oriented manner, the communication between a program framework and the service module can be performed only by passing parameters, the service module is facilitated to be updated and expanded, and the like, so that the difficulty of writing and maintaining a server-side application program can be reduced.
[0127] In addition, the service response method provided by the embodiment of the present application is based on the object of the first code unit corresponding to the first service identifier in the memory, the object of the first code unit corresponding to the first service identifier is called, so that the use of address forced type conversion, operation of the memory and the like non-safe means can be avoided, the filled data structure is prevented from being reinitialized due to the initialization sequence of global or static variables, and the stability of service response is improved.
[0128] The way of establishing the corresponding relationship and obtaining the global table in step S101 is described below.
[0129] In one embodiment of the present application, referring to Figure 2 A flowchart of a corresponding relationship establishment method is provided. The method includes steps S201-S202.
[0130] Step S201: generating an initial table.
[0131] In an implementation mode, the server can generate the initial table by using a preset hash table structure macro definition.
[0132] For example, the preset hash table structure macro definition can be set based on the following code:
[0133] HT_HEAD(htp_handlers, evhtpcb_t) g_htp_handlers = HT_INITIALIZER();
[0134] Step S202: binding the string of the service identifier of the service module, the preset event callback function and the callback parameter, taking the memory address of the object of the first code unit of the service module as the callback parameter, and establishing the corresponding relationship among the service identifier of the service code, the memory address of the object of the first code unit of the service code and the preset event callback function in the initial table.
[0135] The callback parameter is the memory address of the object of the first code unit of the service module
[0136] In an implementation manner, the server can use a preset registration function declaration function to bind a string of a business identity of the business module, a preset event callback function and a callback parameter. Then, a preset insertion operation function is used to insert a corresponding relationship among the business identity of the business code, the memory address of the object of the first code unit of the business code and the preset event callback function in the initial table.
[0137] For example, the preset registration function declaration function can be set based on the following code:
[0138] void reg(const std::string uri, void ( cb)(struct evhttp_request ,void ), void cbarg) { ...
[0140] hent->_uri = strdup(uri.data());
[0141] hent->_cb = cb;
[0142] hent->_cbarg = cbarg;
[0143] The preset insertion operation function can be set based on the following code:
[0144] HT_INSERT(htp_handlers, &g_htp_handlers, hent);
[0145] By binding the string of the business identity, the preset event callback function and the memory address of the object of the first code unit of the business module as the callback parameter, the global table can be constructed based on the object of the first code unit in the memory, so that when the business request is received and the identity of the requested business of the business request hits the business identity in the global table, the memory address of the object of the first code unit corresponding to the business identity can be accurately determined, and the business response accuracy is improved.
[0146] Corresponding to the above business response method, an embodiment of the present application further provides a business response device.
[0147] In an embodiment of the present application, referring to Figure 3 A structural schematic diagram of a business response device is provided, and the device comprises:
[0148] The table construction module 301 is used to load the object of the first code unit into the heap memory in response to the initialization of the application startup on the server side, establish the correspondence between the business identifier of each business module, the memory address of the object of the first code unit of the business module and the event callback function, and obtain a global table containing the established correspondence, wherein the application includes a program framework and a business module, the first code unit is: a code unit that inherits the second code unit constructed in each business module, the second code unit is: a code unit constructed in the program framework, and the event callback function is: a callback function registered in the second code unit for calling the business callback function in each business module.
[0149] The binding module 302 is used to traverse the global table before the main function of the application is executed, and bind the service identifier of the same service module, the memory address of the object of the first code unit, and the event callback function.
[0150] The business response module 303 is used to respond to the reception of a business request. If the identifier of the business requested by the business request is the same as the first business identifier among the business identifiers, the object of the first code unit corresponding to the first business identifier is called by calling the event callback function corresponding to the first business identifier.
[0151] As can be seen from the above, in the business response method provided by the embodiment of the present invention, when the program framework of the server is started and initialized, the object of the first code unit is loaded into the heap memory, and then a global table including the business identifiers of each business module, the memory addresses of the objects of the first code unit of the business module, and the correspondence between the preset event callback functions is constructed. Then, when the program framework of the server is started and executed, the business identifier of the same business module, the memory address of the object of the first code unit, and the preset event callback function are bound. In this way, when responding to a business request, when the identifier of the business requested by the business request is the same as the first business identifier in each business identifier, the event callback function bound to the first business identifier can be called according to the above binding relationship. Based on the object of the first code unit corresponding to the first business identifier in the memory, the object of the first code unit corresponding to the first business identifier is called. In the above process, there is no need to rely on the function under the operating system to load the business module. This way of providing services to the outside world can be applicable to different operating systems, thereby improving the applicability under different operating systems.
[0152] Moreover, the business response method provided by the embodiment of the present invention can be applied to different operating systems, so there is no need for developers to write cross-system code. The business response method provided by the embodiment of the present invention is a passive registration, which does not require the file name, function name, etc. of the business module, does not require the setting of additional configuration files, and does not require the modification of the function signature, which facilitates the updating and expansion of the business module, thereby reducing the difficulty of writing and maintaining server-side applications.
[0153] In addition, the business response method provided by the embodiment of the present invention is based on the object of the first code unit corresponding to the first business identifier in the memory, and implements the call of the object of the first code unit corresponding to the first business identifier. This can avoid the use of unsafe means such as address forced type conversion and memory operation, and avoid the reinitialization of the filled data structure due to the initialization order of global or static variables, thereby improving the stability of the business response.
[0154] In one embodiment of the present invention, the first code unit is constructed in the following manner:
[0155] Based on an initialization expression including an integer static constant, the first code unit is constructed under a preset path.
[0156] It can be seen that the server can determine whether to construct the code unit when the server's program framework is started and initialized based on whether the code unit declares an integer static constant. This can enable the first code unit to be loaded into the heap memory first, ensure the accuracy of constructing the global table, and then accurately call the object of the first code unit. In addition, the applicability of the way of providing external services can be further improved without relying on the preset compiler.
[0157] In one embodiment of the present invention, the business response module is specifically used to copy the object of the first code unit corresponding to the first business identifier according to the memory address of the object of the code unit corresponding to the first business identifier to create a copy object; and use the business request as a member variable to call the copy object.
[0158] It can be seen that for each business request, the server can copy the object of the first code unit, create a copy object corresponding to the business request, and respond to the business request by calling the copy object. In this way, the mutual influence between different requests can be reduced.
[0159] In one embodiment of the present invention, the business response module is specifically used to construct an intermediate inheritance code unit that inherits the second code unit in the framework with the second code unit as the base class; in the business module, an indirect inheritance relationship between the first code unit and the second code unit is established through the intermediate inheritance code unit; based on the indirect inheritance relationship between the first code unit and the second code unit and the memory address of the object of a code unit corresponding to the first business identifier, a copy object is created under the intermediate inheritance code unit.
[0160] In this way, the created copy object can inherit the request members of the second code unit, which can facilitate parameter processing and improve the accuracy of business responses. In addition, the intermediate inheritance code unit constructed in the business module can inherit the various characteristics of the second code unit, facilitating the maintenance and expansion of the business module and improving the maintainability and scalability of the business module.
[0161] In one embodiment of the present invention, the table construction module is specifically used to generate an initial table; bind the character string of the business identifier of the business module, the preset event callback function and the callback parameter, and use the memory address of the object of the first code unit of the business module as the callback parameter to establish a correspondence between the business identifier of the business code, the memory address of the object of the first code unit of the business code and the preset event callback function in the initial table, wherein the callback parameter is the memory address of the object of the first code unit of the business module.
[0162] By binding the business identifier string, the preset event callback function, and the memory address of the object of the first code unit of the business module as the callback parameter, a global table can be constructed based on the object of the first code unit in the memory. In this way, when a business request is received and the identifier of the business requested by the business request hits the business identifier in the global table, the memory address of the object of the first code unit corresponding to the business identifier can be accurately determined, thereby improving the accuracy of the business response.
[0163] The embodiment of the present invention further provides an electronic device, such as Figure 4 As shown, it includes a processor 401 , a communication interface 402 , a memory 403 and a communication bus 404 , wherein the processor 401 , the communication interface 402 and the memory 403 communicate with each other via the communication bus 404 .
[0164] Memory 403, used for storing computer programs;
[0165] The processor 401 is configured to implement any of the above-mentioned service response methods when executing the program stored in the memory 403 .
[0166] The communication bus mentioned in the electronic devices mentioned above can be a Peripheral Component Interconnect (PCI) bus or an Extended Industry Standard Architecture (EISA) bus. This communication bus can be divided into address buses, data buses, control buses, etc. For ease of illustration, only a single thick line is used in the figure, but this does not mean that there is only one bus or only one type of bus.
[0167] The communication interface is used for communication between the above electronic device and other devices.
[0168] The memory may include random access memory (RAM) or non-volatile memory (NVM), such as at least one disk storage. Alternatively, the memory may be at least one storage device located away from the processor.
[0169] The above-mentioned processor can be a general-purpose processor, including a central processing unit (CPU), a network processor (NP), etc.; it can also be a digital signal processor (DSP), an application specific integrated circuit (ASIC), a field programmable gate array (FPGA) or other programmable logic devices, discrete gate or transistor logic devices, and discrete hardware components.
[0170] In another embodiment of the present invention, a computer-readable storage medium is provided, which stores a computer program. When the computer program is executed by a processor, the steps of any of the above-mentioned business response methods are implemented.
[0171] In another embodiment of the present invention, a computer program product including instructions is provided. When the computer program product is run on a computer, the computer is enabled to execute any one of the service response methods in the above embodiments.
[0172] In the embodiments described above, all or some of the steps can be implemented by software, hardware or firmware, or any combination thereof. When implemented by software, all or some of the steps can be implemented in the form of one or more computer programs. The computer program can be stored in any computer readable medium, and loaded into the computer for execution. The computer readable medium includes computer storage media and communication media. The computer storage media includes any tangible or virtual media for storing data, which can be accessed by the computer. The computer storage media includes Random Access Memory (RAM), non-volatile memory including flash memory and hard disk drive (HDD), and other storage media. The communication media typically includes computer readable instructions, data structures, program modules or other data carried by a data signal. The computer program can be distributed over wired or wireless networks or stored in any computer readable media.
[0173] It should be noted that, in this document, the terms such as first and second are used only to distinguish one entity or operation from another, and do not necessarily require or imply any such actual relationship or order between these entities or operations. Also, the terms "comprises", "comprising", or any other variations thereof are intended to cover a non-exclusive inclusion, so that a process, method, article, or apparatus that comprises a list of elements does not only include those elements, but also includes other elements not expressly listed or inherent to such process, method, article, or apparatus. Without more limitations, an element defined by the statement "comprises a" does not exclude the existence of additional identical elements in the process, method, article, or apparatus that includes the element.
[0174] Each of the embodiments in the specification is described in a related manner, and the same or similar parts between the embodiments can be referred to each other. Each embodiment focuses on the difference from other embodiments. In particular, for the device, electronic device, storage medium, and computer program product embodiments, since they are basically similar to the method embodiments, the description is relatively simple, and the relevant parts can be referred to the part of the method embodiment.
[0175] The above description is only a preferred embodiment of the present invention and is not intended to limit the scope of protection of the present invention. Any modifications, equivalent replacements, improvements, etc. made within the spirit and principles of the present invention are included in the scope of protection of the present invention.
Claims
1. A service response method, characterized in that: The method comprises: In response to the initialization of the application startup on the server side, the object of the first code unit is loaded into the heap memory, a correspondence between the business identifier of each business module, the memory address of the object of the first code unit of the business module, and the event callback function is established, and a global table containing the established correspondence is obtained, wherein the application includes a program framework and business modules, the first code unit is: a code unit constructed in each business module that inherits the second code unit, the second code unit is: a code unit constructed in the program framework, and the event callback function is: a callback function registered in the second code unit for calling the business callback function in each business module; Before the main function of the application is executed, the global table is traversed to bind the service identifier of the same service module, the memory address of the object of the first code unit, and the event callback function; In response to receiving a service request, if the identifier of the service requested by the service request is the same as the first service identifier among the service identifiers, the object of the first code unit corresponding to the first service identifier is called by calling the event callback function corresponding to the first service identifier.
2. The method according to claim 1, characterized in that The first code unit is constructed as follows: Based on an initialization expression including an integer static constant, the first code unit is constructed under a preset path.
3. The method according to claim 1, characterized in that The calling of the object of the first code unit corresponding to the first service identifier by calling the event callback function corresponding to the first service identifier includes: According to the memory address of the object of the code unit corresponding to the first service identifier, copy the object of the first code unit corresponding to the first service identifier to create a copy object; The business request is used as a member variable to call the copy object.
4. The method according to claim 3, characterized in that The step of copying the object of the first code unit corresponding to the first service identifier according to the memory address of the object of the code unit corresponding to the first service identifier to create the copy object includes: Taking the second code unit as a base class, constructing an intermediate inheritance code unit that inherits the second code unit in the framework; In the business module, an indirect inheritance relationship between the first code unit and the second code unit is established through the intermediate inheritance code unit; Based on the indirect inheritance relationship between the first code unit and the second code unit and the memory address of the object of a code unit corresponding to the first service identifier, a copy object is created under the intermediate inheritance code unit.
5. The method according to any one of claims 1 to 4, characterized in that The establishing of the correspondence between the service identifier of each service module, the memory address of the object of the first code unit of the service module, and the event callback function includes: Generate the initial table; Bind the business identifier string, event callback function and callback parameters of the business module, use the memory address of the object of the first code unit of the business module as the callback parameter, and establish a correspondence between the business identifier of the business code, the memory address of the object of the first code unit of the business code and the event callback function in the initial table, wherein the callback parameter is the memory address of the object of the first code unit of the business module.
6. A service response device, characterized in that: The device comprises: A table construction module is configured to, in response to the initialization of a server-side application startup, load the object of the first code unit into heap memory, establish a correspondence between the business identifier of each business module, the memory address of the object of the first code unit of the business module, and the event callback function, and obtain a global table containing the established correspondence, wherein the application includes a program framework and business modules, the first code unit is: a code unit constructed in each business module that inherits the second code unit, the second code unit is: a code unit constructed in the program framework, and the event callback function is: a callback function registered in the second code unit for calling the business callback function in each business module; A binding module, configured to traverse the global table before the main function of the application is executed, and bind the service identifier of the same service module, the memory address of the object of the first code unit, and the event callback function; The business response module is used to respond to the receipt of a business request. If the identifier of the business requested by the business request is the same as the first business identifier among the business identifiers, the object of the first code unit corresponding to the first business identifier is called by calling the event callback function corresponding to the first business identifier.
7. The device according to claim 6, characterized in that The first code unit is constructed as follows: Based on an initialization expression including an integer static constant, the first code unit is constructed under a preset path.
8. The device according to claim 6, characterized in that The business response module is specifically configured to copy the object of the first code unit corresponding to the first business identifier according to the memory address of the object of the code unit corresponding to the first business identifier to create a copy object; and call the copy object using the business request as a member variable; and / or The business response module is specifically configured to construct an intermediate inheritance code unit that inherits the second code unit in a framework using the second code unit as a base class; establish an indirect inheritance relationship between the first code unit and the second code unit in the business module through the intermediate inheritance code unit; and create a copy object under the intermediate inheritance code unit based on the indirect inheritance relationship between the first code unit and the second code unit and the memory address of an object of a code unit corresponding to the first business identifier; and / or The table construction module is specifically used to generate an initial table; bind the character string of the business identifier of the business module, the preset event callback function and the callback parameter, and use the memory address of the object of the first code unit of the business module as the callback parameter to establish a correspondence between the business identifier of the business code, the memory address of the object of the first code unit of the business code and the preset event callback function in the initial table, wherein the callback parameter is the memory address of the object of the first code unit of the business module.
9. An electronic device, characterized in that: It includes a processor, a communication interface, a memory and a communication bus, wherein the processor, the communication interface and the memory communicate with each other via the communication bus; Memory for storing computer programs; A processor, configured to implement the method according to any one of claims 1 to 5 when executing a program stored in a memory.
10. A computer-readable storage medium, characterized in that The computer-readable storage medium stores a computer program, and when the computer program is executed by a processor, the method according to any one of claims 1 to 5 is implemented.
Citation Information
Patent Citations
Service event processing system, service event processing method, service event processing device and equipment
CN112579156A
Global transaction processing method and system
CN113849278A