public interface AsyncService { void asyncInvoke(AsyncExec consumer) throws WxErrorException; @FunctionalInterface interface AsyncExec { void exec() throws WxErrorException; } }
@Service public class AsyncServiceImpl implements AsyncService { @Async @Override public void asyncInvoke(AsyncExec asyncExec) throws WxErrorException { asyncExec.exec(); } }
@RunWith(SpringRunner.class) @SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT) @ActiveProfiles("dev") @Slf4j public class AsyncServiceTest { @Autowired private AsyncService asyncService; @Test public void asyncServiceTest() throws WxErrorException, InterruptedException { log.info("111111111111111"); asyncService.asyncInvoke(() -> { try { Thread.sleep(1000); log.info("异步方法执行"); } catch (InterruptedException e) { e.printStackTrace(); } }); log.info("222222222222222"); Thread.sleep(2000); } }