Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

What are the ways to make Java robust?

2025-01-27 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/01 Report--

This article mainly talks about "what are the methods for the robustness of Java". Interested friends may wish to have a look. The method introduced in this paper is simple, fast and practical. Let's let the editor take you to learn what are the methods of Java robustness.

First, conduct a unified business processing response

According to the standard return of Ant Financial Services Group's open platform, an response should have at least 4 return values.

1. Whether the isSuccess call is successful

2. Response data returned by data

3. ErrorCode error code

4. ErrorMsg error message

This requires our interface to have a standard and unified response, so how to achieve it?

1. Spring section, JDK dynamic proxy, Cglib dynamic proxy and so on are realized by proxy class.

2. Anonymous subclass, which uses a common Executor to handle all requests.

Both of the above modes can implement standard response encapsulation, so what exactly should be encapsulated? In fact, the most important thing is a unified try catch to prevent any 500 errors from being given to the caller.

-Why do it at the outermost layer? -

Because 500 errors are fatal and worthless to the caller, regardless of whether the caller is a front-end or other business system

-set a uniform error code-

Such as

Parameter error PARAMETER_ERROR

Database error DATABASE_ERROR

External system error OUTER_SYSTEM_ERROR

With the above error codes and error messages, the business side can at least tell the user what happened, and can also set a series of alarms and automated operation and maintenance methods to deal with these errors.

Second, parameter check

Before the real logic processing, a series of checks should be carried out on the input parameters to maintain the lightweight of the subsequent business processing logic, which is also the guidance of fast fail thought, and if there is an error, the processing should be finished as soon as possible.

What exactly is it? We assume that the parameter is m.

If (null = = m) {return;}

Make a null judgment to prevent the occurrence of NullPointerException with m, but it is not recommended to throw NPE here, because it will be very confusing to see the log.

If (StringUtils.isEmpty (m)) {return;}

Whether the string is an empty string

If (CollectionUtils.isEmpty.isEmpty (m)) {return;}

Whether the collection is empty or null

Try {

JSON.parseObject (m)

Return true

}

Catch (JSONExceptin e) {

Return false

}

Determine whether the string is in JSON format

III. Retry mechanism

For specific external system errors, you can try to retry this strategy many times, of course, under the premise that the resume is idempotent in the other party's service. This can improve the success rate of response in some cases of network instability.

IV. Idempotent mechanism

What is idempotent? It means that no matter when, anywhere or anyone, as long as it is a pre-emptive request, there should be the same response until the final state is reached.

This principle does not pay attention to the result of the last implementation, and Penguin should not fail due to some intermediate state inconsistency caused by the partial success or failure of the last request.

5. Lambda

Optionl.of (target)

.getOrElse (new ArrayList ())

.filter (Object::NotNull)

.forEach (()-> {})

This method of writing ensures that most of the exceptions do not occur, especially when dealing with the collection, because as long as one of the values in the collection will cause the program to fail, the whole program will report an error. Writing in this way, because the data is checked and compatible, the probability of error is relatively low, but there is also a drawback, that is, when such programs are abnormal, developers generally do not know where to start. It is already very difficult to locate which line of data it is.

At this point, I believe that you have a deeper understanding of "what are the robust methods of Java?" you might as well do it in practice. Here is the website, more related content can enter the relevant channels to inquire, follow us, continue to learn!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report