Presenting...

logo knob

Saleem Ansari

twitter: tuxdna

10th February, 2012 at GNUnify

Presenter Notes

Outline

What is TorqueBox?

Polyglot Revolution

TorqueBox

How to use TorqueBox?

Features

Installation and setup: RVM, JRuby, TorqueBox

Deployment

Features contd.

How to begin hacking TorqueBox?

JBoss Polyglot

TorqueBox

Presenter Notes

What is TorqueBox?

Before that lets talk about Polyglot Revolution

Before moving onto TorqueBox, let me first introduce you to the Polyglot Revolution. TorqueBox is a part of Polyglot Revolution itself as we will see later. Today there are many programming languages whose compilers or interpreters target the JVM. Those languages include JRuby, Jython, Scala, Erlang ( Erlang on JVM ), and many more. Many of these languages are dynamically typed languages (or dynamic languages).

Presenter Notes

Polyglot Revolution

What does this enable us to do? Well, basically it enables you to write a program in any of the JVM languages, which gets compiled to Java byte-codes. So, whenever your program written in one JVM language executes, it can also execute the bytecodes generated for any other JVM targeted language. As simple it sounds, it is a very powerful feature because you can now use all your Java code that was written and tested over the years.

JVM is what makes the Polyglot Revolution.

Presenter Notes

Polyglot Revolution cont.

It becomes even more useful in enterprise software, where the projects involve multiple technologies. As a part of Polyglot Revolution there are a bunch of projects in different languages:

TorqueBox (Ruby)

Immutant (Clojure)

Blacktie (C++)

Erjang (Erlang)

Presenter Notes

back to TorqueBox

Started in 2009 by Bob McWhirter @bobmcwhirter

A complete Ruby Application Server

Why?

Productivity

Power and stability of Java Middleware

Scale on demand

TorqueBox is a Ruby Application Server. By Application Server I mean that, it is not just a web framework, but it has batteries included. It is built on top of tested and proven technologies. While JRuby provides you ability to call Java code from within your Ruby code ( and vice-versa ), TorqueBox provides the integration at the application level. It provides the ability to consume the services provided by JBoss and write your own if not avaiable.

Presenter Notes

TorqueBox contd.

stack

Built on

JRuby / Ruby

JBoss AS

Enterprise Java ( EJB, JMS, Caching etc. )

Provides

Messaging

Jobs

Caching

Services

Clustering

Presenter Notes

TorqueBox howto?

Installation

Ruby, RVM and JRuby

1 yum install -y ruby rubygem-rvm
2 rvm-install
3 [[ -s "$HOME/.rvm/scripts/rvm" ]] && . "$HOME/.rvm/scripts/rvm"
4 rvm install jruby-1.6.5
5 rvm use jruby-1.6.5

TorqueBox (prerelease/latest version)

1 gem install --pre torquebox
2 gem install --pre torquebox-messaging-container
3 gem install --pre torquebox-naming-container 
4 gem install --pre torquebox-capistrano-support
5 gem install --pre torquebox-rake-support
6 gem install --pre torquebox-vfs 
7 jruby -J-Xmx1024m -w -S \
8   gem install --pre torquebox-server \
9   --source http://torquebox.org/2x/builds/LATEST/gem-repo

Presenter Notes

TorqueBox howto?

Installation contd.

Rails (latest version)

1 gem install rails
2 gem install bundler

Some caveats

JDBC Drivers for ActiveRecord

1 gem install activerecord-jdbcsqlite3-adapter
2 gem install activerecord-jdbcmysql-adapter

Presenter Notes

TorqueBox howto?

Create a new Rails application

1 rails new rails-app
2 cd rails-app
3 bundle install
4 rails g scaffold Person name:string
5 rake db:migrate

and deploy!

1 torquebox run
2 torquebox deploy /path/to/rails-app

Presenter Notes

Feature: Messaging (via HornetQ JMS)

Sending Messages:

1 timer_queue = TorqueBox::Messaging::Queue.new('/queues/timer')
2 timer_queue.publish "Some Message"
3 topic1 = TorqueBox::Messaging::Topic.new('/topics/topic1')
4 topic1.publish "Some Topic"

