SpingBoot之优雅的异步方法调用

场景:Java中调用python,有些不稳定,有时候非常快,有时候慢。

在保证不污染原有代码的基础上,进行异步方法调用:

 Service代码:

public interface AsyncService {

    /**
     * 异步执行pathon
     * @param cmdArr
     * @return
     */
    void Async(String[] cmdArr);

}
import com.atage.op.service.AsyncService;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;

/**
 * 

* 服务实现类 *

* * @author 鲁达 * @since 2019-09-05 */ @Service public class AsyncServiceImpl implements AsyncService { @Async @Override public void Async(String[] cmdArr) { try { //要执行的业务代码 Runtime.getRuntime().exec(cmdArr); }catch (Exception ex){ ex.printStackTrace(); } } }

Controller代码:

类上加注解:@EnableAsync
@Autowired
private AsyncService asyncService;
调用:
try {
      asyncService.Async(cmdArr);
}catch(Exception e) {
      throw new RuntimeException("业务程序报错啦!!");
}

 

 

你可能感兴趣的:(SpringCloud,Async,SpringBoot)