Skip to content

Instantly share code, notes, and snippets.

@tjayr
Last active August 29, 2015 14:09
Show Gist options
  • Save tjayr/45744a36e4bda4880e65 to your computer and use it in GitHub Desktop.
Save tjayr/45744a36e4bda4880e65 to your computer and use it in GitHub Desktop.
Hello World actor concurrency showing examples with Scala/Akka and Erlang

Scala / Akka

import akka.actor.Actor
import com.clearprecision.neo4jscala.domain.Hello

class HelloActor extends Actor {

  override def receive: Receive = {
    case "Hello" => println("Hello World")
    case Hello(name) => println("Hello there "+name)
    case _ => println("Unknown message received")
  }

}

//Client

val actorSystem = ActorSystem("neo4jscala")
val helloActor = actorSystem.actorOf(Props[HelloActor])
helloActor ! Hello("Jim")
helloActor ! Hello
helloActor ! "Some nonsense"

Erlang

-module(hello_actor).
-export([start/0, loop/0]).

start() -> spawn(hello_actor, loop, []).

loop() ->
    receive
        {hello}         -> io:format("Hello world~n");
        {hello, Name}   -> io:format("Hello there ~s ~n", [Name]);
        _               -> io:format("Dont know what to do~n")
    end,
    loop().
Erlang/OTP 17 [erts-6.2] [source] [64-bit] [smp:8:8] [async-threads:10] [kernel-poll:false]

Eshell V6.2  (abort with ^G)
1> c(hello_actor).
{ok,hello_actor}
2> Act=hello_actor:start().
<0.40.0>
3> Act ! {hello}. 
Hello world
{hello}
4> Act ! {hello, "Jim"}.
Hello there Jim 
{hello,"Jim"}
5> Act ! "Some nonsense".                
Dont know what to do
"Some nonsense"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment