Java spring boot многопоточность

Multi-Threading in Spring Boot Using CompletableFuture

Join the DZone community and get the full member experience.

Multi-threading is similar to multi-tasking, but it enables the processing of executing multiple threads simultaneously, rather than multiple processes. CompletableFuture , which was introduced in Java 8, provides an easy way to write asynchronous, non-blocking, and multi-threaded code.

The Future interface was introduced in Java 5 to handle asynchronous computations. But, this interface did not have any methods to combine multiple asynchronous computations and handle all the possible errors. The CompletableFuture implements Future interface, it can combine multiple asynchronous computations, handle possible errors and offers much more capabilities.

Let’s get down to writing some code and see the benefits.

Create a sample Spring Boot project and add the following dependencies.

  4.0.0 com.techshard.future springboot-future 1.0-SNAPSHOT org.springframework.boot spring-boot-starter-parent 2.1.8.RELEASE  UTF-8 UTF-8   org.springframework.boot spring-boot-starter-web  org.springframework.boot spring-boot-starter-data-jpa  com.h2database h2 runtime  org.slf4j slf4j-api  org.projectlombok lombok 1.18.10 true     org.springframework.boot spring-boot-maven-plugin    

In this article, we will be using sample data about cars. We will create a JPA entity Car and a corresponding JPA repository.

package com.techshard.future.dao.entity; import lombok.Data; import lombok.EqualsAndHashCode; import javax.persistence.*; import javax.validation.constraints.NotNull; import java.io.Serializable; @Data @EqualsAndHashCode @Entity public class Car implements Serializable

package com.techshard.future.dao.repository; import com.techshard.future.dao.entity.Car; import org.springframework.data.jpa.repository.JpaRepository; import org.springframework.stereotype.Repository; @Repository public interface CarRepository extends JpaRepository

Let us now create a configuration class that will be used to enable and configure the asynchronous method execution.

package com.techshard.future; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.scheduling.annotation.EnableAsync; import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor; import java.util.concurrent.Executor; @Configuration @EnableAsync public class AsyncConfiguration < private static final Logger LOGGER = LoggerFactory.getLogger(AsyncConfiguration.class); @Bean (name = "taskExecutor") public Executor taskExecutor() < LOGGER.debug("Creating Async Task Executor"); final ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor(); executor.setCorePoolSize(2); executor.setMaxPoolSize(2); executor.setQueueCapacity(100); executor.setThreadNamePrefix("CarThread-"); executor.initialize(); return executor; >>

The @EnableAsync annotation enables Spring’s ability to run @Async methods in a background thread pool. The bean taskExecutor helps to customize the thread executor such as configuring the number of threads for an application, queue limit size, and so on. Spring will specifically look for this bean when the server is started. If this bean is not defined, Spring will create SimpleAsyncTaskExecutor by default.

We will now create a service and @Async methods.

package com.techshard.future.service; import com.techshard.future.dao.entity.Car; import com.techshard.future.dao.repository.CarRepository; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.scheduling.annotation.Async; import org.springframework.stereotype.Service; import org.springframework.web.multipart.MultipartFile; import java.io.*; import java.util.ArrayList; import java.util.List; import java.util.concurrent.CompletableFuture; @Service public class CarService < private static final Logger LOGGER = LoggerFactory.getLogger(CarService.class); @Autowired private CarRepository carRepository; @Async public CompletableFuture> saveCars(final MultipartFile file) throws Exception < final long start = System.currentTimeMillis(); Listcars = parseCSVFile(file); LOGGER.info("Saving a list of cars of size <> records", cars.size()); cars = carRepository.saveAll(cars); LOGGER.info("Elapsed time: <>", (System.currentTimeMillis() - start)); return CompletableFuture.completedFuture(cars); > private List parseCSVFile(final MultipartFile file) throws Exception < final Listcars=new ArrayList<>(); try < try (final BufferedReader br = new BufferedReader(new InputStreamReader(file.getInputStream()))) < String line; while ((line=br.readLine()) != null) < final String[] data=line.split(";"); final Car car=new Car(); car.setManufacturer(data[0]); car.setModel(data[1]); car.setType(data[2]); cars.add(car); >return cars; > > catch(final IOException e) < LOGGER.error("Failed to parse CSV file <>", e); throw new Exception("Failed to parse CSV file <>", e); > > @Async public CompletableFuture> getAllCars() < LOGGER.info("Request to get a list of cars"); final Listcars = carRepository.findAll(); return CompletableFuture.completedFuture(cars); > >

