Skip to content

Instantly share code, notes, and snippets.

@heuristicfencepost
Created September 13, 2011 02:15
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save heuristicfencepost/1212995 to your computer and use it in GitHub Desktop.
Save heuristicfencepost/1212995 to your computer and use it in GitHub Desktop.
Sample Ruby code for working with Akka in JRuby
require 'java'
require 'akka-actor-1.2-RC6.jar'
require 'scala-library.jar'
java_import 'akka.actor.UntypedActor'
java_import 'akka.actor.Actors'
# Start with something simple. Implement the actor as a distinct
# class and start it up within the Akka runtime.
# Look, I've implemented the actor API!
class SomeActor < UntypedActor
def onReceive msg
puts "Received message: #{msg.to_s}"
end
end
# So we should be fine then, right?
ref = Actors.actorOf SomeActor.new
ref.start
ref.tell "foo"
require 'java'
require 'akka-actor-1.2-RC6.jar'
require 'scala-library.jar'
java_import 'akka.actor.UntypedActor'
java_import 'akka.actor.Actors'
# A second attempt. Define our actor in a standlone class again
# but this time use an ActorFactory (via closure coercion) to
# interact with Akka.
# Define our actor in a class again...
class SomeActor < UntypedActor
def onReceive msg
puts "Received message: #{msg.to_s}"
end
end
# ... and then provide an UntypedActorFactory to instantiate
# the actor. The factory interface qualifies as a SAM so
# we only need to provide a closure to generate the actor and
# let JRuby's closure coercion handle the rest.
ref = Actors.actorOf do
SomeActor.new
end
ref.start
ref.tell "foo"
require 'java'
require 'akka-actor-1.2-RC6.jar'
require 'scala-library.jar'
java_import 'akka.actor.UntypedActor'
java_import 'akka.actor.UntypedActorFactory'
java_import 'akka.actor.Actors'
# Shift to working with code blocks via a generic actor and a simple
# factory for creating them.
class SomeActor < UntypedActor
# Look, I've got a constructor... but it'll get ignored!
def initialize(proc)
@proc = proc
end
def onReceive(msg)
@proc.call msg
end
end
class SomeActorFactory
include UntypedActorFactory
def initialize(&b)
@proc = b
end
def create
SomeActor.new @proc
end
end
# Create a factory for an actor that uses the input block to pass incoming messages.
factory = SomeActorFactory.new do |msg|
puts "Message recieved: #{msg.to_s}"
end
ref = Actors.actorOf factory
ref.start
ref.tell "foo"
require 'java'
require 'akka-actor-1.2-RC6.jar'
require 'scala-library.jar'
java_import 'akka.actor.UntypedActor'
java_import 'akka.actor.UntypedActorFactory'
java_import 'akka.actor.Actors'
# Continue with the generic actor implementation, but shift to using a setter
# for passing in the Proc to which we'll delegate message handling.
class SomeActor < UntypedActor
def proc=(b)
@proc = b
end
def onReceive(msg)
@proc.call msg
end
end
class SomeActorFactory
include UntypedActorFactory
def initialize(&b)
@proc = b
end
def create
rv = SomeActor.new
rv.proc = @proc
rv
end
end
# Create a factory for an actor that uses the input block to pass incoming messages.
factory = SomeActorFactory.new do |msg|
puts "Message recieved: #{msg.to_s}"
end
ref = Actors.actorOf factory
ref.start
ref.tell "foo"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment