Merge pull request #8456 from SmartyAnsh/BAEL-3596_Asynchronous_Method_Calls_in_Java

Bael 3596 asynchronous method calls in java
This commit is contained in:
Eric Martin 2020-01-10 20:13:04 -06:00 committed by GitHub
commit 0a369c4a16
3 changed files with 303 additions and 0 deletions

View File

@ -23,6 +23,37 @@
<version>${assertj.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.jcabi</groupId>
<artifactId>jcabi-aspects</artifactId>
<version>${jcabi-aspects.version}</version>
</dependency>
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjrt</artifactId>
<version>${aspectjrt.version}</version>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>com.google.guava</groupId>
<artifactId>guava</artifactId>
<version>${guava.version}</version>
</dependency>
<dependency>
<groupId>org.cactoos</groupId>
<artifactId>cactoos</artifactId>
<version>${cactoos.version}</version>
</dependency>
<dependency>
<groupId>com.ea.async</groupId>
<artifactId>ea-async</artifactId>
<version>${ea-async.version}</version>
</dependency>
</dependencies>
<build>
@ -36,6 +67,30 @@
<target>${maven.compiler.target}</target>
</configuration>
</plugin>
<plugin>
<groupId>com.jcabi</groupId>
<artifactId>jcabi-maven-plugin</artifactId>
<version>${jcabi-maven-plugin.version}</version>
<executions>
<execution>
<goals>
<goal>ajc</goal>
</goals>
</execution>
</executions>
<dependencies>
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjtools</artifactId>
<version>${aspectjtools.version}</version>
</dependency>
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjweaver</artifactId>
<version>${aspectjweaver.version}</version>
</dependency>
</dependencies>
</plugin>
</plugins>
<resources>
<resource>
@ -49,6 +104,14 @@
<assertj.version>3.14.0</assertj.version>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
<jcabi-aspects.version>0.22.6</jcabi-aspects.version>
<aspectjrt.version>1.9.5</aspectjrt.version>
<guava.version>28.2-jre</guava.version>
<cactoos.version>0.43</cactoos.version>
<ea-async.version>1.2.3</ea-async.version>
<jcabi-maven-plugin.version>0.14.1</jcabi-maven-plugin.version>
<aspectjtools.version>1.9.1</aspectjtools.version>
<aspectjweaver.version>1.9.1</aspectjweaver.version>
</properties>
</project>

View File

@ -0,0 +1,57 @@
package com.baeldung.async;
import static com.ea.async.Async.await;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ExecutionException;
import com.ea.async.Async;
public class EAAsyncExample {
static {
Async.init();
}
public static void main(String[] args) throws Exception {
usingCompletableFuture();
usingAsyncAwait();
}
public static void usingCompletableFuture() throws InterruptedException, ExecutionException, Exception {
CompletableFuture<Void> completableFuture = hello()
.thenComposeAsync(hello -> mergeWorld(hello))
.thenAcceptAsync(helloWorld -> print(helloWorld))
.exceptionally( throwable -> {
System.out.println(throwable.getCause());
return null;
});
completableFuture.get();
}
public static CompletableFuture<String> hello() {
CompletableFuture<String> completableFuture = CompletableFuture.supplyAsync(() -> "Hello");
return completableFuture;
}
public static CompletableFuture<String> mergeWorld(String s) {
CompletableFuture<String> completableFuture = CompletableFuture.supplyAsync(() -> {
return s + " World!";
});
return completableFuture;
}
public static void print(String str) {
CompletableFuture.runAsync(() -> System.out.println(str));
}
private static void usingAsyncAwait() {
try {
String hello = await(hello());
String helloWorld = await(mergeWorld(hello));
await(CompletableFuture.runAsync(() -> print(helloWorld)));
} catch (Exception e) {
e.printStackTrace();
}
}
}

View File

@ -0,0 +1,183 @@
package com.baeldung.async;
import static com.ea.async.Async.await;
import java.util.concurrent.Callable;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;
import com.google.common.util.concurrent.AsyncCallable;
import com.google.common.util.concurrent.Callables;
import com.google.common.util.concurrent.Futures;
import com.google.common.util.concurrent.ListenableFuture;
import com.google.common.util.concurrent.ListeningExecutorService;
import com.google.common.util.concurrent.MoreExecutors;
import com.jcabi.aspects.Async;
import com.jcabi.aspects.Loggable;
public class JavaAsync {
static {
com.ea.async.Async.init();
}
private static final ExecutorService threadpool = Executors.newCachedThreadPool();
public static void main (String[] args) throws InterruptedException, ExecutionException {
int number = 20;
//Thread Example
factorialUsingThread(number).start();
//FutureTask Example
Future<Long> futureTask = factorialUsingFutureTask(number);
System.out.println("Factorial of " + number + " is: " + futureTask.get());
// CompletableFuture Example
Future<Long> completableFuture = factorialUsingCompletableFuture(number);
System.out.println("Factorial of " + number + " is: " + completableFuture.get());
// EA Async example
System.out.println("Factorial of " + number + " is: " + factorialUsingEAAsync(number));
// cactoos async example
Future<Long> asyncFuture = factorialUsingCactoos(number);
System.out.println("Factorial of " + number + " is: " + asyncFuture.get());
// Guava example
ListenableFuture<Long> guavaFuture = factorialUsingGuavaServiceSubmit(number);
System.out.println("Factorial of " + number + " is: " + guavaFuture.get());
ListenableFuture<Long> guavaFutures = factorialUsingGuavaFutures(number);
System.out.println("Factorial of " + number + " is: " + guavaFutures.get());
// @async jcabi-aspect example
Future<Long> aspectFuture = factorialUsingJcabiAspect(number);
System.out.println("Factorial of " + number + " is: " + aspectFuture.get());
}
/**
* Finds factorial of a number
* @param number
* @return
*/
public static long factorial(int number) {
long result = 1;
for(int i=number;i>0;i--) {
result *= i;
}
return result;
}
/**
* Finds factorial of a number using Thread
* @param number
* @return
*/
@Loggable
public static Thread factorialUsingThread(int number) {
Thread newThread = new Thread(() -> {
System.out.println("Factorial of " + number + " is: " + factorial(number));
});
return newThread;
}
/**
* Finds factorial of a number using FutureTask
* @param number
* @return
*/
@Loggable
public static Future<Long> factorialUsingFutureTask(int number) {
Future<Long> futureTask = threadpool.submit(() -> factorial(number));
while (!futureTask.isDone()) {
System.out.println("FutureTask is not finished yet...");
}
return futureTask;
}
/**
* Finds factorial of a number using CompletableFuture
* @param number
* @return
*/
@Loggable
public static Future<Long> factorialUsingCompletableFuture(int number) {
CompletableFuture<Long> completableFuture = CompletableFuture.supplyAsync(() -> factorial(number));
return completableFuture;
}
/**
* Finds factorial of a number using EA Async
* @param number
* @return
*/
@Loggable
public static long factorialUsingEAAsync(int number) {
CompletableFuture<Long> completableFuture = CompletableFuture.supplyAsync(() -> factorial(number));
long result = await(completableFuture);
return result;
}
/**
* Finds factorial of a number using Async of Cactoos
* @param number
* @return
* @throws InterruptedException
* @throws ExecutionException
*/
@Loggable
public static Future<Long> factorialUsingCactoos(int number) throws InterruptedException, ExecutionException {
org.cactoos.func.Async<Integer, Long> asyncFunction = new org.cactoos.func.Async<Integer, Long>(input -> factorial(input));
Future<Long> asyncFuture = asyncFunction.apply(number);
return asyncFuture;
}
/**
* Finds factorial of a number using Guava's ListeningExecutorService.submit()
* @param number
* @return
*/
@Loggable
public static ListenableFuture<Long> factorialUsingGuavaServiceSubmit(int number) {
ListeningExecutorService service = MoreExecutors.listeningDecorator(threadpool);
ListenableFuture<Long> factorialFuture = (ListenableFuture<Long>) service.submit(()-> factorial(number));
return factorialFuture;
}
/**
* Finds factorial of a number using Guava's Futures.submitAsync()
* @param number
* @return
*/
@Loggable
public static ListenableFuture<Long> factorialUsingGuavaFutures(int number) {
ListeningExecutorService service = MoreExecutors.listeningDecorator(threadpool);
AsyncCallable<Long> asyncCallable = Callables.asAsyncCallable(new Callable<Long>() {
public Long call() {
return factorial(number);
}
}, service);
return Futures.submitAsync(asyncCallable, service);
}
/**
* Finds factorial of a number using @Async of jcabi-aspects
* @param number
* @return
*/
@Async
@Loggable
public static Future<Long> factorialUsingJcabiAspect(int number) {
Future<Long> factorialFuture = CompletableFuture.supplyAsync(() -> factorial(number));
return factorialFuture;
}
}