CN113885864A - Java-based code segment failure policy protection method and device - Google Patents
Java-based code segment failure policy protection method and device Download PDFInfo
- Publication number
- CN113885864A CN113885864A CN202111237199.6A CN202111237199A CN113885864A CN 113885864 A CN113885864 A CN 113885864A CN 202111237199 A CN202111237199 A CN 202111237199A CN 113885864 A CN113885864 A CN 113885864A
- Authority
- CN
- China
- Prior art keywords
- policy
- code segment
- strategy
- code
- java
- Prior art date
- Legal status (The legal status is an assumption and is not a legal conclusion. Google has not performed a legal analysis and makes no representation as to the accuracy of the status listed.)
- Pending
Links
- 238000000034 method Methods 0.000 title claims abstract description 13
- 239000012634 fragment Substances 0.000 claims abstract description 14
- 239000002131 composite material Substances 0.000 claims abstract description 6
- 238000004806 packaging method and process Methods 0.000 claims abstract description 3
- 230000002159 abnormal effect Effects 0.000 claims description 3
- 238000005516 engineering process Methods 0.000 abstract description 2
- 230000009286 beneficial effect Effects 0.000 description 1
- 230000007547 defect Effects 0.000 description 1
- 230000000694 effects Effects 0.000 description 1
- 238000005538 encapsulation Methods 0.000 description 1
- 230000000977 initiatory effect Effects 0.000 description 1
- 238000012986 modification Methods 0.000 description 1
- 230000004048 modification Effects 0.000 description 1
Images
Classifications
-
- G—PHYSICS
- G06—COMPUTING; CALCULATING OR COUNTING
- G06F—ELECTRIC DIGITAL DATA PROCESSING
- G06F8/00—Arrangements for software engineering
- G06F8/30—Creation or generation of source code
- G06F8/36—Software reuse
Landscapes
- Engineering & Computer Science (AREA)
- Software Systems (AREA)
- General Engineering & Computer Science (AREA)
- Theoretical Computer Science (AREA)
- Physics & Mathematics (AREA)
- General Physics & Mathematics (AREA)
- Devices For Executing Special Programs (AREA)
Abstract
The invention discloses a Java-based code segment failure policy protection method and device, and relates to the field of basic technologies. The method comprises the following steps: packaging container classes which simultaneously transmit result values and exceptions, abstracting a given code segment to be used as an execution context, defining a strategy interface, taking an execution context which needs to be realized by a strategy and then returning the execution result or the exception of the execution context, constructing an actuator of strategy combination, and allowing a plurality of strategies to be nested and combined into a composite strategy according to an incoming sequence; creating various strategies according to needs to realize a Policy interface; a plurality of policies are assembled and passed into the code fragments. The invention adopts the protection of the failure strategy which has strong readability, strong expansibility, reusability and configurability aiming at the appointed code segment, improves the maintainability of the code and has wide application prospect.
Description
Technical Field
The invention relates to the field of basic technology, in particular to a Java-based code segment failure policy protection method and device.
Background
The code segment failure policy protection refers to that in daily development, a series of policy entries are often required for a specific code segment (for example, obtaining a remote or local resource, initiating a remote or local call, and the like), for example, automatic retry in failure, returning over time, and returning back to use the entry resources. In a traditional development mode, a code segment needs to be packaged into a method, a try catch statement is written to process various conditions, writing of complicated strategies by the try catch statement is abnormal and complicated, and meanwhile, due to the fact that the strategies cannot be reused, multiple places in the code repeatedly appear, and maintainability of the code is reduced. In order to solve the above problems, it is particularly necessary to develop a method and apparatus for Java-based code fragment failure policy protection.
Disclosure of Invention
Aiming at the defects in the prior art, the invention aims to provide a Java-based code segment failure policy protection method and a Java-based code segment failure policy protection device, aiming at the specified code segment, the code segment is protected by a failure policy which is strong in readability, strong in expansibility, reusable and configurable, so that the code maintainability is improved, and the code segment failure policy protection method and the code segment failure policy protection device are easy to popularize and use.
In order to achieve the purpose, the invention is realized by the following technical scheme: a Java-based code segment failure policy protection method comprises the following steps:
(1) packaging and simultaneously transmitting the result value and the abnormal container class;
(2) abstracting a given code segment as an execution context;
(3) defining a policy interface, wherein the policy is required to be implemented and returns an Execution result or exception after being taken to an Execution context (Execution);
(4) constructing an executor of the strategy combination, and allowing a plurality of strategies to be nested and combined into a composite strategy according to the incoming sequence;
(5) creating various strategies according to needs to realize a Policy interface;
(6) a plurality of policies are assembled and passed into the code fragments.
A Java-based device for protecting code segment failure strategies comprises a code segment abstractor, a strategy definition interface and a strategy executor, wherein the code segment abstractor is used for streaming code segments in strategies; the strategy definition interface is used for unifying the behavior specifications taken when the code fragments fail; the policy executor is to apply a given combination of policies to a given code fragment.
The invention has the beneficial effects that: according to the technical scheme, the strategy interface is realized, the strategy executor is used for assembling the composite strategy, and the specified code segment is protected by the failure strategy which is strong in readability, expansibility, reusability and configurability, so that the maintainability of the code is improved, and the application prospect is wide.
Drawings
The invention is described in detail below with reference to the drawings and the detailed description;
FIG. 1 is a flow chart of the present invention.
Detailed Description
In order to make the technical means, the creation characteristics, the achievement purposes and the effects of the invention easy to understand, the invention is further described with the specific embodiments.
Referring to fig. 1, the following technical solutions are adopted in the present embodiment: a Java-based code segment failure policy protection method comprises the following steps:
(1) encapsulation passes the result value along with the container class of the exception.
(2) A given piece of code is abstracted as an execution context.
(3) Defining a policy interface, and returning an Execution result or exception of the policy which needs to be implemented after taking an Execution context (Execution).
public interface Policy<T>{
Either<Throwable,T>execute(Execution<T>execution);
}
(4) And constructing an executor of the strategy combination, and allowing a plurality of strategies to be nested and combined into a composite strategy according to the incoming sequence.
(5) And various strategies are created according to the needs to realize the Policy interface.
(6) A plurality of policies are assembled and passed into the code fragments.
The executor at this time, i.e. the packaged combination strategy, can be multiplexed in the system
For this example, a combination strategy in the form of policyl 2(policy3 (code fragments))) is generated
Executor<?>executor=new Executor<>(policyl,policy2,policy3);
// passing code fragments into a policy executor
result=executor.get(()->return something)。
A Java-based device for protecting code segment failure strategies comprises a code segment abstractor, a strategy definition interface and a strategy executor, wherein the code segment abstractor is used for streaming code segments in strategies; the strategy definition interface is used for unifying the behavior specifications taken when the code fragments fail; the policy executor is to apply a given combination of policies to a given code fragment.
The specific embodiment provides failure policy protection which is strong in readability, expansibility, reusability and configurability aiming at the specified code segments by realizing a policy interface and combining a composite policy by a policy executor, and has a wide application scene, for example:
(1) the application scene one: a common automatic retry strategy follows a failure.
Creation of a code that occurs only in a given code segment may be accomplished by
A failed policy executor that retries a maximum of three times at FileNotFoundException:
Executor<String>retry=new Executor<>(new RetryPolicy<>(3,throwable->throwable instanceof FileNotFoundException));
any code segment that produces a string may be passed in at this point to apply the failure policy:
String result=retry.get(()->{
return new Fil eReader(new File(”/path/to/file”))).readAsString();
});
(2) application scenario two: on the basis of the application scenario one, the standby content is used through a rollback strategy after the failure of trying three times is expected.
At this point a combination strategy can be constructed:
Executor<String>retryOrFallback=new Executor<>(
new RetryPolicy<>(
3,throwable->throwable instanceof FileNotFoundException
) New FallBackplicy < > (() - > "this is a default piece of content")
);
Reintroducing a given code fragment:
String result=retry.get(()->{
return new FileReader(new File(”/path/to/file”))).readAsString();
});
at this time, if the file reading fails for three times, a text of 'this is a default piece of content' is returned.
The foregoing shows and describes the general principles and broad features of the present invention and advantages thereof. It will be understood by those skilled in the art that the present invention is not limited to the embodiments described above, which are described in the specification and illustrated only to illustrate the principle of the present invention, but that various changes and modifications may be made therein without departing from the spirit and scope of the present invention, which fall within the scope of the invention as claimed. The scope of the invention is defined by the appended claims and equivalents thereof.
Claims (2)
1. A Java-based code segment failure policy protection method is characterized by comprising the following steps:
(1) packaging and simultaneously transmitting the result value and the abnormal container class;
(2) abstracting a given code segment as an execution context;
(3) defining a policy interface, wherein the policy is required to be implemented and returns an Execution result or exception after being taken to an Execution context (Execution);
(4) constructing an executor of the strategy combination, and allowing a plurality of strategies to be nested and combined into a composite strategy according to the incoming sequence;
(5) creating various strategies according to needs to realize a Policy interface;
(6) a plurality of policies are assembled and passed into the code fragments.
2. A Java-based code segment failure policy protection device is characterized by comprising a code segment abstractor, a policy definition interface and a policy executor, wherein the code segment abstractor is used for streaming code segments in various policies; the strategy definition interface is used for unifying the behavior specifications taken when the code fragments fail; the policy executor is to apply a given combination of policies to a given code fragment.
Priority Applications (1)
Application Number | Priority Date | Filing Date | Title |
---|---|---|---|
CN202111237199.6A CN113885864A (en) | 2021-10-22 | 2021-10-22 | Java-based code segment failure policy protection method and device |
Applications Claiming Priority (1)
Application Number | Priority Date | Filing Date | Title |
---|---|---|---|
CN202111237199.6A CN113885864A (en) | 2021-10-22 | 2021-10-22 | Java-based code segment failure policy protection method and device |
Publications (1)
Publication Number | Publication Date |
---|---|
CN113885864A true CN113885864A (en) | 2022-01-04 |
Family
ID=79013482
Family Applications (1)
Application Number | Title | Priority Date | Filing Date |
---|---|---|---|
CN202111237199.6A Pending CN113885864A (en) | 2021-10-22 | 2021-10-22 | Java-based code segment failure policy protection method and device |
Country Status (1)
Country | Link |
---|---|
CN (1) | CN113885864A (en) |
Cited By (1)
Publication number | Priority date | Publication date | Assignee | Title |
---|---|---|---|---|
CN116954623A (en) * | 2023-09-19 | 2023-10-27 | 南京国睿信维软件有限公司 | Cross-multilingual code mixed calling method |
Citations (4)
Publication number | Priority date | Publication date | Assignee | Title |
---|---|---|---|---|
CN101639803A (en) * | 2008-07-30 | 2010-02-03 | 株式会社日立制作所 | Exception handling method and exception handling device for multithread application system |
US20100050160A1 (en) * | 2008-08-20 | 2010-02-25 | International Business Machines Corporation | System, method and program product for ranking peer submitted code snippets using execution feedback |
CN106484425A (en) * | 2016-10-19 | 2017-03-08 | 济南浪潮高新科技投资发展有限公司 | A kind of abnormality eliminating method of policy-based configuration |
CN109376025A (en) * | 2018-09-03 | 2019-02-22 | 平安普惠企业管理有限公司 | Abnormality eliminating method, device, computer equipment and storage medium |
-
2021
- 2021-10-22 CN CN202111237199.6A patent/CN113885864A/en active Pending
Patent Citations (4)
Publication number | Priority date | Publication date | Assignee | Title |
---|---|---|---|---|
CN101639803A (en) * | 2008-07-30 | 2010-02-03 | 株式会社日立制作所 | Exception handling method and exception handling device for multithread application system |
US20100050160A1 (en) * | 2008-08-20 | 2010-02-25 | International Business Machines Corporation | System, method and program product for ranking peer submitted code snippets using execution feedback |
CN106484425A (en) * | 2016-10-19 | 2017-03-08 | 济南浪潮高新科技投资发展有限公司 | A kind of abnormality eliminating method of policy-based configuration |
CN109376025A (en) * | 2018-09-03 | 2019-02-22 | 平安普惠企业管理有限公司 | Abnormality eliminating method, device, computer equipment and storage medium |
Non-Patent Citations (1)
Title |
---|
王勇等: "基于不变量的程序运行保护方法", 计算机工程与设计》, vol. 29, no. 5, pages 1069 - 1070 * |
Cited By (2)
Publication number | Priority date | Publication date | Assignee | Title |
---|---|---|---|---|
CN116954623A (en) * | 2023-09-19 | 2023-10-27 | 南京国睿信维软件有限公司 | Cross-multilingual code mixed calling method |
CN116954623B (en) * | 2023-09-19 | 2023-12-22 | 南京国睿信维软件有限公司 | Cross-multilingual code mixed calling method |
Similar Documents
Publication | Publication Date | Title |
---|---|---|
Russinovich et al. | Windows internals, part 2 | |
Long et al. | Automatic runtime error repair and containment via recovery shepherding | |
JP6009459B2 (en) | Store and resume application runtime state | |
US9430333B2 (en) | Recovery of application from snapshot | |
US9218163B2 (en) | Generating complex event processing rules | |
US20120311702A1 (en) | System and method for preserving references in sandboxes | |
US10104063B2 (en) | Android-based mobile equipment security protection method, and device | |
CN104182234B (en) | A kind of method for processing business and operation system | |
TW201140350A (en) | High throughput, reliable replication of transformed data in information systems | |
WO2009146612A1 (en) | Method and apparapus for exception handling in embedded c language environment | |
CN103559118A (en) | Security auditing method based on aspect oriented programming (AOP) and annotation information system | |
CN105579971A (en) | Fast path userspace RDMA resource error detection | |
WO2016112625A1 (en) | System realizing script operation in preset embedded system | |
CN106484425B (en) | A kind of abnormality eliminating method of policy-based configuration | |
CN113885864A (en) | Java-based code segment failure policy protection method and device | |
CN106326129A (en) | Program abnormity information generating method and device | |
US8751872B2 (en) | Separation of error information from error propagation information | |
Katzer | Learning Serverless | |
CN107368498A (en) | The lock for optimizing MySQL Pessimistic Lockings waits the method and device of time-out time | |
Kundu et al. | An approach to convert XMI representation of UML 2. x interaction diagram into control flow graph | |
CN106940681B (en) | Method for debugging storage software by using tracking function and analysis tool | |
Feiler et al. | Architecture fault modeling and analysis with the error model annex, version 2 | |
CN106201730A (en) | A kind of linux User space mutual exclusion lock deadlock detection method and system | |
CN109491806A (en) | Adaptation method and device between a kind of kernel and third-party application | |
Lin et al. | Test sequence reduction of wireless protocol conformance testing to internet of things |
Legal Events
Date | Code | Title | Description |
---|---|---|---|
PB01 | Publication | ||
PB01 | Publication | ||
SE01 | Entry into force of request for substantive examination | ||
SE01 | Entry into force of request for substantive examination | ||
RJ01 | Rejection of invention patent application after publication | ||
RJ01 | Rejection of invention patent application after publication |
Application publication date: 20220104 |