SpingBoot élégante l'appel de méthode asynchrone

Scène: Java python d'appel, un peu instable, parfois très rapide, parfois lent.

Pour éviter toute contamination du code d'origine en fonction de l'appel de méthode asynchrone:

 Code de 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;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @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();
        }
    }
}

Code de commande:

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

 

 

Publié 130 articles originaux · louange gagné 379 · vues 470 000 +

Je suppose que tu aimes

Origine blog.csdn.net/qq_31122833/article/details/103391112
conseillé
Classement