Should every REST endpoint be asynchronous in Java EE?

When using NetBeans and writing an arbitrary REST endpoint, NetBeans always displays a warning that the method may be converted to asynchronous.

For example, I create the following method:

@GET
@Path("/test")
public String hello() {
    return "Hello World!";
}

NetBeans then displays a warning, see below:

Convert to asynchronous

Clicking on the tooltip generates this code:

private final ExecutorService executorService = java.util.concurrent.Executors.newCachedThreadPool();

@GET
@Path(value = "/test")
public void hello(@Suspended final AsyncResponse asyncResponse) {
    executorService.submit(new Runnable() {
        @Override
        public void run() {
            asyncResponse.resume(doHello());
        }
    });
}

private String doHello() {
    return "Hello World!";
}

The same is true when creating a PUT or POST method. Since NetBeans always shows a warning when implementing a REST endpoint, this tells me that writing synchronous endpoints is considered bad / bad practice. So, should each REST endpoint be asynchronous? What for?

+6
source share
3 answers

mode, , HTTP . , . , . . , . - . , , . ( )

.

+2

, . :

  1. , ?

  2. , , ?

, . .

Netbeans , , , - , . , .

, JAX-RS , Netbeans ( , , , , - .

, , : : , , , , , .

+1

  • , -. .
  • 100 ; 900 ; 1 .
  • 100 , 100 -.
  • 100 , 100 -.

  • , IO. .
  • - 100 ; 100 .
  • 100 , 10 - .
  • 900 ; 900 .
  • 100 , 90 .

, .
- :

  • , - , ?
    • sync/one pool 100 ; async/ 10 -.
  • , ?
  • ?
    • , - . .

For a simple application, it does not matter if you synchronize endpoints or run them asynchronously; but in general, with a decent number of requests per second and various tasks with different characteristics (processing time, the need to generate your own child threads, priority), making your endpoints asynchronous is the best way to have a highly sensitive system, while efficient use of resources .

+1
source

Source: https://habr.com/ru/post/1687113/


All Articles