How to do asynchronous REST using Spring?

I am trying to do a little REST using Spring Boot. I have never used Spring or used Java a long time ago (Java 7)!

Over the past 2 years, I have only used Python and C # (but, as I said, I have already used Java).

So now I am trying to create REST using asynchronous methods and have checked some examples, but still I do not really understand the β€œright way” to do this.

Looking at the following documentation: http://carlmartensen.com/completablefuture-deferredresult-async , Java 8 has CompletableFuturewhich I can use with Spring, so I made the following code:

Service :

@Service
public class UserService {
  private UserRepository userRepository;

  // dependency injection
  // don't need Autowire here
  // https://docs.spring.io/spring-boot/docs/current/reference/html/using-boot-spring-beans-and-dependency-injection.html
  public UserService(UserRepository userRepository) {
    this.userRepository = userRepository;
  }

  @Async
  public CompletableFuture<User> findByEmail(String email) throws InterrupedException {
    User user = userRepository.findByEmail(email);
    return CompletableFuture.completedFuture(user);
  }
}

Repository :

public interface UserRepository extends MongoRepository<User, String> {
  @Async
  findByEmail(String email);
}

RestController :

@RestController
public class TestController {

  private UserService userService;

  public TestController(UserService userService) {
    this.userService = userService;
  }

  @RequestMapping(value = "test")
  public @ResponseBody CompletableFuture<User> test(@RequestParam(value = "email", required = true) String email) throws InterruptedException {
    return userService.findByEmail(email).thenApplyAsync(user -> {
      return user;
    })
  }  
}

. , (, ), , Spring ( ):

  @RequestMapping(value = "test")
  public @ResponseBody CompletableFuture<User> test(@RequestParam(value = "email", required = true) String email) throws InterruptedException {
    return userService.findByEmail(email);
  }  
}

?

, : https://spring.io/guides/gs/async-method/, @EnableAsync SpringBootApplication. @EnableAsync @EnableAsync asyncExecutor , /test ( 200 OK, ).

, @EnableAsync? , @EnableAsync, ?

+13
2

, @Async findEmail UserRepository, , , , . User user = userRepository.findByEmail(email); findByEmail List.

@Async , @EnableAsync, @EnableAsync @Async findEmail .

return userService.findByEmail(email); CompletableFuture, UserService.

, thenApplyAsync CompletableFuture , userService.findByEmail(email) , CompletableFuture.

 return userService.findByEmail(email).thenApplyAsync(user -> {
      return user;
    })

, @Async findByEmail, , , @EnableAsync

, , , 2 , , method1, method2 , , method3, 6 . , 2 6.

:

@Async
public  CompletableFuture<Boolean> veryLongMethod()  {

    try {
        Thread.sleep(2000L);
    } catch (InterruptedException e) {
        e.printStackTrace();
    }

    return CompletableFuture.completedFuture(true);
}

,

  @RequestMapping(value = "test")
  public @ResponseBody CompletableFuture<User> test(@RequestParam(value = "email", required = true) String email) throws InterruptedException {
        CompletableFuture<Boolean> boolean1= siteService.veryLongMethod();
        CompletableFuture<Boolean> boolean2= siteService.veryLongMethod();
        CompletableFuture<Boolean> boolean3= siteService.veryLongMethod();

        CompletableFuture.allOf(boolean1,boolean2,boolean3).join();
    return userService.findByEmail(email);
  }  

, , , 6 , , 2 , .

. : @Async, Spring, CompletableFuture

, .

+8

Async. ( 20 30 ). ThreadPoolTaskExecutor() SpringBoot. , , .

+1

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


All Articles