gpt4 book ai didi

spring-boot - Spring Boot 后台作业

转载 作者:行者123 更新时间:2023-12-04 17:52:29 24 4
gpt4 key购买 nike

我正在将 Spring Boot 与 Thymeleaf 结合使用。

当用户单击相关按钮时,它会发送发布请求,并且在相关 Controller 方法中有一个需要 20 分钟的函数。此函数不返回值。

我只想在后台处理这个函数。当应用程序到达这个函数的行时,它应该向这个函数发送参数并继续处理而不等待返回。

这种情况下的最佳做法是什么?

非常感谢。

更新

我的配置类

@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 {


.....

}

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 通过@Async@EnableAsync 为异步方法执行提供注释支持:https://spring.io/guides/gs/async-method/

关于spring-boot - Spring Boot 后台作业,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43517924/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com