JRuby Jetlang Example


/ Published in: Ruby
Save to your folder(s)



Copy this code and paste it in your HTML
  1. require "java"
  2. require "jetlang-0.2.0.jar"
  3.  
  4. include_class "org.jetlang.fibers.ThreadFiber"
  5. include_class "org.jetlang.channels.MemoryChannel"
  6.  
  7.  
  8. class Arnie
  9. def initialize(channel, consumer)
  10. @channel = channel
  11. @consumer = consumer
  12. end
  13. def start
  14. on_receive = Proc.new do |message|
  15. case message
  16. when "The End"
  17. puts "I will be back..."
  18. @consumer.dispose
  19. @consumer.join
  20. when "Terminate"
  21. puts "Hastala vista baby!!!"
  22. else
  23. puts "You are terminated******"
  24. end
  25. end
  26. @channel.subscribe(@consumer, on_receive)
  27. @consumer.start
  28. end
  29. def ^(message)
  30. @channel.publish(message)
  31. end
  32. end
  33.  
  34. terminator = Arnie.new(MemoryChannel.new, ThreadFiber.new)
  35. terminator.start
  36. terminator ^ "Terminate"
  37. terminator ^ "Buy me icecream"
  38. terminator ^ "The End"
  39. terminator ^ "Terminate" # Will not execute as terminator is already dead...

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.