簡體   English   中英

帶有多線程的REST Api用於在Spring Boot中處理文件

[英]REST Api with Multithreading for handling Files in Spring Boot

是他們的任何方式,這樣我就可以利用多線程概念並行調用執行程序,並加快創建的@RestController執行速度,該@RestController將接受StringList<MultipartFile>作為請求參數,並且代碼運行正常。是如果我通過for循環解析另一個文件。 執行所花費的時間更多。

以下是控制器

@RequestMapping(value = "/csvUpload", method = RequestMethod.POST)
    public List<String> csvUpload(@RequestParam String parentPkId, @RequestParam List<MultipartFile> file)
            throws IOException {
        log.info("Entered method csvUpload() of DaoController.class");
        List<String> response = new ArrayList<String>();
        String temp = parentPkId.replaceAll("[-+.^:,]", "");
        for (MultipartFile f : file) {
            String resp = uploadService.csvUpload(temp, f);
            response.add(resp);
        }
        return response;
    }

從控制器中,我正在調用uploadService.csvUpload()方法,其中我正在使用For循環,一個接一個地解析文件。

以下是我的UploadService類

public String csvUpload(String parentPkId, MultipartFile file) {
        try {
            BufferedReader br = new BufferedReader(new InputStreamReader(file.getInputStream()));
            String line = "";
            int header = 0;
            while ((line = br.readLine()) != null) {
                // TO SKIP HEADER
                if(header == 0) {
                    header++;  
                    continue;
                }
                header++;
                //Use Comma As Separator
                String[] csvDataSet = line.split(",");
                //Saving it to DB

        }catch(IOException ex) {
            ex.printStackTrace();
        }
        return "Successfully Uploaded "+ file.getOriginalFilename();
    }

如何使該控制器成為多線程,以便處理並行且快速。 我是多線程技術的新手,我嘗試使用Callable接口,但Call()方法將不使用參數。

歡迎任何線索和建議,謝謝。

您可以使用並行流執行上傳代碼

    List<String> response = file.parallelStream().map(f -> uploadService.csvUpload(temp, f))
    .collect(Collectors.toList());

您可以串行或並行執行流。 當流並行執行時,Java運行時將流划分為多個子流。 聚合操作迭代並並行處理這些子流,然后合並結果。

您需要創建一個類別,該類別將實現如下所示的callable並將期貨存儲在列表中,並最終如下處理期貨

public class ProcessMutlipartFile implements Callable<String>
{
   private Mutlipartfile file; 
   private String temp;
   private UploadService uploadService;
   public ProcessMutlipartFile(Mutlipartfile file,String temp, UploadService uploadService )
   {
       this.file=file;
       this.temp=temp,
       this.uploadService=uploadService;
   }

   public String call() throws Exception 
   {

    return   uploadService.csvUpload(temp, file);
    }

 }

在您的控制器中創建將來對象的列表

 ExecutorService executor = Executors.newFixedThreadPool(10)
List< Future<String> > futureList = new ArrayList<Future<String>>();
     .
     .
     .
         for (MultipartFile f : file) {
                    futureList.add(executor.submit(new ProcessMutlipartFile(file ,temp,uploadService));
                }

終於在您的控制器中

 for (Future f :futureList)
 {
  response.add(f.get());
  }

//shuttingdown the Executor
executor.shutdown();

希望這可以幫助

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM