Coder Social home page Coder Social logo

jollywood's Introduction

Jollywood

Travis codecov.io

Simple actor system written in Java. Jollywood... actors... like Hollywood, but in Java, got the joke? Hahaha! Wonderful! But do not laugh the whole day and start working!

A little bit of theory

High-level overview

The basic entity is an Actor. This entity consist of these things:

  • mailbox (queue of messages to process represented by the Message class)
  • behavior definition which defines how the actor reacts on messages (StatefulActorDefinition, StatelessActorDefinition)

Each actor lives in an ActorSystem. An actor living in an actor system is represented by the ActorHandle class, which can be used to send messages to this actor. One actor definition can be used multiple times to create arbitrary actors in different systems. It is similar as object programming. You can imagine actor behavior definition as a class and the actor handle as an instance of that class.

Actor system

You can create actor system like this:

final ActorSystem system = new ActorSystem(8);

The number parameter of the constructor specifies a count of threads dedicated to processing actor messages.

To close the actor system after all actors are closed, call this:

system.shutdownAfterActorsClosed();

This method will block the current thread until all actors are closed.

Defining an actor

You can define both stateless and stateful actors.

Stateless actors

Stateless actors only define behavior based on messages. To define a behavior, you have to create an instance of StatelessActorDefinition functional interface.

StatelessActorDefinition statelessActorDef = new StatelessActorDefinition() {
    @Override
    public void processMessage(ActorHandle self, Message message) throws Exception {
        // ...
    }
};

Or you can make it shorter by using lambda:

StatelessActorDefinition statelessActorDef = (self, message) -> {
    // ...
};

After the definition is ready, register it to an actor system to obtain a reference to this actor, like this:

ActorHandle statelessActorRef = system.defineActor(statelessActorDef);

Stateful actors

Stateful actors define behavior based on message and some inner state, which is accessible whenever a message is processed.

StatefulActorDefinition<State> statefulActorDef = new StatefulActorDefinition<State>() {
    @Override
    public void processMessage(ActorHandle self, State state, Message message) throws Exception {
        // ...
    }
};

Or you can make it shorter by using lambda:

StatefulActorDefinition<State> statefulActorDef = (self, state, message) -> {
    // ...
};

After the definition is ready, register it to an actor system adding an initial state to obtain a reference to this actor, like this:

ActorHandle statefulActorRef = system.defineActor(statefulActorDef, new State());

Sending messages to single actor

When sending messages, you must posses a recipient reference (instance of ActorHandle).

You can send message to a single actor like this:

actorRef.sendMessage(self, "increment", "Hi, please increment your value by one.");

Also you might reply to an original message sender like this:

message.getSender().sendMessage(self, "response", "I liked your message.");

Sending messages to all actors in a system

You can also broadcast message to all actors in a certain system:

self.getSystem().broadcastMessage(self, "invitation", "Let us go for a beer!");

Closing an actor

It is possible to close the current actor while processing messages, like this:

self.closeActor();

Closing means that the closed actor will not accept any more messages and will be removed from the actor system after it finishes processing of the existing messages.

jollywood's People

Contributors

voho avatar

Watchers

 avatar  avatar

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.