Created
February 16, 2010 10:27
-
-
Save saivenkat/305440 to your computer and use it in GitHub Desktop.
JRuby Jetlang Message Passing Example
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require "java" | |
require "jetlang-0.2.0.jar" | |
include_class "org.jetlang.fibers.ThreadFiber" | |
include_class "org.jetlang.channels.MemoryChannel" | |
class Arnie | |
def initialize(channel, consumer) | |
@channel = channel | |
@consumer = consumer | |
end | |
def start | |
on_receive = Proc.new do |message| | |
case message | |
when "The End" | |
puts "I will be back..." | |
@consumer.dispose | |
@consumer.join | |
when "Terminate" | |
puts "Hastala vista baby!!!" | |
else | |
puts "You are terminated******" | |
end | |
end | |
@channel.subscribe(@consumer, on_receive) | |
@consumer.start | |
end | |
def ^(message) | |
@channel.publish(message) | |
end | |
end | |
terminator = Arnie.new(MemoryChannel.new, ThreadFiber.new) | |
terminator.start | |
terminator ^ "Terminate" | |
terminator ^ "Buy me icecream" | |
terminator ^ "The End" | |
terminator ^ "Terminate" # Will not execute as terminator is already dead... |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment