Files
spring-boot-rest/spring-all/src/main/java/org/baeldung/async/AsyncAnnotationExample.java
2014-12-07 21:15:52 +05:30

44 lines
1.1 KiB
Java

package org.baeldung.async;
import java.util.concurrent.Future;
import org.springframework.scheduling.annotation.Async;
import org.springframework.scheduling.annotation.AsyncResult;
import org.springframework.stereotype.Component;
@Component
public class AsyncAnnotationExample {
@Async
public void asyncMethodWithVoidReturnType() {
System.out.println("Execute method asynchronously. "
+ Thread.currentThread().getName());
}
@Async
public Future<String> asyncMethodWithReturnType() {
System.out.println("Execute method asynchronously "
+ Thread.currentThread().getName());
try {
Thread.sleep(5000);
return new AsyncResult<String>("hello world !!!!");
} catch (final InterruptedException e) {
}
return null;
}
@Async("threadPoolTaskExecutor")
public void asyncMethodWithConfiguredExecutor() {
System.out
.println("Execute method asynchronously with configured executor"
+ Thread.currentThread().getName());
}
@Async
public void asyncMethodWithExceptions() throws Exception {
throw new Exception("Throw message from asynchronous method. ");
}
}