简体   繁体   English

Spring 启动:使用异步方法作为同步方法

[英]Spring boot: Using Async method as sync method

I have some scenario我有一些场景

  1. scheduler(call repeatedly) should call for a async function say fun().调度程序(重复调用)应该调用异步 function 说 fun()。
  2. Controller should call same function fun() and return result based on successful completion(By checking exception) Controller 应该调用相同的 function fun() 并根据成功完成返回结果(通过检查异常)

Please provide me some skeleton with appropriate notations.请给我一些带有适当符号的骨架。

You should have your method as sync, but if you want to call your async methd, inside of your async method you should call your sync method.您应该将方法设置为同步,但如果您想调用异步方法,则应在异步方法内部调用同步方法。

public void mySyncMethod(){
   //whatever you want. 
}

Async method异步方法

@Async
public void myAsyncMethod(){
   mySyncMethod();
}

I supposed you are using spring boot我想你正在使用 spring 引导

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM