Introduction:
In this post, we'll look at how to implement multithreading in a Spring Boot application using books inventory as an example. Multithreading allows the application to handle multiple requests concurrently, improving performance and responsiveness.
We'll use a three-layer architecture, consisting of a Controller layer, a Service layer, and a Repository layer, and use the JpaRepository and @Async annotations provided by Spring to implement the multithreading functionality.
By the end of this post, you'll have a basic understanding of how to implement multithreading in a Spring Boot application, and how to use the CompletableFuture class to retrieve the result of an asynchronous operation. This can help you build high-performance, scalable applications that can handle multiple requests concurrently.
Controller :
import java.util.concurrent.CompletableFuture;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class BookController {
@Autowired
private BookService bookService;
@GetMapping("/books")
public CompletableFuture<Iterable<Book>> getBooks() {
return bookService.getBooks();
}
}
Service :
import java.util.concurrent.CompletableFuture;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;
@Service
public class BookService {
@Autowired
private BookRepository bookRepository;
@Async
public CompletableFuture<Iterable<Book>> getBooks() {
Iterable<Book> books = bookRepository.findAll();
return CompletableFuture.completedFuture(books);
}
}
Repository:
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.stereotype.Repository;
@Repository
public interface BookRepository extends JpaRepository<Book, Integer> {
}
Model :
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
@Entity
public class Book {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private int id;
private String title;
private String author;
private int quantity;
// getters and setters
}
Explanation:
The BookController class is a RESTful web service endpoint that maps HTTP requests to methods in the BookService class.
The BookService class implements business logic and uses the BookRepository to perform database operations. The getBooks method is marked with the @Async annotation, which indicates that this method should run in a separate thread and return a CompletableFuture object.
The BookRepository extends the JpaRepository and provides methods for querying and manipulating the books inventory.
The Book model represents a single book in the inventory, with attributes such as id, title, author, and quantity. The id attribute is annotated with @Id and @GeneratedValue, which indicates that this is the primary key for the Book entity and its value is generated automatically by the database.
When the /books endpoint is accessed, the getBooks method in the BookService is invoked and returns a CompletableFuture object that contains the books inventory. The CompletableFuture allows the caller to retrieve the result of the asynchronous operation in a non-blocking manner.
In this example, the use of multithreading allows the application to handle multiple requests concurrently and improve performance, while still maintaining the consistency of the books inventory.
Comentários