Skip to content

Commit abbfc01

Browse files
author
mgooty
committed
Code samples for spring @async annotation.
1 parent b5cc62b commit abbfc01

File tree

6 files changed

+197
-0
lines changed

6 files changed

+197
-0
lines changed
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
package org.baeldung.async;
2+
3+
import java.util.concurrent.Future;
4+
5+
import org.springframework.scheduling.annotation.Async;
6+
import org.springframework.scheduling.annotation.AsyncResult;
7+
import org.springframework.stereotype.Component;
8+
9+
@Component
10+
public class AsyncAnnotationExample {
11+
12+
@Async
13+
public void asyncMethodWithVoidReturnType() {
14+
System.out.println("Execute method asynchronously. "
15+
+ Thread.currentThread().getName());
16+
}
17+
18+
@Async
19+
public Future<String> asyncMethodWithReturnType() {
20+
System.out.println("Execute method asynchronously "
21+
+ Thread.currentThread().getName());
22+
try {
23+
Thread.sleep(5000);
24+
return new AsyncResult<String>("hello world !!!!");
25+
} catch (final InterruptedException e) {
26+
27+
}
28+
29+
return null;
30+
}
31+
32+
@Async("threadPoolTaskExecutor")
33+
public void asyncMethodWithConfiguredExecutor() {
34+
System.out
35+
.println("Execute method asynchronously with configured executor"
36+
+ Thread.currentThread().getName());
37+
}
38+
39+
@Async
40+
public void asyncMethodWithExceptions() throws Exception {
41+
throw new Exception("Throw message from asynchronous method. ");
42+
}
43+
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
package org.baeldung.async;
2+
3+
import java.lang.reflect.Method;
4+
5+
import org.springframework.aop.interceptor.AsyncUncaughtExceptionHandler;
6+
7+
public class CustomAsyncExceptionHandler implements
8+
AsyncUncaughtExceptionHandler {
9+
10+
@Override
11+
public void handleUncaughtException(final Throwable throwable,
12+
final Method method, final Object... obj) {
13+
System.out.println("Exception message - " + throwable.getMessage());
14+
System.out.println("Method name - " + method.getName());
15+
for (final Object param : obj) {
16+
System.out.println("Param - " + param);
17+
}
18+
19+
}
20+
21+
}
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
package org.baeldung.async.config;
2+
3+
import java.util.concurrent.Executor;
4+
5+
import org.baeldung.async.CustomAsyncExceptionHandler;
6+
import org.springframework.aop.interceptor.AsyncUncaughtExceptionHandler;
7+
import org.springframework.context.annotation.Bean;
8+
import org.springframework.context.annotation.ComponentScan;
9+
import org.springframework.context.annotation.Configuration;
10+
import org.springframework.core.task.SimpleAsyncTaskExecutor;
11+
import org.springframework.scheduling.annotation.AsyncConfigurer;
12+
import org.springframework.scheduling.annotation.EnableAsync;
13+
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;
14+
15+
@Configuration
16+
@EnableAsync()
17+
@ComponentScan("org.baeldung.async")
18+
public class SpringAsyncConfig implements AsyncConfigurer {
19+
20+
@Bean(name = "threadPoolTaskExecutor")
21+
public Executor threadPoolTaskExecutor() {
22+
return new ThreadPoolTaskExecutor();
23+
}
24+
25+
@Override
26+
public Executor getAsyncExecutor() {
27+
return new SimpleAsyncTaskExecutor();
28+
}
29+
30+
@Override
31+
public AsyncUncaughtExceptionHandler getAsyncUncaughtExceptionHandler() {
32+
return new CustomAsyncExceptionHandler();
33+
}
34+
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<beans xmlns="http://www.springframework.org/schema/beans"
3+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context"
4+
xmlns:task="http://www.springframework.org/schema/task"
5+
xsi:schemaLocation="http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task-4.1.xsd
6+
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.1.xsd
7+
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.1.xsd">
8+
9+
<task:annotation-driven executor="myExecutor" />
10+
<task:executor id="myExecutor" pool-size="5" />
11+
12+
<bean id="asyncAnnotationExample" class="org.baeldung.async.AsyncAnnotationExample" />
13+
</beans>
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
package org.baeldung.async;
2+
3+
import java.util.concurrent.ExecutionException;
4+
import java.util.concurrent.Future;
5+
6+
import org.baeldung.async.config.SpringAsyncConfig;
7+
import org.junit.Test;
8+
import org.junit.runner.RunWith;
9+
import org.springframework.beans.factory.annotation.Autowired;
10+
import org.springframework.test.context.ContextConfiguration;
11+
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
12+
import org.springframework.test.context.support.AnnotationConfigContextLoader;
13+
14+
@RunWith(SpringJUnit4ClassRunner.class)
15+
@ContextConfiguration(classes = { SpringAsyncConfig.class }, loader = AnnotationConfigContextLoader.class)
16+
public class AsyncAnnotationExampleTest {
17+
18+
@Autowired
19+
AsyncAnnotationExample asyncAnnotationExample;
20+
21+
@Test
22+
public void testAsyncAnnotationForMethodsWithVoidReturnType() {
23+
System.out.println("Start - invoking an asynchronous method. "
24+
+ Thread.currentThread().getName());
25+
asyncAnnotationExample.asyncMethodWithVoidReturnType();
26+
System.out.println("End - invoking an asynchronous method. ");
27+
}
28+
29+
@Test
30+
public void testAsyncAnnotationForMethodsWithReturnType()
31+
throws InterruptedException, ExecutionException {
32+
System.out.println("Start - invoking an asynchronous method. "
33+
+ Thread.currentThread().getName());
34+
final Future<String> future = asyncAnnotationExample
35+
.asyncMethodWithReturnType();
36+
37+
while (true) {
38+
if (future.isDone()) {
39+
System.out.println("Result from asynchronous process - "
40+
+ future.get());
41+
break;
42+
}
43+
System.out.println("Continue doing something else. ");
44+
Thread.sleep(1000);
45+
}
46+
}
47+
48+
@Test
49+
public void testAsyncAnnotationForMethodsWithConfiguredExecutor() {
50+
System.out.println("Start - invoking an asynchronous method. ");
51+
asyncAnnotationExample.asyncMethodWithConfiguredExecutor();
52+
System.out.println("End - invoking an asynchronous method. ");
53+
}
54+
55+
@Test
56+
public void testAsyncAnnotationForMethodsWithException() throws Exception {
57+
System.out.println("Start - invoking an asynchronous method. ");
58+
asyncAnnotationExample.asyncMethodWithExceptions();
59+
System.out.println("End - invoking an asynchronous method. ");
60+
}
61+
}
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
package org.baeldung.async;
2+
3+
import org.junit.Test;
4+
import org.junit.runner.RunWith;
5+
import org.springframework.beans.factory.annotation.Autowired;
6+
import org.springframework.test.context.ContextConfiguration;
7+
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
8+
9+
@RunWith(SpringJUnit4ClassRunner.class)
10+
@ContextConfiguration("classpath:springAsync-config.xml")
11+
public class AsyncWithXMLTest {
12+
13+
@Autowired
14+
AsyncAnnotationExample asyncAnnotationExample;
15+
16+
@Test
17+
public void testAsyncAnnotationForMethodsWithVoidReturnType()
18+
throws InterruptedException {
19+
System.out.println("Start - invoking an asynchronous method. "
20+
+ Thread.currentThread().getName());
21+
asyncAnnotationExample.asyncMethodWithVoidReturnType();
22+
Thread.sleep(2000);
23+
System.out.println("End - invoking an asynchronous method. ");
24+
}
25+
}

0 commit comments

Comments
 (0)