Browse files

Example of concurrent topic message processing

  • Loading branch information...
1 parent 8c7e7ce commit 97f01ae07c3e8a28f0c20f78ad8629c56d0a5a32 @bbrowning bbrowning committed Apr 1, 2011
View
1 concurrent-topics/Rakefile
@@ -0,0 +1 @@
+require "torquebox-rake-support"
View
16 concurrent-topics/dispatch.rb
@@ -0,0 +1,16 @@
+require 'rubygems'
+require 'torquebox-messaging'
+
+class Dispatch < TorqueBox::Messaging::MessageProcessor
+ include TorqueBox::Injectors
+
+ def initialize
+ puts "Dispatch constructor"
+ @queue = inject("/queues/rackapp1/local")
+ end
+
+ def on_message(msg)
+ puts "dispatch received msg: #{msg.inspect}"
+ @queue.publish(msg)
+ end
+end
View
16 concurrent-topics/list_files.rb
@@ -0,0 +1,16 @@
+require 'rubygems'
+require 'torquebox-messaging'
+
+class ListFiles
+ include TorqueBox::Injectors
+
+ def initialize
+ @path = File.dirname(__FILE__)
+ @topic = inject('/topics/rackapp1/local')
+ end
+
+ def run
+ dir = Dir.new(@path)
+ @topic.publish dir.entries
+ end
+end
View
18 concurrent-topics/torquebox.yml
@@ -0,0 +1,18 @@
+queues:
+ /queues/rackapp1/local:
+topics:
+ /topics/rackapp1/local:
+
+messaging:
+ /topics/rackapp1/local:
+ Dispatch:
+ concurrency: 1
+ /queues/rackapp1/local:
+ Worker:
+ concurrency: 2
+
+jobs:
+ jobname_listfiles:
+ job: ListFiles
+ cron: '*/5 * * * * ?'
+ desc: Publish a folder listing
View
14 concurrent-topics/worker.rb
@@ -0,0 +1,14 @@
+require 'rubygems'
+require 'torquebox-messaging'
+
+class Worker < TorqueBox::Messaging::MessageProcessor
+ def initialize
+ puts "Worker constructor"
+ end
+
+ def on_message(msg)
+ puts "worker #{object_id} received message #{msg.inspect}"
+ sleep(6)
+ puts "worker #{object_id} finished"
+ end
+end

0 comments on commit 97f01ae

Please sign in to comment.