• Stars
    star
    1,367
  • Rank 34,394 (Top 0.7 %)
  • Language
    Java
  • License
    Other
  • Created almost 9 years ago
  • Updated over 2 years ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

EA Async implements async-await methods in the JVM.

EA Async

Release Maven Central Javadocs Build Status

EA Async implements Async-Await methods in the JVM. It allows programmers to write asynchronous code in a sequential fashion.

It is heavily inspired by Async-Await on the .NET CLR, see Asynchronous Programming with Async and Await for more information.

Who should use it?

EA Async should be used to write non-blocking asynchronous code that makes heavy use of CompletableFutures or CompletionStage. It improves scalability by freeing worker threads while your code awaits other processes; And improves productivity by making asynchronous code simpler and more readable.

Developer & License

This project was developed by Electronic Arts and is licensed under the BSD 3-Clause License.

Examples

With EA Async

import static com.ea.async.Async.await;
import static java.util.concurrent.CompletableFuture.completedFuture;

public class Store
{
    public CompletableFuture<Boolean> buyItem(String itemTypeId, int cost)
    {
        if(!await(bank.decrement(cost))) {
            return completedFuture(false);
        }
        await(inventory.giveItem(itemTypeId));
        return completedFuture(true);
    }
}

In this example Bank.decrement returns CompletableFuture<Boolean> and Inventory.giveItem returns CompletableFuture<String>

EA Async rewrites the calls to Async.await making your methods non-blocking.

The methods look blocking but are actually transformed into asynchronous methods that use CompletableFutures to continue the execution as intermediary results arrive.

Without EA Async

This is how the first example looks without EA Async. It is a bit less readable.

import static java.util.concurrent.CompletableFuture.completedFuture;

public class Store
{
    public CompletableFuture<Boolean> buyItem(String itemTypeId, int cost)
    {
        return bank.decrement(cost)
            .thenCompose(result -> {
                if(!result) {
                    return completedFuture(false);
                }
                return inventory.giveItem(itemTypeId).thenApply(res -> true);
            });
    }
}

This is a small example... A method with a few more CompletableFutures can look very convoluted.

EA Async abstracts away the complexity of the CompletableFutures.

With EA Async (2)

So you like CompletableFutures? Try converting this method to use only CompletableFutures without ever blocking (so no joining):

import static com.ea.async.Async.await;
import static java.util.concurrent.CompletableFuture.completedFuture;

public class Store
{
    public CompletableFuture<Boolean> buyItem(String itemTypeId, int cost)
    {
        if(!await(bank.decrement(cost))) {
            return completedFuture(false);
        }
        try {
            await(inventory.giveItem(itemTypeId));
            return completedFuture(true);
        } catch (Exception ex) {
            await(bank.refund(cost));
            throw new AppException(ex);
        }
    }
}

Got it? Send it to us. It probably looks ugly...

Getting started

EA Async currently supports JDK 8-10.

It works with Java and Scala and should work with most JVM languages. The only requirement to use EA Async is that must be used only inside methods that return CompletableFuture, CompletionStage, or subclasses of CompletableFuture.

Using with maven

<dependency>
    <groupId>com.ea.async</groupId>
    <artifactId>ea-async</artifactId>
    <version>1.2.3</version>
</dependency>

Gradle

'com.ea.async:ea-async:1.2.3'

Instrumenting your code

Option 1 - JVM parameter

Start your application with an extra JVM parameter: -javaagent:ea-async-1.2.3.jar

 java -javaagent:ea-async-1.2.3.jar -cp your_claspath YourMainClass args...

It's recommended to add this as a default option to launchers in IntelliJ projects that use ea-async.

Option 2 - Runtime

On your main class or as early as possible, call at least once:

Async.init();

Provided that your JVM has the capability enabled, this will start a runtime instrumentation agent. If you forget to invoke this function, the first call to await will initialize the system (and print a warning).

This is a solution for testing and development, it has the least amount of configuration. It might interfere with JVM debugging. This alternative is present as a fallback.

Option 3 - Run instrumentation tool

The ea-async-1.2.3.jar is a runnable jar that can pre-instrument your files.

Usage:

java -cp YOUR_PROJECT_CLASSPATH -jar ea-async-1.2.3.jar classDirectory

Example:

java -cp guava.jar;commons-lang.jar  -jar ea-async-1.2.3.jar target/classes

After that all the files in target/classes will have been instrumented. There will be no references to Async.await and Async.init left in those classes.

