-
Notifications
You must be signed in to change notification settings - Fork 53.6k
Expand file tree
/
Copy pathAsyncComponent.java
More file actions
40 lines (30 loc) · 1.15 KB
/
AsyncComponent.java
File metadata and controls
40 lines (30 loc) · 1.15 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
package org.baeldung.async;
import org.springframework.scheduling.annotation.Async;
import org.springframework.scheduling.annotation.AsyncResult;
import org.springframework.stereotype.Component;
import java.util.concurrent.Future;
@Component
public class AsyncComponent {
@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<>("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. ");
}
}