Skip to content

Commit 393616f

Browse files
author
eugenp
committed
spring async cleanup before publish
1 parent ef46c6f commit 393616f

File tree

7 files changed

+119
-125
lines changed

7 files changed

+119
-125
lines changed

spring-all/src/main/java/org/baeldung/async/AsyncAnnotationExample.java

Lines changed: 0 additions & 43 deletions
This file was deleted.
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
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 AsyncComponent {
11+
12+
@Async
13+
public void asyncMethodWithVoidReturnType() {
14+
System.out.println("Execute method asynchronously. " + Thread.currentThread().getName());
15+
}
16+
17+
@Async
18+
public Future<String> asyncMethodWithReturnType() {
19+
System.out.println("Execute method asynchronously " + Thread.currentThread().getName());
20+
try {
21+
Thread.sleep(5000);
22+
return new AsyncResult<String>("hello world !!!!");
23+
} catch (final InterruptedException e) {
24+
25+
}
26+
27+
return null;
28+
}
29+
30+
@Async("threadPoolTaskExecutor")
31+
public void asyncMethodWithConfiguredExecutor() {
32+
System.out.println("Execute method asynchronously with configured executor" + Thread.currentThread().getName());
33+
}
34+
35+
@Async
36+
public void asyncMethodWithExceptions() throws Exception {
37+
throw new Exception("Throw message from asynchronous method. ");
38+
}
39+
40+
}

spring-all/src/main/java/org/baeldung/async/CustomAsyncExceptionHandler.java

Lines changed: 9 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -4,18 +4,15 @@
44

55
import org.springframework.aop.interceptor.AsyncUncaughtExceptionHandler;
66

7-
public class CustomAsyncExceptionHandler implements
8-
AsyncUncaughtExceptionHandler {
7+
public class CustomAsyncExceptionHandler implements AsyncUncaughtExceptionHandler {
98

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-
}
9+
@Override
10+
public void handleUncaughtException(final Throwable throwable, final Method method, final Object... obj) {
11+
System.out.println("Exception message - " + throwable.getMessage());
12+
System.out.println("Method name - " + method.getName());
13+
for (final Object param : obj) {
14+
System.out.println("Param - " + param);
15+
}
16+
}
2017

2118
}

spring-all/src/main/java/org/baeldung/async/config/SpringAsyncConfig.java

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -17,18 +17,18 @@
1717
@ComponentScan("org.baeldung.async")
1818
public class SpringAsyncConfig implements AsyncConfigurer {
1919

20-
@Bean(name = "threadPoolTaskExecutor")
21-
public Executor threadPoolTaskExecutor() {
22-
return new ThreadPoolTaskExecutor();
23-
}
20+
@Bean(name = "threadPoolTaskExecutor")
21+
public Executor threadPoolTaskExecutor() {
22+
return new ThreadPoolTaskExecutor();
23+
}
2424

25-
@Override
26-
public Executor getAsyncExecutor() {
27-
return new SimpleAsyncTaskExecutor();
28-
}
25+
@Override
26+
public Executor getAsyncExecutor() {
27+
return new SimpleAsyncTaskExecutor();
28+
}
2929

30-
@Override
31-
public AsyncUncaughtExceptionHandler getAsyncUncaughtExceptionHandler() {
32-
return new CustomAsyncExceptionHandler();
33-
}
30+
@Override
31+
public AsyncUncaughtExceptionHandler getAsyncUncaughtExceptionHandler() {
32+
return new CustomAsyncExceptionHandler();
33+
}
3434
}

spring-all/src/main/resources/springAsync-config.xml

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,10 @@
55
xsi:schemaLocation="http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task-4.1.xsd
66
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.1.xsd
77
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-
8+
9+
<task:annotation-driven executor="myExecutor" />
10+
<task:executor id="myExecutor" pool-size="5" />
11+
1212
<bean id="asyncAnnotationExample" class="org.baeldung.async.AsyncAnnotationExample" />
13+
1314
</beans>

spring-all/src/test/java/org/baeldung/async/AsyncAnnotationExampleTest.java

Lines changed: 41 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -15,47 +15,45 @@
1515
@ContextConfiguration(classes = { SpringAsyncConfig.class }, loader = AnnotationConfigContextLoader.class)
1616
public class AsyncAnnotationExampleTest {
1717

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-
}
18+
@Autowired
19+
private AsyncComponent asyncAnnotationExample;
20+
21+
// tests
22+
23+
@Test
24+
public void testAsyncAnnotationForMethodsWithVoidReturnType() {
25+
System.out.println("Start - invoking an asynchronous method. " + Thread.currentThread().getName());
26+
asyncAnnotationExample.asyncMethodWithVoidReturnType();
27+
System.out.println("End - invoking an asynchronous method. ");
28+
}
29+
30+
@Test
31+
public void testAsyncAnnotationForMethodsWithReturnType() throws InterruptedException, ExecutionException {
32+
System.out.println("Start - invoking an asynchronous method. " + Thread.currentThread().getName());
33+
final Future<String> future = asyncAnnotationExample.asyncMethodWithReturnType();
34+
35+
while (true) {
36+
if (future.isDone()) {
37+
System.out.println("Result from asynchronous process - " + future.get());
38+
break;
39+
}
40+
System.out.println("Continue doing something else. ");
41+
Thread.sleep(1000);
42+
}
43+
}
44+
45+
@Test
46+
public void testAsyncAnnotationForMethodsWithConfiguredExecutor() {
47+
System.out.println("Start - invoking an asynchronous method. ");
48+
asyncAnnotationExample.asyncMethodWithConfiguredExecutor();
49+
System.out.println("End - invoking an asynchronous method. ");
50+
}
51+
52+
@Test
53+
public void testAsyncAnnotationForMethodsWithException() throws Exception {
54+
System.out.println("Start - invoking an asynchronous method. ");
55+
asyncAnnotationExample.asyncMethodWithExceptions();
56+
System.out.println("End - invoking an asynchronous method. ");
57+
}
58+
6159
}

spring-all/src/test/java/org/baeldung/async/AsyncWithXMLTest.java

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -10,16 +10,17 @@
1010
@ContextConfiguration("classpath:springAsync-config.xml")
1111
public class AsyncWithXMLTest {
1212

13-
@Autowired
14-
AsyncAnnotationExample asyncAnnotationExample;
13+
@Autowired
14+
private AsyncComponent asyncAnnotationExample;
15+
16+
// tests
17+
18+
@Test
19+
public void testAsyncAnnotationForMethodsWithVoidReturnType() throws InterruptedException {
20+
System.out.println("Start - invoking an asynchronous method. " + Thread.currentThread().getName());
21+
asyncAnnotationExample.asyncMethodWithVoidReturnType();
22+
Thread.sleep(2000);
23+
System.out.println("End - invoking an asynchronous method. ");
24+
}
1525

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-
}
2526
}

0 commit comments

Comments
 (0)