diff --git a/spring-all/src/main/java/org/baeldung/async/AsyncAnnotationExample.java b/spring-all/src/main/java/org/baeldung/async/AsyncAnnotationExample.java deleted file mode 100644 index 99c69b5273..0000000000 --- a/spring-all/src/main/java/org/baeldung/async/AsyncAnnotationExample.java +++ /dev/null @@ -1,43 +0,0 @@ -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 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. "); - } -} diff --git a/spring-all/src/main/java/org/baeldung/async/AsyncComponent.java b/spring-all/src/main/java/org/baeldung/async/AsyncComponent.java new file mode 100644 index 0000000000..2946ab0aa1 --- /dev/null +++ b/spring-all/src/main/java/org/baeldung/async/AsyncComponent.java @@ -0,0 +1,40 @@ +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 AsyncComponent { + + @Async + public void asyncMethodWithVoidReturnType() { + System.out.println("Execute method asynchronously. " + Thread.currentThread().getName()); + } + + @Async + public Future 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. "); + } + +} diff --git a/spring-all/src/main/java/org/baeldung/async/CustomAsyncExceptionHandler.java b/spring-all/src/main/java/org/baeldung/async/CustomAsyncExceptionHandler.java index 94b1f7a49d..a7a177f8fe 100644 --- a/spring-all/src/main/java/org/baeldung/async/CustomAsyncExceptionHandler.java +++ b/spring-all/src/main/java/org/baeldung/async/CustomAsyncExceptionHandler.java @@ -4,18 +4,15 @@ import java.lang.reflect.Method; import org.springframework.aop.interceptor.AsyncUncaughtExceptionHandler; -public class CustomAsyncExceptionHandler implements -AsyncUncaughtExceptionHandler { +public class CustomAsyncExceptionHandler implements AsyncUncaughtExceptionHandler { - @Override - public void handleUncaughtException(final Throwable throwable, - final Method method, final Object... obj) { - System.out.println("Exception message - " + throwable.getMessage()); - System.out.println("Method name - " + method.getName()); - for (final Object param : obj) { - System.out.println("Param - " + param); - } - - } + @Override + public void handleUncaughtException(final Throwable throwable, final Method method, final Object... obj) { + System.out.println("Exception message - " + throwable.getMessage()); + System.out.println("Method name - " + method.getName()); + for (final Object param : obj) { + System.out.println("Param - " + param); + } + } } diff --git a/spring-all/src/main/java/org/baeldung/async/config/SpringAsyncConfig.java b/spring-all/src/main/java/org/baeldung/async/config/SpringAsyncConfig.java index 6e3f96c18a..0676258207 100644 --- a/spring-all/src/main/java/org/baeldung/async/config/SpringAsyncConfig.java +++ b/spring-all/src/main/java/org/baeldung/async/config/SpringAsyncConfig.java @@ -17,18 +17,18 @@ import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor; @ComponentScan("org.baeldung.async") public class SpringAsyncConfig implements AsyncConfigurer { - @Bean(name = "threadPoolTaskExecutor") - public Executor threadPoolTaskExecutor() { - return new ThreadPoolTaskExecutor(); - } + @Bean(name = "threadPoolTaskExecutor") + public Executor threadPoolTaskExecutor() { + return new ThreadPoolTaskExecutor(); + } - @Override - public Executor getAsyncExecutor() { - return new SimpleAsyncTaskExecutor(); - } + @Override + public Executor getAsyncExecutor() { + return new SimpleAsyncTaskExecutor(); + } - @Override - public AsyncUncaughtExceptionHandler getAsyncUncaughtExceptionHandler() { - return new CustomAsyncExceptionHandler(); - } + @Override + public AsyncUncaughtExceptionHandler getAsyncUncaughtExceptionHandler() { + return new CustomAsyncExceptionHandler(); + } } \ No newline at end of file diff --git a/spring-all/src/main/resources/springAsync-config.xml b/spring-all/src/main/resources/springAsync-config.xml index 23905943d5..692f381990 100644 --- a/spring-all/src/main/resources/springAsync-config.xml +++ b/spring-all/src/main/resources/springAsync-config.xml @@ -5,9 +5,10 @@ xsi:schemaLocation="http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task-4.1.xsd http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.1.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.1.xsd"> - - - - + + + + + \ No newline at end of file diff --git a/spring-all/src/test/java/org/baeldung/async/AsyncAnnotationExampleTest.java b/spring-all/src/test/java/org/baeldung/async/AsyncAnnotationExampleTest.java index a44d4e9cd8..2f41766cb6 100644 --- a/spring-all/src/test/java/org/baeldung/async/AsyncAnnotationExampleTest.java +++ b/spring-all/src/test/java/org/baeldung/async/AsyncAnnotationExampleTest.java @@ -15,47 +15,45 @@ import org.springframework.test.context.support.AnnotationConfigContextLoader; @ContextConfiguration(classes = { SpringAsyncConfig.class }, loader = AnnotationConfigContextLoader.class) public class AsyncAnnotationExampleTest { - @Autowired - AsyncAnnotationExample asyncAnnotationExample; + @Autowired + private AsyncComponent asyncAnnotationExample; - @Test - public void testAsyncAnnotationForMethodsWithVoidReturnType() { - System.out.println("Start - invoking an asynchronous method. " - + Thread.currentThread().getName()); - asyncAnnotationExample.asyncMethodWithVoidReturnType(); - System.out.println("End - invoking an asynchronous method. "); - } + // tests - @Test - public void testAsyncAnnotationForMethodsWithReturnType() - throws InterruptedException, ExecutionException { - System.out.println("Start - invoking an asynchronous method. " - + Thread.currentThread().getName()); - final Future future = asyncAnnotationExample - .asyncMethodWithReturnType(); + @Test + public void testAsyncAnnotationForMethodsWithVoidReturnType() { + System.out.println("Start - invoking an asynchronous method. " + Thread.currentThread().getName()); + asyncAnnotationExample.asyncMethodWithVoidReturnType(); + System.out.println("End - invoking an asynchronous method. "); + } - while (true) { - if (future.isDone()) { - System.out.println("Result from asynchronous process - " - + future.get()); - break; - } - System.out.println("Continue doing something else. "); - Thread.sleep(1000); - } - } + @Test + public void testAsyncAnnotationForMethodsWithReturnType() throws InterruptedException, ExecutionException { + System.out.println("Start - invoking an asynchronous method. " + Thread.currentThread().getName()); + final Future future = asyncAnnotationExample.asyncMethodWithReturnType(); - @Test - public void testAsyncAnnotationForMethodsWithConfiguredExecutor() { - System.out.println("Start - invoking an asynchronous method. "); - asyncAnnotationExample.asyncMethodWithConfiguredExecutor(); - System.out.println("End - invoking an asynchronous method. "); - } + while (true) { + if (future.isDone()) { + System.out.println("Result from asynchronous process - " + future.get()); + break; + } + System.out.println("Continue doing something else. "); + Thread.sleep(1000); + } + } + + @Test + public void testAsyncAnnotationForMethodsWithConfiguredExecutor() { + System.out.println("Start - invoking an asynchronous method. "); + asyncAnnotationExample.asyncMethodWithConfiguredExecutor(); + System.out.println("End - invoking an asynchronous method. "); + } + + @Test + public void testAsyncAnnotationForMethodsWithException() throws Exception { + System.out.println("Start - invoking an asynchronous method. "); + asyncAnnotationExample.asyncMethodWithExceptions(); + System.out.println("End - invoking an asynchronous method. "); + } - @Test - public void testAsyncAnnotationForMethodsWithException() throws Exception { - System.out.println("Start - invoking an asynchronous method. "); - asyncAnnotationExample.asyncMethodWithExceptions(); - System.out.println("End - invoking an asynchronous method. "); - } } diff --git a/spring-all/src/test/java/org/baeldung/async/AsyncWithXMLTest.java b/spring-all/src/test/java/org/baeldung/async/AsyncWithXMLTest.java index 7436928c0c..b91666261c 100644 --- a/spring-all/src/test/java/org/baeldung/async/AsyncWithXMLTest.java +++ b/spring-all/src/test/java/org/baeldung/async/AsyncWithXMLTest.java @@ -10,16 +10,17 @@ import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; @ContextConfiguration("classpath:springAsync-config.xml") public class AsyncWithXMLTest { - @Autowired - AsyncAnnotationExample asyncAnnotationExample; + @Autowired + private AsyncComponent asyncAnnotationExample; + + // tests + + @Test + public void testAsyncAnnotationForMethodsWithVoidReturnType() throws InterruptedException { + System.out.println("Start - invoking an asynchronous method. " + Thread.currentThread().getName()); + asyncAnnotationExample.asyncMethodWithVoidReturnType(); + Thread.sleep(2000); + System.out.println("End - invoking an asynchronous method. "); + } - @Test - public void testAsyncAnnotationForMethodsWithVoidReturnType() - throws InterruptedException { - System.out.println("Start - invoking an asynchronous method. " - + Thread.currentThread().getName()); - asyncAnnotationExample.asyncMethodWithVoidReturnType(); - Thread.sleep(2000); - System.out.println("End - invoking an asynchronous method. "); - } }