JRuby HackDay 1

2:45pm - 3:15pm Panel

3:15pm - 4:00pm Working Session 1

Goals:

Resources:

http://rubyforge.org/projects/jruby-extras/ http://dist.codehaus.org/jruby/

Mac:

Download it

mkdir ~/Hackday/
cd ~/Hackday/
curl -O http://dist.codehaus.org/jruby/jruby-bin-1.0.tar.gz
tar xvzf jruby-bin-1.0.tar.gz
rm jruby-bin-*
export PATH=~/Hackday/jruby-1.0/bin:$PATH

Test with jirb

jirb
irb(main):001:0> string = 'JRuby'        
=> "JRuby"
irb(main):002:0> print string
JRuby=> nil
irb(main):003:0> string.upcase
=> "JRUBY"
irb(main):004:0> exit

Look, it comes with useful stuff. Like Rake. Like RSpec.

jruby -S rake --help
jruby -S spec --help

Gems

Let’s get Rails in there (not using —env-shebang will give you “require: command not found” etc).

which gem
gem update
gem install rails -y --no-rdoc --env-shebang
which rails
head ~/Hackday/jruby-1.0/bin/rails
rails railstest

And the mysql connector for Java

cd ~/Hackday/jruby-1.0/lib/
curl -O http://jxh.bingodisk.com/bingo/public/software/mysql-connector-java-5.0.7/mysql-connector-java-5.0.7-bin.jar

Now we’ll do activerecord-jdbc, openssl, rmagick and hpricot (select “1. hpricot 0.6 (jruby)”), jrexml and json_pure

gem install activerecord-jdbc -y --no-rdoc --env-shebang
gem install jruby-openssl -y --no-rdoc --env-shebang
gem install hpricot -y --no-rdoc --env-shebang
gem install rmagick4j -y --no-rdoc --env-shebang
gem install jrexml -y --no-rdoc --env-shebang
gem install json_pure -y --no-rdoc --env-shebang

There’s some funniness with installing the javafied mongrel via gem.rubyforge.org (the inability to skip fastthread’s installation chokes it)

cd ~/Hackday/jruby-1.0/lib/ruby/gems/1.8/cache
curl -O http://jxh.bingodisk.com/bingo/public/software/mongrel-1.0.1-jruby.gem
curl -O http://jxh.bingodisk.com/bingo/public/software/mongrel_jcluster-0.0.1.gem
gem install daemons -y --no-rdoc --env-shebang
gem install cgi_multipart_eof_fix -y --no-rdoc --env-shebang
gem install gem_plugin
gem install mongrel-1.0.1-jruby.gem -y --no-rdoc --env-shebang
gem install mongrel_jcluster-0.0.1.gem -y --no-rdoc --env-shebang
which mongrel_rails
head ~/Hackday/jruby-1.0/bin/mongrel_rails

We’ll just make sure there’s not any permission issues post-installation where something isn’t executable.

chmod -R 755 ~/Hackday/jruby-1.0/bin

Let’s go back to our railstest

cd ~/Hackday/railstest 
mongrel_rails start

Fire up a browser and head over to http://localhost:3000/, you’re now looking at a JRuby-driven, Mongrel-served Rails application.

Solaris Accelerators

Directory layout

This is a Jason bias. 1 = binaries and libraries that have to be configured and access local or remote data, no machine specific configuration files are allowed to live here, 2 = “user” applications, 3 = configuration files and data.

cd /
mkdir -p /a/1
mkdir -p /a/2
mkdir -p /a/3

cd /a/1/
mkdir app
mkdir databases
mkdir languages
mkdir unix
mkdir web

cd /a/2
mkdir applications

cd /a/3
mkdir data
mkdir configurations

Download it

cd /a/1/languages
curl -O http://dist.codehaus.org/jruby/jruby-bin-1.0.tar.gz
tar zxvf jruby-bin-1.0.tar.gz
rm jruby-bin-1.0.tar.gz 
ln -s /a/1/languages/jruby-1.0 /a/1/languages/jruby
ls -l

Re-org on the Java

cp -R /usr/java /a/1/languages/java

Set the environments so that you’re pointing to this Java and JRuby

export JAVA_HOME=/a/1/languages/java
export PATH=/a/1/languages/jruby/bin:$PATH

Test with jirb

jirb
irb(main):001:0> string = 'JRuby'        

=> "JRuby"
irb(main):002:0> print string
JRuby=> nil

irb(main):003:0> string.upcase
=> "JRUBY"
irb(main):004:0> exit

Gems

Let’s get Rails in there.

which gem
gem update
gem install rails -y --no-rdoc --env-shebang
which rails
head /a/1/languages/jruby/bin/rails
rails /home/railstest

And the mysql connector for Java

cd /a/1/languages/jruby/lib/
curl -O http://jxh.bingodisk.com/bingo/public/software/mysql-connector-java-5.0.7/mysql-connector-java-5.0.7-bin.jar

Now we’ll do activerecord-jdbc, openssl, rmagick and hpricot (select “1. hpricot 0.6 (jruby)”), jrexml and json_pure

