Spring boot Spring启动后台作业
我用的是带Thymeleaf的弹簧靴 当用户点击相关按钮时,它发送post请求,在相关控制器方法中有一个需要20分钟的功能。此函数不返回值 我只想在后台处理这个函数。当应用程序到达this函数的行时,它应该向该函数发送参数,并在不等待返回的情况下继续处理 这种情况下的最佳做法是什么 非常感谢 更新 我的配置类Spring boot Spring启动后台作业,spring-boot,background,thymeleaf,Spring Boot,Background,Thymeleaf,我用的是带Thymeleaf的弹簧靴 当用户点击相关按钮时,它发送post请求,在相关控制器方法中有一个需要20分钟的功能。此函数不返回值 我只想在后台处理这个函数。当应用程序到达this函数的行时,它应该向该函数发送参数,并在不等待返回的情况下继续处理 这种情况下的最佳做法是什么 非常感谢 更新 我的配置类 @Configuration @EnableAsync public class SpringAsyncConfig implements AsyncConfigurer{ @Bean(
@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 {
.....
}
@服务
公共类OCR服务{
@异步的
公共字符串treadliOcr(列表列表,字符串角)抛出
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";
}
@RequestMapping(value=“/aktar”,method=RequestMethod.POST)
公共字符串aktar(@RequestParam(“belgedi”)字符串belgedi,
@RequestParam(“evrakTurId”)字符串evrakTurId,
@RequestParam(“kategoriId”)字符串kategoriId,
@RequestParam(“belgeTurId”)字符串belgeTurId,
@RequestParam(“firmaId”)字符串firmaId,
@RequestParam(“projeId”)字符串projeId,
@RequestParam(“aciklama”)字符串(aciklama)引发InterruptedException、IOException、TesseractException{
整数b=null;
整数p=null;
字符串klasor=getInitYol();
字符串belgeOnAd=belgeAdi.子字符串(0,14);
BelgeIsimleri BelgeIsimleri=新BelgeIsimleri();
List seciliListe=belgeIsimleri.secilibelgelerifilterle(klasor,belgeOnAd);
for(字符串s:seciliListe){
File File=新文件(getInitYol()+s);
if(file.renameTo(新文件(“D:\\Done\\\”+s))){
delete();
System.out.println(s+“yi sildi”);
}
}
OcrService ocr=新的OcrService();
String result=ocr.treadliOcr(seciliListe,getInitYol());
System.out.println(“Ocr dan döndü”);
整数et=整数.valueOf(evrakTurId);
整数k=整数.valueOf(kategoriId);
if(null==belgeTurId | |“.equals(belgeTurId)){
}否则{
b=整数值(belgeTurId);
}
整数f=整数.valueOf(firmaId);
if(null==projeId | |“.equals(projeId)){
}否则{
p=整数值(projeId);
}
belgeRepo.save(新BelgeEntity(et、k、b、f、p、aciklama、result、belgeOnAd));
返回“重定向:/verigiris”;
}
Spring通过@Async
和@enablesync
:可能通过@Async
?这里的例子:我已经检查了这个指南。问题是,在@Async方法完成之前,它不会进入下一行。我已经检查了这个指南。问题是在@Async方法完成之前,它不会进入下一行。您是否添加了@enablesync
注释?我已经添加了所有需要的注释,但它仍等待返回值。主题将用当前值更新。它现在正在返回字符串值。但即使它返回void,它仍然会等待方法完成。您正在使用新的操作符调用服务!使用spring自动连线
注入它。此外,您还应该重新考虑您的逻辑,如前所述,函数不返回值,这适用于异步方法,现在您正在强制返回值?!!它不应该是字符串,而应该是未来的。你是说我需要使用@Inject而不是@Autowired?