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

How to use a specific execution program in the server to block operations

2025-01-21 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

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

This article will explain in detail how to use a specific execution program to prevent operations on the server. The editor thinks it is very practical, so I share it with you as a reference. I hope you can get something after reading this article.

Use a specific execution program to block the operation

The entire server becomes unresponsive because of a slow operation, which is usually not what developers want. Unfortunately, for RPC, the response time is usually unpredictable.

Suppose the server has 100 worker threads and an endpoint called 100 RPS. Internally, it makes a RPC call, which usually takes 10 milliseconds. At some point, the response time of this RPC becomes 2 seconds, and the only thing the server can do during peak periods is to wait for these calls while other endpoints cannot access them.

@ GET@Path ("/ genre/ {name}") @ Produces (MediaType.APPLICATION_JSON) public Response getGenre (@ PathParam ("name") String genreName) {Genre genre = potentiallyVerySlowSynchronousCall (genreName); return Response.ok (genre). Build ();}

The easiest way to solve this problem is to submit the code, which turns the blocking call into a thread pool:

@ GET@Path ("/ genre/ {name}") @ Produces (MediaType.APPLICATION_JSON) public void getGenre (@ PathParam ("name") String genreName, @ Suspended AsyncResponse response) {response.setTimeout (1L, TimeUnit.SECONDS); executorService.submit (()-> {Genre genre = potentiallyVerySlowSynchronousCall (genreName); return response.resume (Response.ok (genre). Build ());}) } this is the end of the article on "how to use specific execution programs in the server to prevent operations". I hope the above content can be of some help to you, so that you can learn more knowledge. If you think the article is good, please share it for more people to see.

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

Development

Wechat

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

12
Report