Processing Messages:

Create a Message class

1 include TorqueBox::Messaging
2 class MyMessageHandler < MessageProcessor
3   def on_message(body)
4     # process the message
5   end
6 end

Add queue/topic to config/torquebox.yml

 1 queues:
 2   /queues/timer:
 3 topics:
 4   /topics/topic1:
 5 
 6 messaging:
 7   /queues/timer:
 8     MyMessageHandler:
 9       concurrency: 5
10   /topics/topic1:
11     Topic1Handler

Presenter Notes

Feature: Jobs

Jobs ( Backgroundable ):

include TorqueBox::Messaging::Backgroundable to any Ruby class

1 class LuceneIndexJob
2   include TorqueBox::Messaging::Backgroundable
3   def do_index
4     # very long running job
5   end
6 end

get the future ( optionally store it in a serialized format somewhere )

1 indexer = LuceneIndexJob.new
2 future = indexer.background.do_index
3 future.started?
4 future.complete?
5 future.error?
6 future.status
7 # store future object for later retrieval ( maybe in database )

Presenter Notes

Feature: Jobs contd.

Scheduled Jobs:

Create a class with run() method

1 class MonthlyBirthdayEmailer
2   def run
3     # some long task here
4   end
5 end

Add entry on config/torquebox.yml with crontab(5) like time specification

1 jobs:
2   monthly_birthday_notifications:
3     description: first of month
4     job: MonthlyBirthdayEmailer
5     cron: '0 0 0 1 * ?'

Presenter Notes

Feature: Caching

Caching ( via Infinispan ):

Add gem 'torquebox-cache' to Gemfile

1 gem 'torquebox-cache'

update application.rb to use torquebox cache

1 class Application < Rails::Application
2   config.cache_store = :torque_box_store
3 end

access the cache

 1 require 'torquebox-cache'
 2 cache = TorqueBox::Infinispan::Cache.new( :name => 'treasure',
 3   :persist=>'/data/treasure' )
 4 # Put some stuff in the cache
 5 cache.put('akey', "a string value" )
 6 cache.put("time", Time.now )
 7 cache.put(user.id, user )
 8 # Get it back again
 9 time = cache.get( "time" )
10 user = cache.get( params[:id] )

Presenter Notes

Feature: Services

Crete a class with initialize, start and stop methods

 1  class TimeService
 2    def initialize(opts)
 3      puts "Queue Name: #{opts['queue']}"
 4      @queue=TorqueBox::Messaging::Queue.new(opts['queue'])
 5    end
 6    def start
 7      Thread.new{run}
 8    end
 9    def stop
10      @done=true
11    end
12    def run
13      until @done
14        @queue.publish(Time.now)
15        sleep(1)
16      end
17    end
18  end

Add service to application configuration file

1 services:
2   TimeMachine:
3     config:
4       queue: /queue/time

Presenter Notes

Feature: Clustering

Clustering:

torquebox run --clustered

clustered deployment

clustered cache

shared web sessions etc.

High Availability Singleton Services

1 $JBOSS_HOME/bin/standalone.sh --server-config=standalone-ha.xml
2 mark the service as singleon: true in the configuration file

Presenter Notes

Hacking TorqueBox

block_diagram

Presenter Notes

Hacking TorqueBox contd.

JBoss-Polyglot

github.com/projectodd/jboss-polyglot

TorqueBox

github.com/torquebox/torquebox

Presenter Notes

Summary

Built on Ruby / JRuby, TorqueBox is well suited for rapid application development in the settings where Enterprise Java Applications are already in place.

TorqueBox Provides these features out of the box, without any hassle:

Messaging, Jobs, Caching, Services and Clustering

TorqueBox / JRuby gotchas:

TorqueBox gives you real threads but many existing Ruby libraries suck with multi threading.

Can invoke Java

Does Real Threading

FFI support

Native C extensions written for MRI Ruby dont work

Native gems are not supported, except FFI.

Presenter Notes

Questions?

IRC: #torquebox on FreeNode Web: torquebox.org

Presenter Notes

Thanks

Presenter Notes