我正在使用Thymeleaf春季啓動。春季啓動背景作業
當用戶點擊相關按鈕時,它發送post requst,在相關的控制器方法中有一個需要20分鐘的功能。這個函數不會返回一個值。
我只是想在後臺處理這個功能。當應用程序進入這個函數的行時,它應該向這個函數發送參數並且不用等待返回就繼續處理。
這種情況的最佳做法是什麼?
非常感謝提前。
UPDATE
我的配置類
@Configuration
@EnableAsync
public class SpringAsyncConfig implements AsyncConfigurer{
@Bean(name = "ocrThread-")
public Executor threadPoolTaskExecutor() {
return new ThreadPoolTaskExecutor();
}
@Override
public Executor getAsyncExecutor() {
ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor();
executor.setCorePoolSize(2);
executor.setMaxPoolSize(2);
executor.setQueueCapacity(10);
executor.initialize();
return executor;
}
@Override
public AsyncUncaughtExceptionHandler getAsyncUncaughtExceptionHandler() {
// TODO Auto-generated method stub
return null;
}
}
服務類
@Service
public class OcrService {
@Async
public String treadliOcr(List<String> liste, String kok) throws
InterruptedException, IOException, TesseractException {
.....
}
}
控制器
@RequestMapping(value="/aktar", method= RequestMethod.POST)
public String aktar(@RequestParam("belgeAdi") String belgeAdi,
@RequestParam("evrakTurId") String evrakTurId,
@RequestParam("kategoriId") String kategoriId,
@RequestParam("belgeTurId") String belgeTurId,
@RequestParam("firmaId") String firmaId,
@RequestParam("projeId") String projeId,
@RequestParam("aciklama") String aciklama) throws InterruptedException, IOException, TesseractException{
Integer b = null;
Integer p = null;
String klasor = getInitYol();
String belgeOnAd = belgeAdi.substring(0, 14);
BelgeIsimleri belgeIsimleri = new BelgeIsimleri();
List<String> seciliListe = belgeIsimleri.seciliBelgeleriFiltrele(klasor, belgeOnAd);
for(String s:seciliListe){
File file = new File (getInitYol()+s);
if(file.renameTo(new File("D:\\Done\\"+s))){
file.delete();
System.out.println(s+"yi sildi");
}
}
OcrService ocr = new OcrService();
String result=ocr.treadliOcr(seciliListe,getInitYol());
System.out.println("Ocr dan döndü");
Integer et = Integer.valueOf(evrakTurId);
Integer k = Integer.valueOf(kategoriId);
if(null==belgeTurId || "".equals(belgeTurId)){
}else{
b = Integer.valueOf(belgeTurId);
}
Integer f = Integer.valueOf(firmaId);
if(null==projeId || "".equals(projeId)){
}else{
p = Integer.valueOf(projeId);
}
belgeRepo.save(new BelgeEntity(et,k ,b , f ,p ,aciklama, result,belgeOnAd));
return "redirect:/verigiris";
}
也許與'@ Async'?示例:https://spring.io/guides/gs/async-method/ –
我已經檢查過本指南。問題是,在@Async方法完成之前,它並沒有進入下一行。 – Murmelto