Coder Social home page Coder Social logo

hazelcast-actor's Introduction

What

This project is a simple library implementing the actor model for concurrant programming. Using Hazelcast for the message passing allows a very simple scaling of the actors accross multiple nodes.

How

A simple echo actor

final class EchoActor extends AbstractActor<String, String> {

    public EchoActor() {
        super(new DefaultNamingStrategy(), EchoActor.class, true);
    }

    @Override public String call(String input)
    {
        return input;
    }
}

The actor worker

Occasionally you need to scale your actor, i.e. run the same actor with multiple threads. For this you can use the ActorWorker and a corresponding factory to run the actor inside an ExecutorService.

private class EchoActor extends AbstractActorWorker<String, String> {

    protected EchoActor(InputMessage<String> inputMsg, ITopic<OutputMessage<String>> topic) {
        super(inputMsg, topic);
    }

    @Override
    public String call(String input) {
        return input;
    }

}

class EchoActorFactory implements ActorWorkerFactory<String, String> {

    @Override
    public Class<? extends AbstractActorWorker<String, String>> getClazz() {
        return EchoActor.class;
    }

    @Override
    public AbstractActorWorker<String, String> newInstance(InputMessage<String> input, ITopic<OutputMessage<String>> topic) {
        return new EchoActor(input, topic);
    }

}

In order to instantiate the actor you could then do something like this:

ActorManager<String, String> echoManager = new ActorManager<String, String>(new DefaultNamingStrategy(), new EchoActorFactory());
Thread manager = new Thread(echoManager);
manager.start();

And directors

DirectorImpl<String, String> echoProxy = new DirectorImpl<String, String>(new DefaultNamingStrategy(), EchoActor.class);
Future<String> future = echoProxy.call("Test");

Or you can call the same actor with a list of strings and you will get a list of futures (wow, multiple futures...)

List<Future<String>> futures;
List<String> strings = new ArrayList<String>() {
    private static final long serialVersionUID = 7328631044879958719L;
    {
        this.add("Test1");
        this.add("Test2");
    }
};

futures = echoProxy.call(strings);

Notes

Actually I am not really sure if it is appropriate to run the actor within an ExecutorService but this is the only way I could think of to allow recursive calls, e.g. Maybe someone else will guide me to the right path!?

hazelcast-actor's People

Stargazers

 avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

hazelcast-actor's Issues

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.