Here, we have two @Async methods: saveCar() and getAllCars() . The first one accepts a multipart file, parses it, and stores the data in the database. The second method reads the data from the database.

Both methods are returning a new CompletableFuture that was already completed with the given values.

Let us create a Rest Controller and provide some endpoints:

package com.techshard.future.controller; import com.techshard.future.dao.entity.Car; import com.techshard.future.service.CarService; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.http.HttpStatus; import org.springframework.http.MediaType; import org.springframework.http.ResponseEntity; import org.springframework.web.bind.annotation.*; import org.springframework.web.multipart.MultipartFile; import java.io.File; import java.util.List; import java.util.concurrent.CompletableFuture; import java.util.function.Function; @RestController @RequestMapping("/api/car") public class CarController < private static final Logger LOGGER = LoggerFactory.getLogger(CarController.class); @Autowired private CarService carService; @RequestMapping (method = RequestMethod.POST, consumes=, produces=) public @ResponseBody ResponseEntity uploadFile( @RequestParam (value = "files") MultipartFile[] files) < try < for(final MultipartFile file: files) < carService.saveCars(file); >return ResponseEntity.status(HttpStatus.CREATED).build(); > catch(final Exception e) < return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).build(); >> @RequestMapping (method = RequestMethod.GET, consumes=, produces=) public @ResponseBody CompletableFuture getAllCars() < return carService.getAllCars().thenApply(ResponseEntity::ok) .exceptionally(handleGetCarFailure); > private static Function>> handleGetCarFailure = throwable -> < LOGGER.error("Failed to read records: <>", throwable); return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).build(); >; >

The first REST endpoint accepts a list of multipart files. The second endpoint is to read the data. As you notice the GET endpoint, there is some difference in the return statement. We are returning a list of cars and we are also handling exceptions.

The function handleGetCarFailure is invoked when the CompletableFuture completes exceptionally, otherwise, if this CompletableFuture completes normally, it returns a list of cars to the client.

Testing the Application

Run the Spring Boot Application. Once the server is started, test the POST endpoint. The sample screenshot from Postman tool.

Postman tool

Make sure to provide Content-Type as multipart\form-data in the headers section. When you send a request, you will notice that two threads have started at the same time, one thread for each file.

Two threads starting at the same time

Let us now test the GET endpoint.

Testing the GET endpoint

Now, just modify the GET endpoint as follows:

@RequestMapping (method = RequestMethod.GET, consumes=, produces=) public @ResponseBody ResponseEntity getAllCars() < try < CompletableFuture> cars1=carService.getAllCars(); CompletableFuture> cars2=carService.getAllCars(); CompletableFuture> cars3=carService.getAllCars(); CompletableFuture.allOf(cars1, cars2, cars3).join(); return ResponseEntity.status(HttpStatus.OK).build(); > catch(final Exception e) < return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).build(); >>

Here, we are calling the Async method 3 times. The CompletableFuture.allOf() will wait until all the CompletableFutures have been completed, and join() will combine the results. Note that this is just for demonstration purposes.

Add Thread.sleep(1000L) in getAllCars() of the CarService class. We are giving a delay of 1 second just for testing purpose.

Restart the application and test GET endpoint again.

Restarting the app and testing the GET endpoint again

As you see in the above screenshot, the first two calls to the Async method have started simultaneously. The third call has started with a delay of 1 second.

Remember that we have configured only 2 threads that can be used simultaneously. When at least one of the two threads becomes free, the third request to the Async method will be made.

Conclusion

In this article, we’ve seen some typical use cases of the CompletableFuture . Let me know if you have any comments or suggestions in the comments section below.

The source code for this article can be found on this GitHub repository.

Further Reading

Published at DZone with permission of Swathi Prasad , DZone MVB . See the original article here.

Opinions expressed by DZone contributors are their own.

Источник

Читайте также:  Php ini set load extension
Оцените статью