Did he just say Ruby at a Java conference?
Yep, it's a
based on JBoss AS
$ torquebox run
Let's refresh the knowledge!
There are
and
# Creates and starts a new queue
@queue = TorqueBox::Messaging::Queue.start("/queues/new_queue")
# Creates and starts a new topic
@topic = TorqueBox::Messaging::Topic.start("/topics/new_topic")
TorqueBox.configure do
# Creates and starts the queue with the application
queue '/queues/one'
# Creates and starts the topic with the application
topic '/topics/one'
end
torquebox.rb
Creating and destroying destinations
# Sending a text message
@queue.publish("Hello!")
# Sending a Ruby Hash
@queue.publish(:type => :car, :price => 20455.73)
@queue.publish("Hello!",
:properties => {
'type' => 'CUCUMBER', # String
'count' => 126, # Integer
'price' => 4367.23, # Float
'valid' => true # Boolean
}
)
# Watch out - this call can block
# the thread forever if no new messages arrive
message = @queue.receive
# Wait at most 10 seconds for the message
message = @queue.receive(:timeout => 10_000)
Sending and receiving messages
class SimpleProcessor < TorqueBox::Messaging::MessageProcessor
def on_message(message)
puts "Received '#{message}'"
end
end
TorqueBox.configure do
# Define the queue
queue '/queues/stuff' do
# Specify the message processor implementation
processor SimpleProcessor
end
end
torquebox.rb
Using message processors
type = 'CUCUMBER' AND price < 1.23
action = 'BUY' AND JMSPriority BETWEEN 5 AND 8
name LIKE 'MAR%'
message = @queue.receive(:selector => "type = 'CUCUMBER' AND price < 1.23")
TorqueBox.configure do
# Define the queue
queue '/queues/selector' do
# Specify the message processor implementation
processor SimpleProcessor do
# Define the filter expression
selector "score > 5"
end
end
end
torquebox.rb
Using message selectors
By default: 4
Message provider delivers messages with higher priority first
:low
set to 1:normal
set to 4:high
set to 7:critical
set to 9# low priority (1)
@queue.publish("Is someone there?", :priority => :low)
# default priority (4)
@queue.publish("Hello!")
# critical priority (9)
@queue.publish("Urgent!", :priority => :critical)
Message priorities
By default: publish NOW!
# Message is published immediately (:scheduled set to 0)
@queue.publish("Normal message")
# Message is published 10 seconds after executing the call
@queue.publish("Scheduled message", :scheduled => Time.now + 10)
Message scheduling
You said that messaging is asynchronous!
# Publish a message, then wait for reply
answer = @queue.publish_and_receive("Hello")
# Wait for a message and reply
@queue.receive_and_publish do |message|
message.upcase
end
Synchronous messaging
Message management
Queue management
Things you want to execute sometime in the future
class SimpleJob
def run
# Stuff goes here
end
end
simple_job.rb
Seconds | Minutes | Hours | Day of month | Month | Day of week | Year |
---|---|---|---|---|---|---|
*/5 | * | * | * | * | ? | |
0 | 15 | 3 | 1 | * | ? |
TorqueBox::ScheduledJob.schedule(
"SimpleJob",
'*/5 * * * * ?',
:name => 'simple.job',
:description => 'Simple job',
:timeout => '5s',
:config => { 'foo' => 'bar' }
)
TorqueBox.configure do
job SimpleJob do
cron '*/5 * * * * ?'
end
end
torquebox.rb
Cron jobs
# Run a job every 200 ms for over 5 seconds, from now
TorqueBox::ScheduledJob.at(
'SimpleJob', :every => 200, :until => Time.now + 5)
# Start in 1 second, then every 200 ms for over 4 seconds
TorqueBox::ScheduledJob.at(
'SimpleJob', :at => Time.now + 1, :every => 200, :until => Time.now + 5)
# Start in 1 second, then every 200 ms for over 4 seconds
TorqueBox::ScheduledJob.at(
'SimpleJob', :in => 1_000, :every => 200, :until => Time.now + 5)
# Start in 1 second, then repeat te job 10 times, every 200 ms
TorqueBox::ScheduledJob.at(
'SimpleJob', :in => 1_000, :repeat => 10, :every => 200)
'At' jobs
All mentioned features require TorqueBox 3
Or at least latest incrementals