Summary of the invention
The fundamental purpose of the application is to provide a kind of method for monitoring application program and device, at least to solve the higher problem of code complexity and maintenance cost in prior art.
According to an aspect of the application, provide a kind of method for monitoring application program, it comprises: in class file, add monitoring hook, and wherein, monitoring hook is associated with watchdog routine; When performing the monitoring hook of class file, the watchdog routine application programs calling the association of monitoring hook is monitored, wherein, described watchdog routine and application code lay respectively in different class files, and this two class file is independent of the class file containing described monitoring hook.
The step adding monitoring hook in class file comprises: check when system starts whether class file exists the monitored item of coupling; If exist, then in class file, add monitoring hook.
The step adding monitoring hook in class file comprises: the bytecode of loading classes file; Monitoring hook is joined in bytecode.
Check that when system starts the step whether class file exists the monitored item of coupling comprises: the configuration file reading all monitored item; Judge whether the application information in described class file mates with monitored item when loading the bytecode of described class file, wherein, described monitored item comprises the character string of bag name, class name, method name and parameter list; If coupling, then judge that described class file exists the monitored item of coupling.
After calling watchdog routine pass coupling program corresponding to monitoring hook and monitoring, method for monitoring application program also comprises: class file is arranged to the monitored item not comprising coupling; Reload class file.
After the watchdog routine application programs calling the association of monitoring hook is monitored, method for monitoring application program also comprises: the watchdog routine revising the association of described monitoring hook; Reload described class file, and when performing the monitoring hook of described class file, the watchdog routine calling the association of described monitoring hook is monitored to described application program.
According to the another aspect of the application, provide a kind of Application Monitoring device, it comprises: setting unit, and for adding monitoring hook in class file, wherein, described monitoring hook is associated with watchdog routine; Monitoring unit, for when performing the monitoring hook of described class file, the watchdog routine calling the association of described monitoring hook is monitored described application program, wherein, described watchdog routine and application code lay respectively in different class files, and this two class file is independent of the class file containing described monitoring hook.
Setting unit comprises: checking module, for checking when system starts whether class file exists the monitored item of coupling; Add module, for adding monitoring hook when class file exists the monitored item of coupling in class file.
Add module to comprise: load submodule, for the bytecode of loading classes file; Add submodule, for being joined in bytecode by monitoring hook.
Checking module comprises: reading submodule, for reading the configuration file of all monitored item; Judge submodule, for judging whether the application information in described class file mates with monitored item when loading the bytecode of described class file, wherein, described monitored item comprises the character string of bag name, class name, method name and parameter list; If coupling, then judge that described class file exists the monitored item of coupling.
Monitoring unit comprises: modified module, for revising the watchdog routine of described monitoring hook association; Monitoring module, for reloading described class file, and when performing the monitoring hook of described class file, the watchdog routine calling the association of described monitoring hook is monitored described application program.
By the technical scheme of the application, following beneficial effect can be reached:
1) the application is separated with watchdog routine by making business logic codes, solves the problem that code complexity and maintenance cost in prior art are higher, reduces cost and the complexity of code maintenance;
2) automatically in class file, monitoring hook is added according to monitoring configuration when the application is by running, thus achieving while business logic codes is separated with watchdog routine, minimize the change to existing code, reduce further cost and the complexity of code maintenance;
3) the application only needs to upgrade watchdog routine separately, and does not need to revise business logic codes, thus achieves Application Monitoring neatly, improves monitoring efficiency, saves monitoring period.
Certainly, the arbitrary product implementing the application might not need to reach above-described all advantages simultaneously.
Embodiment
Hereinafter also describe the application in detail with reference to accompanying drawing in conjunction with the embodiments.It should be noted that, when not conflicting, the embodiment in the application and the feature in embodiment can combine mutually.
Before the further details of each embodiment describing the application, the counting system structure that of the principle that can be used for realizing the application is suitable is described with reference to Fig. 1.In the following description, except as otherwise noted, the symbol otherwise with reference to the action performed by one or more computing machine and operation represents each embodiment describing the application.Thus, be appreciated that processing unit that this kind of action performed sometimes referred to as computing machine and operation comprise computing machine is to the manipulation of electric signal representing data with structured form.It is safeguarded in this manipulation transforms data or the position in the accumulator system of computing machine, and this reshuffles or changes the operation of computing machine in the mode that those skilled in the art understands.The data structure of service data is the physical location of the storer of the particular community that the form with data defines.But although describe the application in above-mentioned context, it does not also mean that restrictive, as understood by those skilled in the art, hereinafter described action and each side of operation also can realize with hardware.
Turn to accompanying drawing, wherein identical reference number refers to identical element, and the principle of the application is shown in a suitable computing environment and realizes.Below describe the embodiment based on described the application, and should not think about the alternative embodiment clearly do not described herein and limit the application.
Fig. 1 shows the schematic diagram of the example computer architecture that can be used for these equipment.For purposes of illustration, the architecture of painting is only an example of proper environment, not proposes any limitation to the usable range of the application or function.This computing system should be interpreted as, to the arbitrary assembly shown in Fig. 1 or its combination, there is any dependence or demand yet.
The principle of the application can use other universal or special calculating or communication environment or configuration to operate.Be applicable to the well-known computing system of the application, the example of environment and configuration includes but not limited to, personal computer, server, multicomputer system, system based on micro-process, minicomputer, mainframe computer and comprise the distributed computing environment of arbitrary said system or equipment.
In the configuration that it is the most basic, the system 100 in Fig. 1 at least comprises: as server 102 and one or more client 104 of Application Monitoring device.Server 102 can include but not limited to Micro-processor MCV or programmable logic device (PLD) FPGA etc. treating apparatus, for storing the memory storage of data and the transmitting device with client communication; Client 104 can comprise: Micro-processor MCV, with the transmitting device of server communication, display device with user interactions.In the present specification and claims, " Application Monitoring device " also can be defined as executive software, firmware or microcode can coming any nextport hardware component NextPort of practical function or the combination of nextport hardware component NextPort.System 100 can be even distributed, to realize distributed function.
As used in this application, term " module ", " assembly " or " unit " can refer to the software object that performs on server 102 or routine.Different assembly described herein, module, unit, engine and service can be implemented as on server 102, perform (such as, as independent thread) object or process.Although system and method described herein preferably realizes with software, the realization of the combination of hardware or software and hardware also may and conceived.
Embodiment 1
As shown in Figure 2, Application Monitoring device (preferred, can be positioned on the server 102 of Fig. 1) comprising: setting unit 202, and for adding monitoring hook in class file, wherein, monitoring hook is associated with watchdog routine; Monitoring unit 204, for when performing the monitoring hook of described class file, the watchdog routine calling the association of described monitoring hook is monitored described application program, wherein, described watchdog routine and application code lay respectively in different class files, and this two class file is independent of the class file containing described monitoring hook.
Both the present embodiment watchdog routine and business logic codes are not carried out coupling and are write, and wherein watchdog routine and business logic codes lay respectively in different class files, and this two class file is independent of the class file containing monitoring hook.Monitoring hook can be loaded in business logic codes operational process, and call watchdog routine by monitoring hook, thus achieve being separated of business logic codes and watchdog routine, like this, the hybrid code of design comparison complexity is not needed to come coupling business logical code and watchdog routine, when needing to upgrade different watchdog routines, only need to load different monitoring hooks, and do not need again to write above-mentioned hybrid code, thus code complexity and the higher problem of maintenance cost in solution prior art, reduce cost and the complexity of code maintenance.
In order to add monitoring hook exactly, this application provides a kind of preferred setting unit, wherein, setting unit 202 comprises: checking module 2021, for checking when system starts whether class file exists the monitored item of coupling; Add module 2022, for adding monitoring hook when class file exists the monitored item of coupling in class file.In the present embodiment, by the coupling of monitored item, the current monitoring the need of carrying out application program can being determined exactly, improve the dirigibility of configuration.
Present invention also provides a kind of preferred implementation adding monitoring hook, specifically, add module 2022 and comprise: load submodule, for the bytecode of loading classes file; Add submodule, for being joined in bytecode by monitoring hook.In the present embodiment, because the execution efficiency of bytecode is very high, therefore, the execution efficiency of monitoring hook interpolation can be improved in this preferred embodiment by the mode of the bytecode of amendment class file.
In order to check whether class file exists the monitored item of coupling exactly, this application provides a kind of preferred checking module 2021, it comprises: reading submodule, for reading the configuration file of all monitored item; Judge submodule, for judging whether the application information in described class file mates with monitored item when loading the bytecode of described class file, wherein, described monitored item comprises the character string of bag name, class name, method name and parameter list; If coupling, then judge that class file exists the monitored item of coupling.By the above-mentioned coupling one by one to pointer configuration item, can judge that class file exists the monitored item of coupling exactly.
Present invention also provides a kind of modification to watchdog routine, in this scenario, monitoring unit 204 comprises: modified module 2041, for revising the watchdog routine of described monitoring hook association; Monitoring module 2042, for reloading described class file, and when performing the monitoring hook of described class file, the watchdog routine calling the association of described monitoring hook is monitored described application program.In the present embodiment, when needing to upgrade different watchdog routines, only need the watchdog routine upgrading separately the association of monitoring hook, and reload the class file with monitoring hook, and do not need again to write as prior art the hybrid code of business logic codes and watchdog routine of being coupled, thus solve the higher problem of code complexity and maintenance cost in prior art, reduce cost and the complexity of code maintenance, improve monitoring efficiency, save monitoring period.
Present invention also provides a kind of deletion scheme to watchdog routine, in this scenario, after calling watchdog routine application programs corresponding to monitoring hook and monitoring, described class file is arranged to the monitored item not comprising coupling by Application Monitoring device, like this, when reloading class file, not carrying out monitoring calling of hook, and directly performing other programs.In this way, can when not needing application programs to monitor, realize by deleting monitoring hook, and do not need again to write as prior art the hybrid code of business logic codes and watchdog routine of being coupled, minimize the change to existing code, thus reduce further cost and the complexity of code maintenance, improve monitoring efficiency, save monitoring period.
The detailed process starting watchdog routine and add monitoring hook is described in detail below in conjunction with accompanying drawing.
First, as shown in Figure 4, in order to add monitoring hook exactly, starting watchdog routine and comprising the steps:
S402, starts JVM;
Preferably, setting unit 202 adds in the start-up parameter of JVM "-javaagent:momtor.jar "
S404, starts monitoring module;
1) checking module 2021 reads monitoring configuration file, and loads all monitored item;
The content of monitored item comprises:
Point: the character string being a bag name+class name+method name+parameter list, is used to specify the method needing monitoring;
Advice: be a Monitor interface realize class, when performing to the method for specifying, can be called this and realize class, thus whether analyze current runtime data normal, abnormal then send abnormal to Surveillance center;
The profile instance of monitored item can see following code:
2) after monitoring module initialization completes, a ClassFileTransformer (class file converter) can be registered to JVM, be named as monitorTransformer, for modifying to the bytecode of class file, to add corresponding monitoring hook (Hook).
S406, starts application system.
Secondly, as shown in Figure 5, realize by the bytecode revising class (Class) file the interpolation monitoring hook, specifically, the method for the interpolation monitoring hook of the embodiment of the present application comprises the following steps:
S502, virtual machine (JVM) loads Class file;
S504, class file converter (monitorTransformer) carries out pre-service to Class file, preferably, travels through all monitored item, finds out the monitored item of this Class file of coupling;
S506, judges whether the Point configuration item containing coupling; If coupling, then go to S508; If do not mate, then go to S514
Preferably, travel through all monitoring configuration items, check whether this Class has the monitored item of coupling; Whether the rule checked is all application programs (or being called method) needing monitoring of searching loop Class file, configure with the Point of configuration item, i.e. " bag name+class name+method name+parameter list " coupling;
S508, lists all application programs needing monitoring;
S510, joins in the bytecode of the application program needing monitoring by monitoring hook; Preferably, Hook is divided into before|after|around type, that is, Before type refers to and call Hook before this application call; After type refers to calls Hook after this application call; Around type refers to calls Hook when this application exception;
S512, returns to virtual machine by amended bytecode;
S514, initialization Class file.
Like this, when code performs HOOK, system can call this Hook, Hook will call the monitoring logic defined, thus realizes system monitoring during system cloud gray model.
The present embodiment only needs the application program being arranged the monitoring of multiple needs in independently class file by monitoring configuration item, and monitoring hook is added in this class file, automatically load the monitoring hook in this class file like this when needing the application program of monitoring to run, and call corresponding watchdog routine by this monitoring hook.Therefore, because the present embodiment is not arrange the corresponding application program needing monitoring by monitoring configuration item in multiple class file, can avoids system in different class files, travel through monitoring configuration item, improve the speed of system searching class file.
Embodiment 2
Fig. 3 is that it comprises the steps: according to the preferred process flow diagram of the one of the method for monitoring application program of the embodiment of the present application
S302, adds monitoring hook in class file, and wherein, monitoring hook is associated with watchdog routine;
S304, when performing the monitoring hook of described class file, the watchdog routine calling the association of described monitoring hook is monitored described application program, wherein, described watchdog routine and application code lay respectively in different class files, and this two class file is independent of the class file containing described monitoring hook.
Both the present embodiment watchdog routine and business logic codes are not carried out coupling and are write, and wherein watchdog routine and business logic codes lay respectively in different class files, and this two class file is independent of the class file containing monitoring hook.Monitoring hook can be loaded in business logic codes operational process, and call watchdog routine by monitoring hook, thus achieve being separated of business logic codes and watchdog routine, like this, the hybrid code of design comparison complexity is not needed to come coupling business logical code and watchdog routine, when needing to upgrade different watchdog routines, only need to load different monitoring hooks, and do not need again to write above-mentioned hybrid code, thus code complexity and the higher problem of maintenance cost in solution prior art, reduce cost and the complexity of code maintenance.
In order to add monitoring hook exactly, this application provides a kind of step preferably adding monitoring hook in class file, it comprises: check when system starts whether class file exists the monitored item of coupling; If exist, then in class file, add monitoring hook.In the present embodiment, by the coupling of monitored item, the current monitoring the need of carrying out application program can being determined exactly, improve the dirigibility of configuration.
Present invention also provides a kind of preferred implementation adding monitoring hook, specifically, the step adding monitoring hook in class file comprises: the bytecode of loading classes file; Monitoring hook is joined in bytecode.In the present embodiment, because the execution efficiency of bytecode is very high, therefore, the execution efficiency of monitoring hook interpolation can be improved in this preferred embodiment by the mode of the bytecode of amendment class file.
In order to check whether class file exists the monitored item of coupling exactly, this application provides a kind of preferred when system starts inspection class file whether there is the step of the monitored item of coupling, it comprises: the configuration file reading all monitored item; Judge whether the application information in described class file mates with monitored item when loading the bytecode of described class file, wherein, described monitored item comprises the character string of bag name, class name, method name and parameter list; If coupling, then judge that described class file exists the monitored item of coupling.By the above-mentioned coupling one by one to pointer configuration item, can judge that class file exists the monitored item of coupling exactly.
Present invention also provides a kind of deletion scheme to watchdog routine, in this scenario, after the watchdog routine application programs calling the association of monitoring hook is monitored, method for monitoring application program also comprises: class file is arranged to the monitored item not comprising coupling; Reload class file.Like this, when reloading class file, not carrying out monitoring calling of hook, and directly performing other programs.In this way, can when not needing application programs to monitor, realize by deleting monitoring hook, and do not need again to write the hybrid code of business logic codes and watchdog routine of being coupled, minimize the change to existing code, thus reduce further cost and the complexity of code maintenance, improve monitoring efficiency, save monitoring period.
Present invention also provides a kind of modification to watchdog routine, in this scenario, after the watchdog routine application programs calling the association of monitoring hook is monitored, method for monitoring application program also comprises: the watchdog routine of amendment monitoring hook association; Reload described class file, and when performing the monitoring hook of described class file, the watchdog routine calling the association of described monitoring hook is monitored to described application program.In the present embodiment, when needing to upgrade different watchdog routines, only need revise and load different monitoring hooks, and do not need again to write the hybrid code of business logic codes and watchdog routine of being coupled, thus code complexity and the higher problem of maintenance cost in solution prior art, reduce cost and the complexity of code maintenance, improve monitoring efficiency, save monitoring period.
The detailed process starting watchdog routine and add monitoring hook is described in detail below in conjunction with accompanying drawing.
First, as shown in Figure 4, in order to add monitoring hook exactly, starting watchdog routine and comprising the steps:
S402, starts JVM;
Preferably, setting unit 202 adds in the start-up parameter of JVM "-javaagent:monitor.jar "
S404, starts monitoring module;
1) checking module 2021 reads monitoring configuration file, and loads all monitored item;
The content of monitored item comprises:
Point: the character string being a bag name+class name+method name+parameter list, is used to specify the method needing monitoring;
Advice: be a Monitor interface realize class, when performing to the method for specifying, can be called this and realize class, thus whether analyze current runtime data normal, abnormal then send abnormal to Surveillance center;
The profile instance of monitored item can see following code:
2) after monitoring module initialization completes, a ClassFileTransformer (class file converter) can be registered to JVM, be named as monitorTransformer, for modifying to the bytecode of class file, to add corresponding monitoring hook (Hook).
S406, starts application system.
Secondly, as shown in Figure 5, realize by the bytecode revising class (Class) file the interpolation monitoring hook, specifically, the method for the interpolation monitoring hook of the embodiment of the present application comprises the following steps:
S502, virtual machine (JVM) loads Class file;
S504, class file converter (monitorTransformer) carries out pre-service to Class file, preferably, travels through all monitored item, finds out the monitored item of this Class file of coupling;
S506, judges whether the Point configuration item containing coupling; If coupling, then go to S508; If do not mate, then go to S514
Preferably, travel through all monitoring configuration items, check whether this Class has the monitored item of coupling; Whether the rule checked is all application programs (or being called method) needing monitoring of searching loop Class file, configure with the Point of configuration item, i.e. " bag name+class name+method name+parameter list " coupling;
S508, lists all application programs needing monitoring;
S510, joins in the bytecode of the application program needing monitoring by monitoring hook; Preferably, Hook is divided into before|after|around type, that is, Before type refers to and call Hook before this application call; After type refers to calls Hook after this application call; Around type refers to calls Hook when this application exception;
S512, returns to virtual machine by amended bytecode;
S514, initialization Class file.
Like this, when code performs HOOK, system can call this Hook, Hook will call the monitoring logic defined, thus realizes system monitoring during system cloud gray model.In addition, the present embodiment only needs the application program being arranged the monitoring of multiple needs in independently class file by monitoring configuration item, and monitoring hook is added in this class file, in the application program operational process needing monitoring, automatically load the monitoring hook in this class file like this, and call corresponding watchdog routine by this monitoring hook.Therefore, because the present embodiment is not arrange the corresponding application program needing monitoring by monitoring configuration item in multiple class file, can avoids system in different class files, travel through monitoring configuration item, improve the speed of system searching class file.
The service code of the logical and application of application monitoring is separated by the application completely, class bytecode is revised dynamically when system starts, to insert a monitoring hook (Hook), in hook program, call the logic of application monitoring, thus on business without any impact.By the technical scheme described in each embodiment of the application, can by the configuration unified management of application monitoring, and be separated completely with the configuration file of business, service logic exploitation is separated completely with the exploitation of monitoring logic, makes the existence of the imperceptible supervisory system of operation system.
Obviously, those skilled in the art should be understood that, each module of above-mentioned the application or each step can realize with general calculation element, they can concentrate on single calculation element, or be distributed on network that multiple calculation element forms, alternatively, they can realize with the executable program code of calculation element, thus, they can be stored and be performed by calculation element in the storage device, and in some cases, step shown or described by can performing with the order be different from herein, or they are made into each integrated circuit modules respectively, or the multiple module in them or step are made into single integrated circuit module to realize.Like this, the application is not restricted to any specific hardware and software combination.
The foregoing is only the preferred embodiment of the application, be not limited to the application, for a person skilled in the art, the application can have various modifications and variations.Within all spirit in the application and principle, any amendment done, equivalent replacement, improvement etc., within the protection domain that all should be included in the application.