Option 4 - Build time instrumentation, with Maven - Preferred

Use the ea-async-maven-plugin. It will instrument your classes in compile time and remove all references to Async.await and Async.init().

With build time instrumentation your project users won't need to have EA Async in their classpath unless they also choose to use it. This means that EA Async does not need to be a transitive dependency.

This is the best option for libraries and maven projects.

<build>
    <plugins>
        <plugin>
            <groupId>com.ea.async</groupId>
            <artifactId>ea-async-maven-plugin</artifactId>
            <version>1.2.3</version>
            <executions>
                <execution>
                    <goals>
                        <goal>instrument</goal>
                        <goal>instrument-test</goal>
                    </goals>
                </execution>
            </executions>
        </plugin>
    </plugins>
</build>

More Repositories

1

CnC_Remastered_Collection

C++
18,221
star
2

EASTL

EASTL stands for Electronic Arts Standard Template Library. It is an extensive and robust implementation that has an emphasis on high performance.
C++
7,676
star
3

dem-bones

An automated algorithm to extract the linear blend skinning (LBS) from a set of example poses
C++
837
star
4

EAStdC

EAStdC is a package which implements basic library facilities that are similar to those in the standard C library.
C++
478
star
5

EAThread

EAThread implements a unified cross-platform interface for multithreaded programming.
C++
288
star
6

character-motion-vaes

Character Controllers using Motion VAEs
Python
239
star
7

EACopy

Robocopy alternative with a bunch of different features
C++
232
star
8

EABase

EABase is a small set of header files that define platform-independent data types and platform feature macros.
C++
157
star
9

RenderWare3Docs

White Papers and User Guide from the RenderWare game engine release for PC.
145
star
10

gatling-aws-maven-plugin

The Gatling AWS Maven plugin takes the pain out of scaling up your Gatling tests. It runs your load test on a configurable number of EC2 instances, aggregates a single load test report, and uploads the results to S3. All EC2 instances are terminated at the end of the test to ensure you are only paying for what you need.
Java
99
star
11

ea-agent-loader

EA Agent Loader is a collection of utilities for java agent developers.
Java
92
star
12

Tunable-Colorblindness-Solution

A tunable colorblindness solution that improves color-shifting and luminosity to address common colorblind issues relating to color differentiation and visibility in colorblind accessibility settings.
HLSL
89
star
13

IRIS

EA’s photosensitivity analysis tool
C++
69
star
14

cpp-ml-intro

C++ ML Intro
C
65
star
15

ava-capture

Ava Capture is a distributed system to control and record several cameras from a central UI. This system would typically be used for a photogrammetry or 4D capture rig based on Ximea cameras.
Python
64
star
16

rig-inversion

Example code for the technique described in the paper "Rig Inversion by Training a Differentiable Rig Function" published at SIGGRAPH Asia 2022.
Python
60
star
17

SimpleTeamSportsSimulator

Python
54
star
18

CNC_TS_and_RA2_Mission_Editor

FinalSun & FinalAlert2 Level Editors
C++
52
star
19

awsudo

Automated AWS API access using a SAML compliant identity provider
Ruby
43
star
20

fastnoise

Filter-adapted Spatiotemporal Sampling for Real-Time Rendering
C++
42
star
21

EAMain

EAMain provides a multi-platform entry point used for platforms that don't support console output, return codes and command-line arguments.
C++
41
star
22

EAAssert

EAAssert is a simple and lightweight assert technology.
C++
39
star
23

EATest

EATest is a C++ unit testing framework. It's goal is to have a scalable architecture, have minimal dependencies, and simple usage.
C++
37
star
24

fonttik

Fonttik is a text size & contrast checking solution created by EACC Tech to check accessibility criteria regarding text readability
C++
35
star
25

siggraph-asia-2019-gata

Supports SIGGRAPH Asia 2019 paper "GATA: Multi-Theme Generative Adversarial Terrain Amplification"
Python
29
star
26

MELE_ModdingSupport

C
28
star
27

interactive_training

Jupyter Notebook
18
star
28

minicoros

C++
17
star
29

orbit

Moved to https://github.com/orbit/orbit
14
star
30

NetTAP

TAP parser for .NET
C#
12
star
31

helmci

helmci is a program that takes a set of helm release values and automatically deploys them to a Kubernetes cluster
Rust
10
star
32

kara

Automated generation of Finagle HTTP/JSON services and Swagger UI from Thrift service definitions.
Scala
8
star
33

harmony

Java
5
star