BAEL-7713 - add example merging async services response (#16280)

* BAEL-7713 - add example merging async services response

* BAEL-7713 update test class naming convention
This commit is contained in:
Bogdan Cardoş 2024-04-02 02:38:06 +03:00 committed by GitHub
parent 6ba485f0e4
commit 485cab4c99
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
4 changed files with 103 additions and 0 deletions

View File

@ -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<String> asyncMergeServicesResponse() throws InterruptedException {
CompletableFuture<String> fisrtServiceResponse = fisrtService.asyncGetData();
CompletableFuture<String> secondServiceResponse = secondService.asyncGetData();
// Merge responses from FirstAsyncService and SecondAsyncService
return fisrtServiceResponse.thenCompose(fisrtServiceValue -> secondServiceResponse.thenApply(secondServiceValue -> fisrtServiceValue + secondServiceValue));
}
}

View File

@ -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<String> asyncGetData() throws InterruptedException {
System.out.println("Execute method asynchronously " + Thread.currentThread()
.getName());
Thread.sleep(4000);
return new AsyncResult<>(super.getClass().getSimpleName() + " response !!! ").completable();
}
}

View File

@ -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<String> asyncGetData() throws InterruptedException {
System.out.println("Execute method asynchronously " + Thread.currentThread()
.getName());
Thread.sleep(4000);
return new AsyncResult<>(super.getClass().getSimpleName() + " response !!! ").completable();
}
}

View File

@ -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<String> 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);
}
}
}