gem install activerecord-jdbc -y --no-rdoc --env-shebang
gem install jruby-openssl -y --no-rdoc --env-shebang
gem install hpricot -y --no-rdoc --env-shebang
gem install rmagick4j -y --no-rdoc --env-shebang
gem install jrexml -y --no-rdoc --env-shebang
gem install json_pure -y --no-rdoc --env-shebang

There’s some funniness with installing the javafied mongrel via gem.rubyforge.org (the inability to skip fastthread’s installation chokes it)

cd /a/1/languages/jruby/lib/ruby/gems/1.8/cache
curl -O http://jxh.bingodisk.com/bingo/public/software/mongrel-1.0.1-jruby.gem
curl -O http://jxh.bingodisk.com/bingo/public/software/mongrel_jcluster-0.0.1.gem
gem install daemons -y --no-rdoc --env-shebang
gem install cgi_multipart_eof_fix -y --no-rdoc --env-shebang
gem install gem_plugin
gem install mongrel-1.0.1-jruby.gem -y --no-rdoc --env-shebang
gem install mongrel_jcluster-0.0.1.gem -y --no-rdoc --env-shebang
which mongrel_rails
head /a/1/languages/jruby/bin/mongrel_rails

We’ll just make sure there’s not any permission issues post-installation where something isn’t executable.

