diff --git a/spring-scheduling/src/main/java/com/baeldung/async/AsyncService.java b/spring-scheduling/src/main/java/com/baeldung/async/AsyncService.java new file mode 100644 index 0000000000..f55fd57f53 --- /dev/null +++ b/spring-scheduling/src/main/java/com/baeldung/async/AsyncService.java @@ -0,0 +1,23 @@ +package com.baeldung.async; + +import java.util.concurrent.CompletableFuture; + +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.stereotype.Service; + +@Service +public class AsyncService { + + @Autowired + private FirstAsyncService fisrtService; + @Autowired + private SecondAsyncService secondService; + + public CompletableFuture asyncMergeServicesResponse() throws InterruptedException { + CompletableFuture fisrtServiceResponse = fisrtService.asyncGetData(); + CompletableFuture secondServiceResponse = secondService.asyncGetData(); + + // Merge responses from FirstAsyncService and SecondAsyncService + return fisrtServiceResponse.thenCompose(fisrtServiceValue -> secondServiceResponse.thenApply(secondServiceValue -> fisrtServiceValue + secondServiceValue)); + } +} diff --git a/spring-scheduling/src/main/java/com/baeldung/async/FirstAsyncService.java b/spring-scheduling/src/main/java/com/baeldung/async/FirstAsyncService.java new file mode 100644 index 0000000000..fdcb236c88 --- /dev/null +++ b/spring-scheduling/src/main/java/com/baeldung/async/FirstAsyncService.java @@ -0,0 +1,20 @@ +package com.baeldung.async; + +import java.util.concurrent.CompletableFuture; + +import org.springframework.scheduling.annotation.Async; +import org.springframework.scheduling.annotation.AsyncResult; +import org.springframework.stereotype.Service; + +@Service +public class FirstAsyncService { + + @Async + public CompletableFuture asyncGetData() throws InterruptedException { + System.out.println("Execute method asynchronously " + Thread.currentThread() + .getName()); + Thread.sleep(4000); + return new AsyncResult<>(super.getClass().getSimpleName() + " response !!! ").completable(); + } + +} diff --git a/spring-scheduling/src/main/java/com/baeldung/async/SecondAsyncService.java b/spring-scheduling/src/main/java/com/baeldung/async/SecondAsyncService.java new file mode 100644 index 0000000000..f5657a6e9e --- /dev/null +++ b/spring-scheduling/src/main/java/com/baeldung/async/SecondAsyncService.java @@ -0,0 +1,20 @@ +package com.baeldung.async; + +import java.util.concurrent.CompletableFuture; + +import org.springframework.scheduling.annotation.Async; +import org.springframework.scheduling.annotation.AsyncResult; +import org.springframework.stereotype.Service; + +@Service +public class SecondAsyncService { + + @Async + public CompletableFuture asyncGetData() throws InterruptedException { + System.out.println("Execute method asynchronously " + Thread.currentThread() + .getName()); + Thread.sleep(4000); + return new AsyncResult<>(super.getClass().getSimpleName() + " response !!! ").completable(); + } + +} diff --git a/spring-scheduling/src/test/java/com/baeldung/async/AsyncServiceUnitTest.java b/spring-scheduling/src/test/java/com/baeldung/async/AsyncServiceUnitTest.java new file mode 100644 index 0000000000..b06544e77d --- /dev/null +++ b/spring-scheduling/src/test/java/com/baeldung/async/AsyncServiceUnitTest.java @@ -0,0 +1,40 @@ +package com.baeldung.async; + +import java.util.concurrent.CompletableFuture; +import java.util.concurrent.ExecutionException; + +import org.junit.Test; +import org.junit.runner.RunWith; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.test.context.ContextConfiguration; +import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; +import org.springframework.test.context.support.AnnotationConfigContextLoader; + +import com.baeldung.async.config.SpringAsyncConfig; + +@RunWith(SpringJUnit4ClassRunner.class) +@ContextConfiguration(classes = { SpringAsyncConfig.class }, loader = AnnotationConfigContextLoader.class) +public class AsyncServiceUnitTest { + + @Autowired + private AsyncService asyncServiceExample; + + // tests + + @Test + public void testAsyncAnnotationForMergedServicesResponse() throws InterruptedException, ExecutionException { + System.out.println("Invoking an asynchronous method. " + Thread.currentThread() + .getName()); + CompletableFuture completableFuture = asyncServiceExample.asyncMergeServicesResponse(); + + while (true) { + if (completableFuture.isDone()) { + System.out.println("Result from asynchronous process - " + completableFuture.get()); + break; + } + System.out.println("Continue doing something else. "); + Thread.sleep(1000); + } + } + +}