@Async public CompletableFuture<User> findUser(String user) throws InterruptedException { logger.info("Looking up " + user); String url = String.format("https://api.github.com/users/%s", user); User results = restTemplate.getForObject(url, User.class); // Artificial delay of 1s for demonstration purposes Thread.sleep(1000L); return CompletableFuture.completedFuture(results); }
@Async CompletableFuture<List<Customer>> readAllBy(); }
/** * Return the qualifier or bean name of the executor to be used when executing the * given method, specified via {@link Async#value} at the method or declaring * class level. If {@code @Async} is specified at both the method and class level, the * method's {@code #value} takes precedence (even if empty string, indicating that * the default executor should be used preferentially). * @param method the method to inspect for executor qualifier metadata * @return the qualifier if specified, otherwise empty string indicating that the * {@linkplain #setExecutor(Executor) default executor} should be used * @see #determineAsyncExecutor(Method) */ @Override @Nullable protected String getExecutorQualifier(Method method) { // Maintainer's note: changes made here should also be made in // AnnotationAsyncExecutionAspect#getExecutorQualifier Async async = AnnotatedElementUtils.findMergedAnnotation(method, Async.class); if (async == null) { async = AnnotatedElementUtils.findMergedAnnotation(method.getDeclaringClass(), Async.class); } return (async != null ? async.value() : null); }
@Async void saveLog(ProceedingJoinPoint point, SysLog log) throws JsonProcessingException; }
/** * Return the qualifier or bean name of the executor to be used when executing the * given method, specified via {@link Async#value} at the method or declaring * class level. If {@code @Async} is specified at both the method and class level, the * method's {@code #value} takes precedence (even if empty string, indicating that * the default executor should be used preferentially). * @param method the method to inspect for executor qualifier metadata * @return the qualifier if specified, otherwise empty string indicating that the * {@linkplain #setExecutor(Executor) default executor} should be used * @see #determineAsyncExecutor(Method) */ @Override @Nullable protected String getExecutorQualifier(Method method) { // Maintainer's note: changes made here should also be made in // AnnotationAsyncExecutionAspect#getExecutorQualifier Async async = AnnotatedElementUtils.findMergedAnnotation(method, Async.class); if (async == null) { async = AnnotatedElementUtils.findMergedAnnotation(method.getDeclaringClass(), Async.class); } return (async != null ? async.value() : null); }
@Async public interface AsyncInterface { void doSomething(int i); Future<String> returnSomething(int i); }
@Async public static class AsyncClassListener implements ApplicationListener<ApplicationEvent> { public AsyncClassListener() { listenerConstructed++; } @Override public void onApplicationEvent(ApplicationEvent event) { listenerCalled++; assertTrue(!Thread.currentThread().getName().equals(originalThreadName)); } }
@Async @Override public void doIt() { } }
@Async public static class AsyncClassBeanWithInterface implements RegularInterface { public void doSomething(int i) { assertTrue(!Thread.currentThread().getName().equals(originalThreadName)); } public Future<String> returnSomething(int i) { assertTrue(!Thread.currentThread().getName().equals(originalThreadName)); return new AsyncResult<>(Integer.toString(i)); } }
@Async @Override public void doIt() { } }
@Async void asyncMethod() { } }
@Async public void handleAsync(); }
@Async Future<String> returnSomething(int i); }
@Async public void incrementAsync() { counter++; }
@Async Future<String> asyncFoo(int id);
@Async void work();
@Async void test();
@Async void doSomething(int i);
@Async static class ClassWithAsyncAnnotation { int counter; public void increment() { counter++; } // Manually check that there is a warning from the 'declare warning' statement in // AnnotationAsyncExecutionAspect /* public int return5() { return 5; } */ public Future<Integer> incrementReturningAFuture() { counter++; return new AsyncResult<Integer>(5); } }
@Async("e0") public static class AsyncMethodWithQualifierBean { public void doNothing(int i) { assertTrue(Thread.currentThread().getName().equals(originalThreadName)); } @Async("e1") public void doSomething(int i) { assertTrue(!Thread.currentThread().getName().equals(originalThreadName)); assertTrue(Thread.currentThread().getName().startsWith("e1-")); } @MyAsync public Future<String> returnSomething(int i) { assertTrue(!Thread.currentThread().getName().equals(originalThreadName)); assertTrue(Thread.currentThread().getName().startsWith("e2-")); return new AsyncResult<>(Integer.toString(i)); } public Future<String> returnSomething2(int i) { assertTrue(!Thread.currentThread().getName().equals(originalThreadName)); assertTrue(Thread.currentThread().getName().startsWith("e0-")); return new AsyncResult<>(Integer.toString(i)); } }