JBoss
Rules, Rails
JAOO, Australia, 2009
Sydney, Brisbane
Michael Neale
JBoss R&D
Red Hat Middleware
1
Michael Neale
R&D on Drools (rule engine).
Open source history (user -> fulltime
developer).
Contribute to other jboss and non jboss
projects
me on the web:
www.michaelneale.net, twitter.com/michaelneale,
michaelneale.blogspot.com
2
Agenda
•Doing more with less...
•But isn't java hard/slow?
•Quick introduction to Rails basics
•Setting up JBoss for Rails
•What are rules and why use them?
•Drools
3
More, with less
Does any one need to:
Build apps of growing complexity
Do it quicker and cheaper
Cope with people changing their mind
All the time
At runtime
If not – you can go !
4
Drools:
Logic and declarative programming for
developers
User friendly GUIs and management
tools for the rest
Allows controlled changes to business
logic (even at runtime)
5
Rails
Popular RESTful web-app framework
(full stack)
Runs just fine on the JVM
Famous for productivity
6
Why the JVM?
7
Its the platform..
Not the language !
8
9
A fan...
Oh that reminds me, twitter?
10
Languages
Ruby
Scala **
Groovy
Clojure
Java7????
11
So not necessarily...
Big heavy and bloated
But it is a platform...
12
Introduction to
Ruby-on-Rails
13
What is Rails?
Rails is a “lightweight”
MVC framework for
building websites in
Ruby.
14
Rails MVC
•ActiveRecord for the models
• ERb templates for the view
•Simple ruby classes for the
controllers
15
ActiveRecord
Definitely is not
Hibernate, but works
well-enough.
16
ActiveRecord
class Comment < ActiveRecord::Base
belongs_to :post
belongs_to :user
validate_presence_of :text
...
end
17
ERb Templates
ERb allows snippets of
Ruby to be embedded
within HTML or other
templates.
18
ERb Templates
<p>
There are <%= @post.comments.size %>
new comments since your last visit:
</p>
<% for comment in @post.comments %>
<% div_for( comment ) do %>
<%= comment.author.full_name %>
<% end %>
<% end %>
19
ActionController
Your ActionController sub-
classes provides the logic
behind the pages and
forms.
20
ActionController
class CommentsController < ApplicationController
before_filter :load_post
def create
@comment = @post.comments.build( params[:comment] )
....
end
private
def load_post
@post = Post.find_by_slug( params[:slug] )
redirect_to posts_url and return false unless @post
end
end
21
ActionController
Actions
class CommentsController < ApplicationController
before_filter :load_post
def create
@comment = @post.comments.build( params[:comment] )
....
end
private
def load_post
@post = Post.find_by_slug( params[:slug] )
redirect_to posts_url and return false unless @post
end
end
22
ActionController
Filters
class CommentsController < ApplicationController
before_filter :load_post
def create
@comment = @post.comments.build( params[:comment] )
....
end
private
def load_post
@post = Post.find_by_slug( params[:slug] )
redirect_to posts_url and return false unless @post
end
end
23
Where’s the Java?
JRuby is a full and fast
Ruby interpreter built on
top of the Java Virtual
Machine.
24
JRuby
JRuby is actively
developed by Charlie
Nutter, Thomas Enebo,
and others.
25
Run Rails in the
JVM
After solving a few
issues, Rails runs
easily within most any
servlet container.
26
Why JBoss, then?
•JBoss provides a perfectly nice servlet
container.
•JBoss provides a whole lot more, too:
•JMS
• Transactions
•Rules-Engine
•Telecom
27
Rails is not enough
Rails is a great
solution...
for the web.
28
Beyond Rails
Apply the Ruby and
Rails philosophies to
enterprise-grade
services.
29
Ruby is just a
syntax
Approach Ruby as just
another syntax for
driving JEE/JVM
technologies.
30
Create the DB stuff
postgres> create user twiggl
with password 'twiggl';
CREATE ROLE
postgres> create database
twiggl_development
with owner twiggl
encoding 'UTF8';
CREATE DATABASE
57
Set up your app
config/database.yml
development:
adapter: postgresql
database: twiggl_development
username: twiggl
password: twiggl
host: localhost
encoding: UTF8
58
Set up your app
config/database.yml
development:
adapter: postgresql
Just like
database: twiggl_development
username: twiggl
regular Rails!
password: twiggl
host: localhost
encoding: UTF8
59
Database access
Rails on JBoss can take
advantage of JDBC
drivers.
60
Easy JDBC
The jboss-rails-support
library makes it easy to
set up your Rails app for
JDBC.
61
Install the JDBC
gems
$ rake jboss:gems:jdbc:install
62
You’re ready!
•AS5 is ready to serve Rails apps.
•A bare database is setup.
•A bare Rails app is setup.
• Configured to access the database.
•Using JDBC.
•Extra JBoss goodness is installed.
65
Deploying a Rails
App
The jboss-rails-
support rake tasks
handle deploying and
undeploying.
66
From your app
$ rake jboss:rails:deploy
Deployed twiggl
67
Start AS
$ rake jboss:as:run
(in /Users/bob/twiggl)
JBoss-Rails server: /Users/bob/jboss-5.0.1.GA/server/default
=========================================================================
JBoss Bootstrap Environment
JBOSS_HOME: /Users/bob/jboss-5.0.1.GA
JAVA: /System/Library/Frameworks/JavaVM.framework/Versions/1.6/Home//bin/java
JAVA_OPTS: -Dprogram.name=run.sh -Xms128m -Xmx512m -XX:MaxPermSize=256m -Dorg.jboss. resolver.warning=true -Dsun.
rmi.dgc.client.gcInterval=3600000 -Dsun.rmi.dgc.server.gcInterval=3600000
CLASSPATH: /Users/bob/preso/jboss-5.0.1.GA/bin/run.jar
=========================================================================
13:44:45,281 INFO [ServerImpl] Starting JBoss (Microcontainer)...
13:44:45,283 INFO [ServerImpl] Release ID: JBoss [Morpheus] 5.0.1.GA (build: SVNTag=JBoss_5_0_1_GA date=200902232048)
13:44:45,283 INFO [ServerImpl] Bootstrap URL: null
13:44:45,283 INFO [ServerImpl] Home Dir: /Users/bob/j boss-5.0.1.GA
13:44:45,283 INFO [ServerImpl] Home URL: file:/Users/bob/jboss-5.0.1.GA/
13:44:45,287 INFO [ServerImpl] Library URL: fi le:/Users/bob/jboss-5.0.1.GA/lib/
13:44:45,288 INFO [ServerImpl] Patch URL: null
13:44:45,288 INFO [ServerImpl] Common Base URL: fi le:/Users/bob/jboss-5.0.1.GA/common/
13:44:45,288 INFO [ServerImpl] Common Library URL: file:/Users/bob/jboss-5.0.1.GA/common/lib/
13:44:45,288 INFO [ServerImpl] Server Name: default
13:44:45,289 INFO [ServerImpl] Server Base Dir: /Users/bob/jboss-5.0.1.GA/server
13:44:45,289 INFO [ServerImpl] Server Base URL: file:/Users/bob/jboss-5.0.1.GA/server/
13:44:45,289 INFO [ServerImpl] Server Confi g URL: file:/Users/bob/jboss-5.0.1.GA/server/default/conf/
13:44:45,289 INFO [ServerImpl] Server Home Dir: /Users/bob/jboss-5.0.1.GA/server/default
13:44:45,289 INFO [ServerImpl] Server Home URL: file:/Users/bob/jboss-5.0.1.GA/server/default/
70
Create models
db/migrate/*_create_twigs.rb
class CreateTwigs < ActiveRecord::Migration
def self.up
create_table :twigs do |t|
t.string :name, :limit=>42, :null=>false
t.timestamps
end
end
def self.down
drop_table :twigs
end
end
77
Blow it into the DB
$ rake db:migrate
(in /Users/bob/twiggl)
== CreateTwigs: migrating
===============
-- create_table(:twigs)
-> 0.0669s
-> 0 rows
== CreateTwigs: migrated (0.0683s) ======
78
Rough in a controller
app/controllers/twigs_controller.rb
class TwigsController < ApplicationController
def index
@twigs = Twig.find( :all )
end
end
79
Rough in a template
app/views/twigs/index.html.erb
<p>
There are <%= @twigs.size %> twigs.
</p>
<% for twig in @twigs %>
<% div_for( twig ) do %>
<%= twig.name %>
<% end %>
<% end %>
80
Rough in a template
app/views/twigs/index.html.erb
<p>
There are <%= @twigs.size %> twigs.
</p>
<% for twig in @twigs %>
<% div_for( twig ) do %>
<%= twig.name %>
<% end %>
<% end %>
81
Rough in a template
app/views/twigs/index.html.erb
<p>
There are <%= @twigs.size %> twigs.
</p>
<% for twig in @twigs %>
<% div_for( twig ) do %>
<%= twig.name %>
<% end %>
<% end %>
82
Hit it!
83
We didn’t restart
AS.
And we didn’t redeploy
our app, either. That’s
okay, we’re hacking hot
code.
84
Twig management
app/controllers/twigs_controller.rb
class TwigsController < ApplicationController
def new
@twig = Twig.new
end
def create
@twig = Twig.create( params[:twig] )
redirect_to @twig
end
end
85
Twig form
app/views/twigs/new.html.erb
<% form_for @twig do %>
<%= label :twig, :name %>
<%= text_field :twig, :name %>
<%= submit_tag 'Create twig!' %>
<% end %>
86
Twig management
app/controllers/twigs_controller.rb
class TwigsController < ApplicationController
def show
@twig = Twig.find_by_id( params[:id] )
redirect_to twigs_url unless @twig
end
end
87
Twig view
app/views/twigs/show.html.erb
<% div_for @twig do %>
<h1><%= @twig.name %></h1>
<% end %>
88
Create a twig
89
Create a twig
90
Back to the index
91
It’s just like
regular Rails.
92
Beyond Rails
But, we have the entire
freakin’ JBoss AS5
there.
Let’s use it.
93
Job scheduling
Sometimes you need
some code to fire on a
recurring basis, outside of
the web context.
94
Job class
app/jobs/twitter_sender.rb
class TwitterSender < JBoss::Jobs::BaseJob
def run
new_twigs = Twig.find( :all,
:conditions=>[
'created_at >= ?',
Time.now - 30.minutes
] )
return if new_twigs.empty?
tweet( new_twigs.size )
end
def tweet(num_new_twigs)
..
end
end
97
Hot, hot code
Once your job is
deployed, you can
continue to edit the
actual service method.
106
No further
redeployment is
necessary, unless you
add jobs or alter the
schedule.
107
Some More Possibilities
Message Queues (MQ/JMS)
Legacy/JCA integration
WS style web services...
108
Ruby Databinding
JBoss-Rails provides
automatic databinding
to XMLSchema types.
109
Ruby Databinding
When WS deployed, the
WSDL is examined and
real Ruby classes are
created.
110
Shifting gears...
111
What are rules?
Business rules
Declarative statements
Logic/reasoning
112
What are rules?
Capture domain expert
knowledge as
statements/assertions of truth
(aka: business rules)
113
What are rules?
An executable knowledge base
114
Drools
Drools provides a runtime,
compiler and tools to develop
and manage executable
knowledge bases
(all open source of course)
115
For example:
rule “age and history compliance”
when
Driver(age < 25, sex=”M”)
not Accident(severity > 3)
not Infringement(type==”dui”)
then
approveDriver(“Let this one through”)
end
116
For example:
rule “age and history compliance”
when
Driver(age < 25, sex=”M”)
not Accident(severity > 3)
not Infringement(type==”dui”)
then
approveDriver(“Let this one through”)
end
117
For example:
rule “age and history compliance”
when
Driver(age < 25, sex=”M”)
not Accident(severity > 3)
not Infringement(type==”dui”)
then
approveDriver(“Let this one through”)
end
118
For example:
rule “age and history compliance”
when
Driver(age < 25, sex=”M”)
not Accident(severity > 3)
not Infringement(type==”dui”)
then
approveDriver(“Let this one through”)
end
119
For example:
rule “age and history compliance”
when
Driver(age < 25, sex=”M”)
not Accident(severity > 3)
not Infringement(type==”dui”)
then
approveDriver(“Let this one through”)
end
120
For example:
rule “age and history compliance”
when
Driver(age < 25, sex=”M”)
not Accident(severity > 3)
not Infringement(type==”dui”)
then
approveDriver(“Let this one through”)
end
121
For example:
rule “age and history compliance”
when
Driver(age < 25, sex=”M”)
not Accident(severity > 3)
not Infringement(type==”dui”)
then
approveDriver(“Let this one through”)
end
122
Why?
You have domain experts
The rules change
Anti-spaghetti
if else if else if else ...
123
124
125
126
127
The Guvnor..
Repository and Web interface
128
129
130
131
132
133
134
How?
In process on the JVM
As a decision service
135
Where?
Logistics (Fedex – talk at J1)
Insurance, risk, fraud etc..
All domains..
136
Questions!
For more info:
Contact me: http://twitter.com/michaelneale
http://oddthesis.org/theses/jboss-rails/projects/jboss-rails
http://jboss.org/drools
137
0 comments
Post a comment