Async With CompletableFuture
Async With CompletableFuture
Wrap the processing logic inside a CompletableFuture to perform the upload operation
asynchronously.
java
Copy code
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;
import java.util.concurrent.CompletableFuture;
@Service
@Async
Spring Batch:
Define a job with steps to read, process, and write data from the CSV file to the database.
Configure chunk-based processing to process data in chunks, reducing memory consumption and
improving performance.
java
Copy code
import org.springframework.batch.core.Job;
import org.springframework.batch.core.Step;
import org.springframework.batch.core.configuration.annotation.EnableBatchProcessing;
import org.springframework.batch.core.configuration.annotation.JobBuilderFactory;
import org.springframework.batch.core.configuration.annotation.StepBuilderFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.multipart.MultipartFile;
@Configuration
@EnableBatchProcessing
@Autowired
@Autowired
@Bean
return jobBuilderFactory.get("csvUploadJob")
.start(csvUploadStep())
.build();
}
@Bean
return stepBuilderFactory.get("csvUploadStep")
.reader(csvItemReader())
.processor(csvItemProcessor())
.writer(csvItemWriter())
.build();
Integration:
Trigger the Spring Batch job asynchronously from your controller or service method using
CompletableFuture.
Monitor the completion of the job and handle the response accordingly.
java
Copy code
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.multipart.MultipartFile;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.CompletableFuture;
@Controller
@Autowired
private StockService stockService;
@PostMapping("/upload-csv")
try {
if (file.isEmpty()) {
response.put("success", false);
return ResponseEntity.badRequest().body(response);
uploadResult.thenAccept(success -> {
if (success) {
response.put("success", true);
} else {
response.put("success", false);
});
return ResponseEntity.ok().body(response);
} catch (Exception e) {
response.put("success", false);
return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(response);
}
}
By leveraging asynchronous processing with CompletableFuture and integrating Spring Batch for
batch processing, you can significantly improve the execution time and scalability of your CSV upload
operation.
Today
@Service
@Autowired
@Autowired
try {
Files.write(path, bytes);
.addString("filePath", path.toAbsolutePath().toString())
.toJobParameters());
Files.deleteIfExists(path);
} catch (Exception e) {
// Handle exception
e.printStackTrace();