Java, Software Development, Spring, Spring Boot

Spring Boot Asynchronous Controller Without External Queues

This post shows how to create an asynchronous web controller or endpoint in Spring Boot (or Spring in general). The codes won’t use external queues of any kind. However, they’ll use ThreadPoolTaskExecutor to run codes in the background without having a client wait for a response.  Why do we need an asynchronous endpoint or web controller?  The reason is simple – the processing of data may take longer than the client expects. Instead of making the client wait after it sends a request, our service application sends the response back by calling back the client’s URL at a later time typically, after the processing completes.

Stuff This Post Uses

Although some are optional, these are the following stuff this post uses.

  1. Windows 10
  2. Spring Boot 2.5.0
  3. JDK 11
  4. Spring Initializr
  5. IntelliJ IDEA – Optional

Generate Spring Boot Project Using Spring Initialzr

To create an asynchronous web controller in Spring Boot, we use two Java annotations with Spring – @EnableAsync, and @Async. Therefore, it is easier to set our codebase up using Spring Initialzr.  First, generate a Spring Boot project using Spring Initialzr with Spring Web dependency only.  Spring Web allows us the use the @RestController annotation. Then, our pom.xml may look something like the following, where we only have two dependencies –  Spring Boot Starter Web and Spring Boot Starter Test.

Spring Boot Asynchronous Codes 

In reality, there is no asynchronous controller (or controller methods) because we need to respond to the client as soon as possible with HTTP 20x. Meaning, if we had to convert an existing synchronous endpoint to an asynchronous, we retain the controller’s method’s signature and immediately respond. What we instead do is create a service class with an asynchronous method. Then, before the controller responded to the client, it calls that other class’ asynchronous method. Get the picture so far? If yes, then we proceed with more codes.

Next, we need to create a Java configuration class. We could modify an existing configuration class, but a separate class would make things neat.

Underneath, asynchronous methods use Thread. 

Next, we create a class with a public method and annotate it with @Async. For our purpose, we annotate the class with  @Service

To simulate a long-running process (but not long enough for a job), we shortly pause the operation using TimeUnit.MINUTES.sleep(1).  With this, we should see an immediate response from our controller before our async method prints out “Done!”.

Finally, we create our REST controller.

Testing Our Spring Boot Application Asynchronous REST Controller’

Finally, we can test our Spring Boot application! As you can see on the screenshot below, we immediately received an HTTP response from our endpoint while the application still processes the back-end request.

Spring Boot Asynchronous Controller

The service application can take its time processing the request and reach out to the client later using callbacks.

Loading

Got comments or suggestions? We disabled the comments on this site to fight off spammers, but you can still contact us via our Facebook page!.


You Might Also Like