chmod -R 755 /a/1/languages/jruby/bin/*

Let’s go back to our railstest

cd /home/railstest 
mongrel_rails start

Fire up a browser and head over to http://AcceleratorIP:3000/, you’re now looking at a JRuby-driven, Mongrel-served Rails application.

4:00pm - 4:15pm Break

Goals:

4:15pm - 4:30pm Panel: Diving Deeper: Gory Details of JRuby Development

Goals:

4:30pm - 5:45pm Coding your first JRuby App

Goals:

Hello world - single mongrel

Or come up with something on your own. Steps below seem boring? They’re supposed to be. JRuby on Rails development strives to be exactly what normal Rails development already is.

Mac

Add the controller and view

cd ~/Hackday/railstest
jruby script/generate controller hello
nano app/views/hello/index.rhtml

Fire up a mongrel on a port

mongrel_rails start -p 4000

Take a look in the browser

http://localhost:4000/
http://localhost:4000/hello/

Modify the route

nano config/routes.rb 
    >map.connect '', :controller => "hello"
mv public/index.html public/welcome-index.html

Kill the mongrel with a ctrl-C and restart

mongrel_rails start -p 4000

Take a look in the browser

http://localhost:4000/
http://localhost:4000/hello/

Accelerator

Add the controller and view

cd /home/railstest 
jruby script/generate controller hello
nano app/views/hello/index.rhtml

Fire up a mongrel on a port

mongrel_rails start -p 4000

Take a look in the browser

http://AcceleratorIP:4000/
http://AcceleratorIP:4000/hello/

Modify the route

nano config/routes.rb 
    >map.connect '', :controller => "hello"
mv public/index.html public/welcome-index.html

Kill the mongrel with a ctrl-C and restart

mongrel_rails start -p 4000

Take a look in the browser

http://AcceleratorIP:4000/
http://AcceleratorIP:4000/hello/

Hello world - with mongrel jcluster

Create the configuration file

mongrel_rails jcluster::configure

Take a look

cat config/mongrel_jcluster.yml  
--- 
jport: 19222
port: 3000
pid_file: log/mongrel.pid
servers: 2

Now you fire them up like “normal”

mongrel_rails jcluster::start
** Ruby version is not up-to-date; loading cgi_multipart_eof_fix
Starting JRuby server...
Starting 2 Mongrel servers...

What is looks like in a process list

# prstat -Z
    PID USERNAME  SIZE   RSS STATE  PRI NICE      TIME  CPU PROCESS/NLWP       
    21826 root      241M  203M sleep   59    0   0:00:37 0.1% java/20

Now let’s take a database-driven application [Nick]

Mac

Modify database.yml (change password if necessary):

development:
  host: localhost
  adapter: jdbc
  driver: com.mysql.jdbc.Driver
  url: jdbc:mysql://localhost/railstest_development
  username: root
  password: root

test:
  host: localhost
  adapter: jdbc
  driver: com.mysql.jdbc.Driver
  url: jdbc:mysql://localhost/railstest_test
  username: root
  password: root

production:
  host: localhost
  adapter: jdbc
  driver: com.mysql.jdbc.Driver
  url: jdbc:mysql://localhost/railstest_production
  username: root
  password: root

Create the databases

mysql -u root -p
Password:
mysql> create database railstest_development
Query OK, 1 row affected (0.00 sec)

mysql> create database railstest_test
Query OK, 1 row affected (0.00 sec)

Scaffold a model:

jruby script/generate scaffold_resource post title:string created_on:date body:text published:boolean

Migrate your database

jruby -S rake db:migrate

Start Mongrel and visit http://localhost:4000/posts

mongrel_rails start -p 4000

Accelerator

Steps should be same as above

5:45pm - 6:15pm Dinner

Goals:

6:15pm - 6:30pm Panel: Advantages/disadvantages of a production JRuby environment

6:30pm Last working session: Deploying a JRuby Project into Glassfish on an Accelerator

Goals

Hello world in glassfish

Get glassfish

cd /a/1/app/
curl -O http://jxh.bingodisk.com/bingo/public/software/glassfish-installer-v2-b50g.jar
java -Xmx256m -jar glassfish-installer-v2-b50g.jar
rm glassfish-installer-v2-b50g.jar
cd glassfish/
chmod -R +x /a/1/app/glassfish/lib/ant/bin
/a/1/app/glassfish/lib/ant/bin/ant -f setup-cluster.xml
cp /a/1/languages/jruby/lib/mysql-connector-java-5.0.7-bin.jar /a/1/app/glassfish/lib/mysql-connector-java-5.0.7-bin.jar

Fire it up

/a/1/app/glassfish/bin/asadmin start-domain domain1

Take a look. Glassfish admin site is on port 4848 — default username/password is admin/adminadmin.

http://jason.joyent.com:8080/
http://jason.joyent.com:4848/

Let’s get “goldspike”

cd /home/railstest/
script/plugin install http://jruby-extras.rubyforge.org/svn/trunk/rails-integration/plugins/goldspike

Output will look like

A    /home/railstest/vendor/plugins/goldspike
A    /home/railstest/vendor/plugins/goldspike/test
A    /home/railstest/vendor/plugins/goldspike/test/war_config_test.rb
A    /home/railstest/vendor/plugins/goldspike/test/war_config_test_config.rb
A    /home/railstest/vendor/plugins/goldspike/test/java_library_test.rb
A    /home/railstest/vendor/plugins/goldspike/test/maven_library_test.rb
A    /home/railstest/vendor/plugins/goldspike/test/create_war_test.rb
A    /home/railstest/vendor/plugins/goldspike/Rakefile
A    /home/railstest/vendor/plugins/goldspike/init.rb
A    /home/railstest/vendor/plugins/goldspike/tasks
A    /home/railstest/vendor/plugins/goldspike/tasks/war.rake
A    /home/railstest/vendor/plugins/goldspike/lib
A    /home/railstest/vendor/plugins/goldspike/lib/new_java_library.rb
A    /home/railstest/vendor/plugins/goldspike/lib/war_config.rb
A    /home/railstest/vendor/plugins/goldspike/lib/run.rb
A    /home/railstest/vendor/plugins/goldspike/lib/java_library.rb
A    /home/railstest/vendor/plugins/goldspike/lib/packer.rb
A    /home/railstest/vendor/plugins/goldspike/lib/create_war.rb
A    /home/railstest/vendor/plugins/goldspike/install.rb
A    /home/railstest/vendor/plugins/goldspike/README
Exported revision 682.

Create the WAR

rake war:standalone:create
(in /home/jason)
Assembling web application
    Adding web application
    Adding Ruby gem ActiveRecord-JDBC version 0.4
    Creating web archive

It’s there

[jason:/home/jason] root# ls railstest.war
    railstest.war

Copy it into Glassfish’s autodeploy directory so that it can pick it up

cp railstest.war /a/1/app/glassfish/domains/domain1/autodeploy/railstest.war

Take a look in the browser

http://jason.joyent.com:8080/railstest/

Database-backed application in Glassfish (Nick)

Create the production database

mysql -u root
mysql> create database railstest_production
Query OK, 1 row affected (0.00 sec)

Migrate your production database

jruby -S rake db:migrate RAILS_ENV=production

Now modify database.yml to use a connection pool via JNDI

production:
  jndi: jdbc/railstest
  driver: com.mysql.jdbc.Driver

Create a Glassfish connection pool and resource and check that your connection pool is set up correctly (check the password)

/a/1/app/glassfish/bin/asadmin create-jdbc-connection-pool \
  --restype javax.sql.DataSource \
  --datasourceclassname com.mysql.jdbc.jdbc2.optional.MysqlConnectionPoolDataSource \
  --property User=root:Password=root:URL=jdbc\\:mysql\\://localhost/railstest_production \
  jdbc/railstestpool
/a/1/app/glassfish/bin/asadmin ping-connection-pool jdbc/railstestpool

Create the jdbc resource

/a/1/app/glassfish/bin/asadmin create-jdbc-resource \
  --connectionpoolid jdbc/railstestpool jdbc/railstest

Recreate the WAR

rake war:standalone:create
(in /home/jason)
Assembling web application
        Adding web application
        Adding Ruby gem ActiveRecord-JDBC version 0.4
    Creating web archive

Copy it into Glassfish’s autodeploy directory so that it can pick it up

cp railstest.war /a/1/app/glassfish/domains/domain1/autodeploy/railstest.war

Now go to http://AcceleratorIP:8080/railstest/posts

Notes

ActiveRecord-JDBC documentation for reference.

If you want to run tests with JRuby, you’ll need to fix test database bootstrapping to use migrations — it’s more friendly to JDBC.

script/plugin install http://svn.caldersphere.net/svn/main/plugins/migrate_testdb