zakgof / actr

Simple, fast and typesafe Java actor model implementation
Apache License 2.0
119 stars 20 forks source link

Futures compatibility #11

Closed andob closed 4 years ago

andob commented 4 years ago

First of all, thanks for creating this library. It's great because it's simple (I like simplicity) and TypeSafe (I took a look at akka, but didn't like it so much because its weak typeness). I'm going to use actr in an upcoming project.

I think it would be great to have future compatibility, for instance:

primeNumberDetectorActor.ask(
        /*action*/ primeNumberDetector -> primeNumberDetector.isNumberPrime(5),
        /*consumer*/ isPrime -> System.out.println("5 is prime? "+isPrime));

If I want to handle success / error states, I have to wrap the action in try - catch:

public static class ActorResult<SUCCESS_TYPE>
{
    public SUCCESS_TYPE successValue = null;
    public Throwable error = null;
}

primeNumberDetectorActor.ask(
    /*action*/ primeNumberDetector -> {
        ActorResult<Boolean> result = new ActorResult<>();

        try {
            result.successValue = primeNumberDetector.isNumberPrime(5);
        } catch (Throwable ex) {
            result.error = ex;
        }

        return result;
    },
    /*consumer*/ result -> {
        if (result.error!=null)
            result.error.printStackTrace();
        else System.out.println("5 is prime? "+result.successValue);
    });

It would be nice to have this wrapping done automatically by the library, and to be able to use futures to handle success / error cases.

In the following example, I'm proposing a method with the following signature:

public <ACTOR, SUCCESS_RESULT_TYPE> CompletableFuture<SUCCESS_RESULT_TYPE> askAnsweringFuture(Function<ACTOR, SUCCESS_RESULT_TYPE> action);

Usage:

primeNumberDetectorActor.askAnsweringFuture(primeNumberDetector -> primeNumberDetector.isNumberPrime(5))
                .thenApply(isPrime -> System.out.println("5 is prime? "+isPrime))
                .exceptionally(ex -> ex.printStackTrace());

Since I'm going to use Kotlin, I already made a similar extension method. But since Java doesn't have extension methods, I think this would be a nice feature.

zakgof commented 4 years ago

Thanks for your suggestion! Actor model common philosophy used to be 'let it crash', but I've always felt that ignoring errors is not sufficient enough. _Ask_ing with CompletableFuture looks very natural. Actually I'm thinking of revising the whole error handling in actr, but I'd like to avoid complexity and turning actr into another all-in-one reactive stream framework.

andob commented 4 years ago

I'd like to avoid complexity and turning actr into another all-in-one reactive stream framework.

Agree :D

I think it would be nice to let the vanilla ask behavior untouched. And to have separate integration modules, eg:

compile 'com.github.zakgof:actr:0.2.1'
//actor.askAnsweringFuture() -> Future<T>
compile 'com.github.zakgof:actr-futures:0.2.1'
//actor.askAnsweringRxObservable() -> Observable<T> - for RxJava
compile 'com.github.zakgof:actr-rx:0.2.1'
//and so on...
compile 'com.github.zakgof:actr-other-integration:0.2.1'
zakgof commented 4 years ago

Just added the CompletableFuture API to ActorRef. Released as v.0.3.0