I am using Spring Boot with Thymeleaf.
When user click on related button, it sends post requst and in the related controller method there is a function which takes 20 mins. This function does not return a value.
I just want to process this function in background. When application comes to the this function's line, it should send parameters to this function and keep processing without waiting a return.
What is the best practice for this case?
Many thanks in advance.
UPDATE
My config class
@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 class
@Service
public class OcrService {
@Async
public String treadliOcr(List<String> liste, String kok) throws
InterruptedException, IOException, TesseractException {
.....
}
}
Controller
@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";
}
Spring provides annotation support for asynchronous method execution via @Async and @EnableAsync: https://spring.io/guides/gs/async-method/
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With