GF Scripting Bloggers @ Sun

GlassFish v3 provides a REST interface to management and monitoring information as discussed in TOTD #96. As mentioned in that blog "the REST interface is a lower level API that enables toolkit developers and IT administrators to write their custom scripts/clients using language of their choice". This blog introduces a tool that uses the REST API to provide management and monitoring of GlassFish v3 and is written using JavaFX.

This tool is only a proof-of-concept that demonstrates that GlassFish v3 REST interface is functionally very rich and can indeed be used to write third-party administration tools. The tool uses a subset of the REST interface and exposes only a limited amount of management and monitoring capabilities otherwise exposed. After all this is a proof-of-concept :-)

A screencast of this tool in action along with a downloadable JNLP version will soon be available. For now, here is a snapshot of the main window of this tool:

The main screen allows you to enter a URL for the GlassFish administration. Then the GlassFish instance can be stopped/restarted from the main window using the buttons on top right. There is an animation at the bottom of the screen where the glassfish is swimming in the ocean and is directly related to the state of server running in the background. If the server is running, the animation works. If the server is not running then the animation stops as well.

The main screen has three main buttons:

  • "List Applications" - list all the applications deployed on the running instance
  • "Show Monitoring Levels" - show/Update all the monitoring levels
  • "Server Stats" - show statistics of the running server

Clicking on "List Applications" shows the list of applications deployed on this particular instance. Here is how a snapshot looks like for an instance running on my localhost at port 4848:

As shown in the screen, it shows a radio-bulleted list of all the applications. Each bullet is also accompanied by an image indicating the type of application - Web or Rails for now. Select the application and click on "Monitor" button to monitor that particular application. The REST API exposes a vast amount of monitoring data but a subset of monitoring data is displayed for Web and Rails application for now. Here is a snapshot of the monitoring data published for a Web application:

As evident by the list of engines, this web application has EJBs bundled as well. It also shows total number of Servlets/JSPs loaded, number of requests made to this web application and some other monitoring data.

Here is a snapshot of the monitoring data published for a Rails application:

It shows number of JRuby runtimes configured for the application, number of requests sent to the application, number of responses with different HTTP access codes and some other data.

The monitoring levels of different containers can be easily updated by clicking on "Show Monitoring Levels" as shown below:

And finally some server statistics are shown by clicking on "Server Stats" as shown below:

It shows when the server was started, host/port information, version and finally how long the server has been running for. The dials are an animation that shows the server up time.

Here are other related JavaFX and GlassFish related blogs published earlier:

How are you going to use the REST interface exposed by GlassFish v3 in your environment ?

Are you using JavaFX with GlassFish together in any way ?

Leave a comment on this blog if you do!

Technorati: javafx glassfish v3 rest web jruby rubyonrails rest administration monitoring management

I've always regarded working full time on Jython at Sun as a miraculous sabbatical that might come to an end at any moment. Sadly that time has come. I've worked with many amazing people and had a great time: Sun provided me with one of the greatest professional experiences I have had, for which I will always be grateful.

I'm very pleased with how far Jython has come during my tenure at Sun. Jython is now a modern version of Python, and has the momentum to continue its growth. A far larger group of developers than ever before contribute regularly, making Jython a very healthy project. Jython runs many more of the key frameworks and applications that are popular in the Python world. In the future we will be making Jython better, faster, and more complete. I started working on Jython long before I joined Sun, and I certainly plan be a part of Jython's future.

I am looking for a new opportunity, and am open to many possibilities. I have been working in software for more than twelve years, often in a lead role. I am well regarded in the Open Source world, where I have participated in and helped build communities. I have been the Jython project lead for almost five years. I am a committer on the Python project, and a member of the Python Software Foundation. I have done a wide variety of work in recent history: development and leadership work in Java and Python, from the abstract level of parsing Python source and compiling to Java bytecodes, to the more concrete work of web development. I am able to do work in any of these areas, on either a full time or consulting/contracting basis. My contact information can be found on my Google profile and my LinkedIn profile is a good summary of my credentials.
On behalf of the Jython development team, I'm pleased to announce that Jython 2.5.1 final is available for download. See the installation instructions.

Jython 2.5.1 fixes a number of bugs, including some major errors when using coroutines and when using relative imports, as well as a potential data loss bug when writing to files in append mode. Please see the NEWS file for detailed release notes.

Please report any bugs that you find. Thanks!
On behalf of the Jython development team, I'm pleased to announce that Jython 2.5.1rc3 is available for download. See the installation instructions.

I didn't really want to have an RC3, I was hoping to have a final by now, but a data loss bug was discovered in RC2 and that prompted one more RC. I'm hoping that this is the last one and that we'll have a final shortly. Please see the NEWS file for detailed release notes.

Please report any bugs that you find. Thanks!

Got the following message in my inbox today:

All details (including registration) are available here.

Looking forward to see you there!

Technorati: glassfish rubyonrails jruby webinar

If you where thinking about submitting a talk to PyCon 2010 you should do it now!
On behalf of the Jython development team, I'm pleased to announce that Jython 2.5.1rc2 is available for download. See the installation instructions.

Jython 2.5.1rc2 fixes bugs that we found when testing rc1, including some db, codec, and locking issues. Please see the NEWS file for detailed release notes.

Please report any bugs that you find. Thanks!
On behalf of the Jython development team, I'm pleased to announce that Jython 2.5.1rc1 is available for download. See the installation instructions.

Jython 2.5.1rc1 fixes a number of bugs, including some major errors when using
coroutines and when using relative imports. Please see the NEWS file for
detailed release notes.

Please report any bugs that you find. Thanks!
Sometime back I started a project - WOM, short for WSDL Object Model. The idea was to come up with a library that provides efficient parsing and provides a WSDL Object Model that can be used to inspect, traverse/navigate and will be useful for IDEs such as NetBeans and also WSDL 2 Java generators, such as Metro wsimport.

After some initial (90%) of work I left the project ideal to get back to it after I get past some other interesting things I was involved with. Recently someone from community sent me mail and asked whats up with this project and he plans to build some custome API using WOM and XSOM APIs for schema object model. I fixed some bugs, added documentations and released 0.9.0 version of WOM to maven.

The design of WOM is largely based on XSML Schema Object Model (XSOM). It is very extensible. For example, you can use your own ContentHandler to parse the XML Schema. Use your own extension handlers to parse WSDL extensibility elements. I need to do more work on adding documentation for these. But what I have there should be sufficient for you to start playing with it.
WOMParser parser = new WOMParser();
WSDLSet wsdls = parser.parse(new File("sample.wsdl"));
WSDLDefinitions def = wsdls.getWSDLs().next();
Refer to API doc on how to navigate and do some interesting things with the WSDL components.

See the project page for details and send your feedback.
The invokedynamic work from the Da Vinci Machine Project has come a long way. In fact, it has started to make it's way into OpenJDK 7 builds. To try the bleeding edge version out for yourself, follow these instructions to patch and build your own. But, be warned, it's a bit tricky to get started. You may find that these examples will work with OpenJDK 7 binaries (sooner or later they should!). For those who haven't heard about this work yet, invokedynamic, which is a part of JSR-292, allows a program to dynamically call methods with (potentially) the same performance characteristics as static calls. Here is about the simplest invokedynamic program you can write:


import java.dyn.CallSite;
import java.dyn.InvokeDynamic;
import java.dyn.Linkage;
import java.dyn.MethodType;
import java.dyn.MethodHandles;

public class Example {
public static void main(String... av) throws Throwable {
for (String a : av) {
InvokeDynamic.call(a);
}
InvokeDynamic.call2(av[0]);
}

public static void foo(String x) {
System.out.println("Hello, " + x);
}

static { Linkage.registerBootstrapMethod("linkDynamic"); }
private static CallSite linkDynamic(Class caller, String name, MethodType type) {
System.out.println("linking:" + name);
CallSite c = new CallSite(caller, name, type);
c.setTarget(MethodHandles.lookup().findStatic(Example1.class, "foo",
MethodType.make(void.class, String.class)));
return c;
}
}


First notice the linkDynamic method. The static initializer above it registers that method as a special bootstrap method that invokedynamic will use when you want to call a method dynamically. The interesting bit to notice are the two calls to methods on InvokeDynamic. InvokeDynamic has no actual methods of its own. You can call any method on it (here I use "call" and "call2", but it could just as well have been "foo123" or "sasquatch") When you make these calls, control is passed to the bootstrap method (linkDynamic in this case). linkDynamic gets passed the Class of the caller, the name that was used ("call" and "call2" in this program) and the MethodType of the call, which specifieds the signature of the dynamic call.


To compile the above code, execute:


javac -XDinvokedynamic Example.java


To run it:

java -XX:+EnableMethodHandles -XX:+EnableInvokeDynamic Example a b c


Which should produce:

linking:call
Hello, a
Hello, b
Hello, c
linking:call2
Hello, a

Notice that we called "call" three times, but it only needed to be linked once. We then called call2 and it was separately linked. Had we invoked call2 again it would not have needed linking. Next in my exploration, I'll wire it up with some Python internal dispatch logic:

package org.python.compiler;

import java.dyn.CallSite;
import java.dyn.InvokeDynamic;
import java.dyn.Linkage;
import java.dyn.MethodType;
import java.dyn.MethodHandle;
import java.dyn.MethodHandles;
import java.dyn.MethodType;

import org.python.core.Py;
import org.python.core.PyCode;
import org.python.core.PyFunction;
import org.python.core.PyInteger;
import org.python.core.PyObject;
import org.python.core.PyString;
import org.python.core.PyTuple;
import org.python.core.ThreadState;

public class IndyTest {

public static void run() throws Throwable {
PyObject result = InvokeDynamic.foo(new PyInteger(4), Py.None,
Py.EmptyObjects, new PyTuple());
System.out.println("result = " + result);
}

static { Linkage.registerBootstrapMethod("linkDynamic"); }
private static CallSite linkDynamic(Class caller, String name, MethodType type) {
System.out.println("linkDynamic...");
CallSite site = new CallSite(caller, name, type);
ThreadState ts = Py.getThreadState();
PyCode func_code = ((PyFunction)ts.frame.getname(name)).func_code;

MethodType oneArgCall = MethodType.make(
PyObject.class, //return type
ThreadState.class, // state
PyObject.class, // arg1
PyObject.class, // globals
PyObject[].class, // defaults
PyObject.class // closure
);
MethodHandle call = MethodHandles.lookup().findVirtual(PyCode.class, "call", oneArgCall);
call = MethodHandles.insertArguments(call, 0, func_code, ts);
call = MethodHandles.convertArguments(call, type);
site.setTarget(call);

return site;
}
}



There is a bit more going on here. In short, we look up the current state of the Jython interpreter when this is invoked and look for a function called "foo". We pass the PyInteger 4 (plus some boilerplate at the end -- in the future we should be able to only pass the 4, but MethodHandles.insertArguments can only add three arguments at the moment.) to the foo(), and then print the result out. Again "foo" is bound the first time it is called. When we create a CallSite, grab the current ThreadState and get our "foo" object from the current Jython frame. We then create a MethodType that corresponds to the way that we call Jython methods internally, and use methods off of MethodHandles (insertArguments and convertArguments) to coerce our call to the internal call semantics. Then our "foo" call is permanently bound to the function we found. Ultimately this is not what we will want since "foo" could be rebound in Jython in a number of ways. We'll get to this problem in a future post. To try this code out, we need a Jython program with a foo method:


def foo(x):
print "hello %s!" % x
foo("test")

from org.python.compiler import IndyTest
IndyTest.run()

def foo(x):
print "goodbye %s!" % x
foo("test")

IndyTest.run()


Run it like this (requires a Jython 2.5 install and we need to call Jython with --boot to put the Jython internals into the boot classpath):

jython --boot -J-XX:+EnableMethodHandles -J-XX:+EnableInvokeDynamic indy.py



hello test!
foo = 1
linkDynamic...
hello 4!
result = None
goodbye test!
foo = 2
hello 4!
result = None


The first IndyTest.run() looks great (hello 4!) -- but the second call still emits "hello 4!" even though foo() has been redefined. We'll look at fixing this in the next post.

By the way, to *really* learn about the guts of invokedynamic look to the posts from John Rose and Fredrik Öhrström along with the Da Vinci Machine site.

The GlassFish High Availability allows to setup a cluster of GlassFish instances and achieve highly scalable architecture using in-memory session state replication. This cluster can be very easily created and tested using the "clusterjsp" sample bundled with GlassFish. Here are some clustering related entries published on this blog so far:
  • TOTD #84 shows how to setup Apache + mod_proxy balancer for Ruby-on-Rails load balancing
  • TOTD #81 shows how to use nginx to front end a cluster of GlassFish Gems
  • TOTD #69 explains how a GlassFish cluster can be front-ended using Sun Web Server and Load Balancer Plugin
  • TOTD #67 shows the same thing using Apache httpd + mod_jk
#67 & #69 uses a web application "clusterjsp" (bundled with GlassFish) that uses JSP to demonstrate in-memory session replication state replication. This blog creates a similar application "clusterrails" - this time using Ruby-on-Rails and deploy it on GlassFish v2.1.1. The idea is to demonstrate how Rails applications can leverage the in-memory session replication feature of GlassFish.

Rails applications can be easily deployed as a WAR file on GlassFish v2 as explained in TOTD #73. This blog will guide through the steps of creating the Controller and View to mimic "clusterjsp" and configuring the Rails application for session replication.
  1. Create a template Rails application and create/migrate the database. Add a Controller/View as:

    ~/samples/jruby/session >~/tools/jruby/bin/jruby script/generate controller home index
    JRuby limited openssl loaded. gem install jruby-openssl for full support.
    http://wiki.jruby.org/wiki/JRuby_Builtin_OpenSSL
          exists  app/controllers/
          exists  app/helpers/
          create  app/views/home
          exists  test/functional/
          create  test/unit/helpers/
          create  app/controllers/home_controller.rb
          create  test/functional/home_controller_test.rb
          create  app/helpers/home_helper.rb
          create  test/unit/helpers/home_helper_test.rb
          create  app/views/home/index.html.erb

  2. Edit the controller in "app/controllers/home_controller.rb" and change the code to (explained below):

    class HomeController < ApplicationController
      include Java

      def index
        @server_served = servlet_request.get_server_name
        @port = servlet_request.get_server_port
        @instance = java.lang.System.get_property "com.sun.aas.instanceName"
        @server_executed = java.net.InetAddress.get_local_host().get_host_name()
        @ip = java.net.InetAddress.get_local_host().get_host_address
        @session_id = servlet_request.session.get_id
        @session_created = servlet_request.session.get_creation_time
        @session_last_accessed = servlet_request.session.get_last_accessed_time
        @session_inactive = servlet_request.session.get_max_inactive_interval

        if (params[:name] != nil)
          servlet_request.session[params[:name]] = params[:value]
        end

        @session_values = ""
        value_names = servlet_request.session.get_attribute_names
        unless (value_names.has_more_elements)
          @session_values = "<br>No parameter entered for this request"
        else
            @session_values << "<UL>"
            while (value_names.has_more_elements)
                param = value_names.next_element
                unless (param.starts_with?("__"))
                  value = servlet_request.session.get_attribute(param)
                  @session_values << "<LI>" + param + " = " + value + "</LI>"
                end
            end
            @session_values << "</UL>"
        end

      end

      def adddata
        servlet_request.session.set_attribute(params[:name], params[:value])
        render :action => "index"
      end

      def cleardata
        servlet_request.session.invalidate
        render :action => "index"
      end
    end

    The "index" action initializes some instance variables using the "servlet_request" variable mapped from "javax.servlet.http.ServletRequest" class. The "servlet_request" provides access to different properties of the request received such as server name/port, host name/address and others. It also uses an application server specific property "com.sun.aas.instanceName" to fetch the name of particular instance serving the request. In this blog we'll create a cluster with 2 instances. The action then prints the servlet session attributes name/value pairs entered so far.

    The "adddata" action takes the name/value pair entered on the page and stores them in the servlet request. The "cleardata" action clears any data that is storied in the session.
  3. Edit the view in "app/views/home/index.html.erb" and change to (explained below):

    <h1>Home#index</h1>
    <p>Find me in app/views/home/index.html.erb</p>
    <B>HttpSession Information:</B>
    <UL>
    <LI>Served From Server:   <b><%= @server_served %></b></LI>
    <LI>Server Port Number:   <b><%= @port %></b></LI>
    <LI>Executed From Server: <b><%= @server_executed %></b></LI>
    <LI>Served From Server instance: <b><%= @instance %></b></LI>
    <LI>Executed Server IP Address: <b><%= @ip %></b></LI>
    <LI>Session ID:    <b><%= @session_id %></b></LI>
    <LI>Session Created:  <%= @session_created %></LI>
    <LI>Last Accessed:    <%= @session_last_accessed %></LI>
    <LI>Session will go inactive in  <b><%= @session_inactive %> seconds</b></LI>
    </UL>
    <BR>
    <% form_tag "/session/home/index" do %>
      <label for="name">Name of Session Attribute:</label>
      <%= text_field_tag :name, params[:name] %><br>

      <label for="value">Value of Session Attribute:</label>
      <%= text_field_tag :value, params[:value] %><br>

        <%= submit_tag "Add Session Data" %>
    <% end  %>
    <% form_tag "/session/home/cleardata" do %>
        <%= submit_tag "Clear Session Data" %>
    <% end %>
    <% form_tag "/session/home/index" do %>
        <%= submit_tag "Reload Page" %>
    <% end %>
    <BR>
    <B>Data retrieved from the HttpSession: </B>
    <%= @session_values %>

    The view dumps the property value retrieved from the servlet context in the action. Then it consists of some forms to enter the session name/value pairs, clear the session and reload the page. The application is now ready, lets configure it for WAR packaging.
  4. Generate a template "web.xml" and copy it to "config" directory as:

    ~/samples/jruby/session >~/tools/jruby/bin/jruby -S warble war:webxml
    mkdir -p tmp/war/WEB-INF
    ~/samples/jruby/session >cp tmp/war/WEB-INF/web.xml config/
    1. Edit "tmp/war/WEB-INF/web.xml" and change the first few lines from:

      <!DOCTYPE web-app PUBLIC
        "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
        "http://java.sun.com/dtd/web-app_2_3.dtd">
      <web-app>

      to

      <web-app version="2.4" xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">

      This is required because the element to be added next is introduced in the Servlet 2.4 specification.
    2. Add the following element:

      <distributable/>

      as the first element, right after "<web-app>". This element marks the web application to be distributable across multiple JVMs in a cluster.
  5. Generate and configure "warble/config.rb" as described in TOTD #87. This configuration is an important step otherwise you'll encounter JRUBY-3789. Create a WAR file as:

    ~/samples/jruby/session >~/tools/jruby/bin/jruby -S warble
    mkdir -p tmp/war/WEB-INF/gems/specifications
    cp /Users/arungupta/tools/jruby-1.3.0/lib/ruby/gems/1.8/specifications/rails-2.3.2.gemspec tmp/war/WEB-INF/gems/specifications/rails-2.3.2.gemspec

    . . .

    mkdir -p tmp/war/WEB-INF
    cp config/web.xml tmp/war/WEB-INF
    jar cf session.war  -C tmp/war .

  6. Download latest GlassFish v2.1.1, install/configure GlassFish and create/configure/start a cluster using the script described here. Make sure to change the download location and filename in the script. This script creates a cluster "wines" with two instances - "cabernet" runing on the port 58080 and "merlot" running on the port 58081.
  7. Deploy the application using the command:

    ~/samples/jruby/session >asadmin deploy --target wines --port 5048 --availabilityenabled=true session.war
Now, the screenshots from the two instances are shown and explained below. The two (or more) instances are front-ended by a load balancer so none of this is typically visible to the user but it helps to understand.
Here is a snapshot of this application deployed on "cabernet":



The instance name and the session id is highlighted in the red box. It also shows the time when the session was created in "Session Created" field.

And now the same application form "merlot":



Notice, the session id exactly matches the one from the "cabernet" instance. Similarly "Session Created" matches but "Last Accessed" does not because the same session session is accessed from a different instance.

Lets enter some session data in the "cabernet" instance and click on "Add Session Data" button as shown below:



The session attribute is "aaa" and value is "111". Also the "Last Accessed" time is updated. In the "merlot" page, click on the "Reload Page" button and the same session name/value pairs are retrieved as shown below:



Notice, the "Last Accessed" time is after the time showed in "cabernet" instance. The session information added in "cabernet" is automatically replicated to the "merlot" instance.

Now, lets add a new session name/value pair in "merlot" instance as shown below:



The "Last Accessed" is updated and the session name/value pair ("bbb"/"222") is shown in the page. Click on "Reload page" in "cabernet" instance as shown below:



This time the session information added to "merlot" is replicated to "cabernet".

So any session information added in "cabernet" is replicated to "merlot" and vice versa.

Now, lets stop "cabernet" instance as shown below:



and click on "Reload Page" in "merlot" instance to see the following:



Even though one instance from which the session data was added is stopped, the replicating instance continues to serve both the session values.

As explained earlier, these two instances are front-ended by a load-balancer typically running at port 80. So the user makes a request to port 80 and the correct session values are served even if one of the instance goes down and there by providing in-memory session replication.

Please leave suggestions on other TOTD that you'd like to see. A complete archive of all the tips is available here.

Technorati: totd glassfish clustering rubyonrails jruby highavailability loadbalancer

This Tip Of The Day (TOTD) explains how to add pagination to your Rails application.
  1. Create a simple Rails scaffold as:

    ~/samples/jruby >~/tools/jruby/bin/jruby -S rails paginate
    ~/samples/jruby/paginate >~/tools/jruby/bin/jruby script/generate scaffold book title:string author:string
    ~/samples/jruby/paginate >sed s/'adapter: sqlite3'/'adapter: jdbcsqlite3'/ <config/database.yml >config/database.yml.new
    ~/samples/jruby/paginate >mv config/database.yml.new config/database.yml
    ~/samples/jruby/paginate >~/tools/jruby/bin/jruby -S rake db:migrate

  2. Edit "test/fixtures/books.yml" and specify the content as:

    # Read about fixtures at http://ar.rubyonrails.org/classes/Fixtures.html

    one:
      title: Ultramarathon Man Confessions of an All-Night Runner
      author: Dean Karnazes

    two:
      title: My Life on the Run
      author: Bart Yasso

    three:
      title: 50/50 Secrets I Learned Running 50 Marathons in 50 Days
      author: Dean Karnazes

    four:
      title: Born to Run
      author: Christopher Mcdougall

    five:
      title: Four Months to a Four-hour Marathon
      author: Dave Kuehls

    six:
      title:  Galloway's Book on Running
      author: Jeff Galloway

    seven:
      title: Marathoning for Mortals
      author: John Bingham and Jenny Hadfield

    eight:
      title:  Marathon You Can Do It!
      author: Jeff Galloway

    nine:
      title: Marathon The Ultimate Training Guide
      author: Hal Higdon

    ten:
      title: Running for Mortals
      author: John Bingham and Jenny Hadfield

    and load the fixtures as:

    ~/samples/jruby/paginate >~/tools/jruby/bin/jruby -S rake db:fixtures:load
    (in /Users/arungupta/samples/jruby/paginate)

  3. Run the application as:

    ~/samples/jruby/paginate >~/tools/jruby/bin/jruby -S glassfish -l
    Starting GlassFish server at: 129.145.132.8:3000 in development environment...
    Writing log messages to: /Users/arungupta/samples/jruby/paginate/log/development.log.

    . . .

    Jul 29, 2009 2:06:44 PM com.sun.grizzly.scripting.pool.DynamicPool$1 run
    INFO: New instance created in 7,488 milliseconds

    The application is accessible at "http://localhost:3000/books" and looks like:



    The page shows 10 rows, all in one page.
  4. Lets add pagination to this simple sample.
    1. Install will_paginate gem as:

      /tools/jruby >./bin/jruby -S gem install will_paginate
      JRuby limited openssl loaded. gem install jruby-openssl for full support.
      http://wiki.jruby.org/wiki/JRuby_Builtin_OpenSSL
      Successfully installed will_paginate-2.2.2
      1 gem installed
      Installing ri documentation for will_paginate-2.2.2...
      Installing RDoc documentation for will_paginate-2.2.2...

      There are other methods of installation as well.
    2. Edit "config/environment.rb" and add

      require "will_paginate"

      as the last line.
    3. Edit the "index" action in "app/controllers/books_controller.rb" as:

      @books = Book.paginate(:page => params[:page], :per_page => 5)
      #@books = Book.all

      ":per_page" specifies the number of items to be displayed in each page.
    4. In "app/views/books/index.html.erb", add:

      <%= will_paginate @books %>

      right after "</table>".

      The output now looks like:



      and clicking on "Next" shows:



      The information is nicely split amongst 2 pages.
An important point to remember is that will_paginate only adds pagination to your Rails app. You are still required to display all the values.

But essentially replacing "@books = Book.all" with "@books = Book.paginate(:page => params[:page], :per_page => 5)" in the Controller and adding
"<%= will_paginate @books %>" did the trick for us.

Clean and simple!

Please leave suggestions on other TOTD (Tip Of The Day) that you'd like to see. A complete archive of all the tips is available here.

Technorati: jruby rubyonrails glassfish pagination will_paginate

If you are using Warbler to create a WAR file of your application and deploying on GlassFish or any other Servlet container, then you are likely seeing the following error during deployment:

[#|2009-07-30T15:29:50.788-0700|SEVERE|sun-appserver2.1|javax.enterprise.system.container.web|_ThreadID=17;
_ThreadName=httpWorkerThread-4848-0;_RequestID=1d7e8f18-1c9a-4924-bd0b-6a07eba425ba;|WebModule
[/session]unable to create shared application instance
org.jruby.rack.RackInitializationException: undefined method `new' for "Rack::Lock":String
        from /Users/arungupta/tools/glassfish/v2.1/glassfish/domains/domain1/applications/j2ee-modules/session/WEB-INF/gems/gems/actionpack-2.3.2/lib/
action_controller/middleware_stack.rb:116:in `inject'
        from /Users/arungupta/tools/glassfish/v2.1/glassfish/domains/domain1/applications/j2ee-modules/session/WEB-INF/gems/gems/actionpack-2.3.2/lib/
action_controller/middleware_stack.rb:116:in `build'
        from /Users/arungupta/tools/glassfish/v2.1/glassfish/domains/domain1/applications/j2ee-modules/session/WEB-INF/gems/gems/actionpack-2.3.2/lib/
action_controller/dispatcher.rb:82:in `initialize'

. . .

This is a known issue as reported at JRUBY-3789 and JRUBY_RACK-18.

As the bug report indicates, this is actually an issue with jruby-rack-0.9.4 and is fixed in jruby-rack-0.9.5. The 3-step workaround is described here and explained below for convenience:
  1. Do "warble war:clean" to clean up the .war file and staging area. This basically removes previous version of jruby-rack.jar.
  2. Download the latest jruby-rack-0.9.5 snapshot (complete list) and copy in the "lib" directory of your application.
  3. If "config/warble.rb" does not exist then generate it using "jruby -S config warble". Edit "config/warble.rb" such that it looks like:

      # Additional Java .jar files to include. Note that if .jar files are placed
      # in lib (and not otherwise excluded) then they need not be mentioned here.
      # JRuby and JRuby-Rack are pre-loaded in this list. Be sure to include your
      # own versions if you directly set the value
      # config.java_libs += FileList["lib/java/*.jar"]
      config.java_libs.delete_if {|f| f =~ /jruby-rack/ }
      config.java_libs += FileList["lib/jruby-rack*.jar"]

    This will pack jruby-rack-0.9.5 snapshot instead of the one bundled with Warbler.

    Now warbler 1.0.0 bundles "jruby-complete-1.3.0RC1.jar". Optionally, you can also download the latest jruby-complete (jruby-complete-1.3.1.jar as of this writing) and copy in the "lib" directory of your application. In that case, modify the above fragment to:

      # Additional Java .jar files to include. Note that if .jar files are placed
      # in lib (and not otherwise excluded) then they need not be mentioned here.
      # JRuby and JRuby-Rack are pre-loaded in this list. Be sure to include your
      # own versions if you directly set the value
      # config.java_libs += FileList["lib/java/*.jar"]
      config.java_libs.delete_if {|f| f =~ /jruby-rack/ || f =~ /jruby-complete/ }
      config.java_libs += FileList["lib/jruby-complete*.jar"]
      config.java_libs += FileList["lib/jruby-rack*.jar"]

    This packs the "jruby-complete-1.3.1.jar" in your .war file.
And now follow your regular procedure of creating the .war file using "jruby -S warble" and happily deploy your Rails/Sintara/Merb applications on GlassFish.

There are several users who are already using Rails on GlassFish in production environment and they are listed at rubyonrails+glassfish+stories. Drop a comment on this blog if you are using it too :)

Technorati: jruby rack glassfish war servlet rubyonrails

This blog introduces a new application that will provide basic tracking of your running distance and generate charts to monitor progress. There are numerous similar applications that are already available/hosted and this is a very basic application. What's different about this ?

The first version of this application is built using JRuby, Ruby-on-Rails, GlassFish Gem, MySQL, and NetBeans IDE. This combination of technologies is a high quality Rails stack that is used in production deploymnet at various places. Still nothing different ?

A similar version of this application will be built using a variety of Web frameworks such as Java EEGrails, Wicket, Spring and Struts2 (in no particular order). The goal is to provide a similar application, slightly bigger than "Hello World," built using different frameworks and deploy on GlassFish. Each framework will then be evaluated based upon the criteria ranging from the basic principles of framework, ease-of-use in design/development/testing/debugging/production of this web app, database interaction, tools support, ability to add 3rd party libraries, browser compatibility and other points. 

An important point to note is that this is not an exhaustive evaluation of different Web frameworks and the scope is limited only to this application.

A complete list of frameworks planned is available here. The criteria used to evaluate each framework is described here. Your feedback in terms of Web frameworks and evaluation criteria is highly appreciated.  Please share your feedback on the users list.

Now the first version of application. The complete instructions to check out and run the Rails version of this application are available here.

Here are some charts generated using the application:



and



YUI is used for all the charting capabilities.

And here is a short video that explains how the application work:



If you are a runner, check out the application and use it for tracking your miles. A sample runlog is available in "test/fixtures/runlogs.yml" and races in "test/fixtures/races.yml".

If you know Rails, please provide feedback if the application is DRY and using the right set of helpers.

If you'd like the existing list of web frameworks to be pruned or include another one to the list, let us know.

Share you feedback at users@runner.kenai.com.

Technorati: jruby rubyonrails glassfish netbeans mysql yahoo yui chart running miles framework
I am moving my blog to http://blog.enebo.com/

I should even update it more often that this one...

Here are some quotes about running Rails applications on GlassFish from user@jruby mailing list:

I find the glassfish gem to be the most performant of all -- and I don't need to war-up my app.

I also have some mongrel cluster stuff, but glassfish is simpler and just works.

Voila...blazing speed, can handle lots of traffic. Note that I am also cominging into apache from a dyndns name. So, whatever IP I have, I can go straight to execution on the glassfish gem and NO warring up! What could be easier deployment, or a faster execution?

It's running fantasticly and performing like nothing I've seen before :) Completely stable memory, no wirings or anything bad for 5 days now.. (with several ab/htperf stresstests).

It's always exciting to get good endorsements of our efforts in the GlassFish team :)

Other similar stories for using Rails/GlassFish in production are described at rubyonrails+stories.

Technorati: glassfish v3 gem rubyonrails stories jruby

TOTD #81 explained how to install/configure nginx for load-balancing/front-ending a cluster of Rails application running on GlassFish Gem. Another popular approach in the Rails community is to use Apache HTTPDmod_proxy_balancer. A user asked the exact details of this setup on the GlassFish Gem Forum. This Tip Of The Day (TOTD) will clearly explain the steps.
  1. Create a simple Rails scaffold and run this application using GlassFish Gem on 3 separate ports as explained in TOTD #81.
  2. Setup and configure HTTPD and mod_proxy_balancer
    1. Setup and install Apache HTTPD as explained here. I believe mod_proxy_balancer and other related modules comes pre-bundled with HTTPD, at least that's what I observed with Mac OS X 10.5.7. Make sure that the "mod_proxy_balancer" module is enabled by verifying the following line is uncommented in "/etc/apache2/httpd.conf":

      LoadModule proxy_balancer_module libexec/apache2/mod_proxy_balancer.so

      Please note another similar file exists in "/etc/httpd/httpd.conf" but ignore that one.
    2. Setup a mod_proxy_balancer cluster by adding the following fragment in "httpd.conf" as:

      <Proxy balancer://glassfishgem>
      BalancerMember http://localhost:3000
      BalancerMember http://localhost:3001
      BalancerMember http://localhost:3002
      </Proxy>

      The port numbers must exactly match with those used in the first step.
    3. Specify the ProxyPass directives to map the cluster to a local path as:

      ProxyPass / balancer://glassfishgem/
      CustomLog /var/log/glassfishgem.log/apache_access_log combined

      The "/" at the end of "balancer://glassfishgem" is very important to ensure that all the files are resolved correctly.
    4. Optionally, the following directive can be added to view the access log:

      CustomLog /var/log/glassfishgem.log/apache_access_log combined

      Make sure to create the directory specified in "CustomLog" directive.
  3. Now the application is accessible at "http://localhost/runlogs". If a new GlassFish instance is started then update the <Proxy> directive and restart your HTTPD as "sudo httpd -k restart". Dynamic update of BalancerMembers can be configured as explained here.
TOTD #81 started the Rails application in root context. You can alternatively start the application in a non-root context as:

~/tools/jruby/rails/runner >../../bin/jruby -S glassfish -e production -c myapp
Starting GlassFish server at: 10.0.177.178:3000 in production environment...
Writing log messages to: /Users/arungupta/tools/jruby-1.3.0/rails/runner/log/production.log.
Press Ctrl+C to stop.
. . .
~/tools/jruby/rails/runner >../../bin/jruby -S glassfish -e production -c myapp -p 3001
Starting GlassFish server at: 10.0.177.178:3001 in production environment...
Writing log messages to: /Users/arungupta/tools/jruby-1.3.0/rails/runner/log/production.log.
Press Ctrl+C to stop.
. . .
~/tools/jruby/rails/runner >../../bin/jruby -S glassfish -e production -c myapp -p 3002
Starting GlassFish server at: 10.0.177.178:3002 in production environment...
Writing log messages to: /Users/arungupta/tools/jruby-1.3.0/rails/runner/log/production.log.
Press Ctrl+C to stop.

and then the ProxyPass directive will change to:

ProxyPass /myapp/ balancer://glassfishgem/myapp/

The changes are highlighted in bold. And the application is now accessible at "http://localhost/myapp/runlogs".

After discussing on Apache HTTP Server forum, the BalancerMember host/port can be printd in the log file using a custom log format. So add the following log format to "/etc/apache2/httpd.conf":

LogFormat "%h %l %u %t \"%r\" %>s %b \"%{Referer}i\" \"%{User-agent}i\" \"%{BALANCER_WORKER_NAME}e\"" custom

And change the format from the default "combined" to the newly defined "custom" format as:

CustomLog /var/log/glassfishgem.com/apache_access_log custom

Three subsequent invocations of "http://localhost/runlogs" then prints the following log entries:

::1 - - [17/Jun/2009:10:53:53 -0700] "GET /runlogs HTTP/1.1" 304 - "-" "Mozilla/5.0 (Macintosh; U; Intel Mac OS X 10.5; en-US; rv:1.9.0.11) Gecko/2009060214 Firefox/3.0.11" "http://localhost:3002"
::1 - - [17/Jun/2009:10:54:04 -0700] "GET /runlogs HTTP/1.1" 200 621 "-" "Mozilla/5.0 (Macintosh; U; Intel Mac OS X 10.5; en-US; rv:1.9.0.11) Gecko/2009060214 Firefox/3.0.11" "http://localhost:3000"
::1 - - [17/Jun/2009:10:54:05 -0700] "GET /runlogs HTTP/1.1" 304 - "-" "Mozilla/5.0 (Macintosh; U; Intel Mac OS X 10.5; en-US; rv:1.9.0.11) Gecko/2009060214 Firefox/3.0.11" "http://localhost:3001"

As evident from the last fragment of each log line, the load is distributed amongst three GlassFish Gem instances. More details on load balancer algorithm are available here.

Feel free to drop a comment on this blog if you are using GlassFish in production for your Rails applications. Several stories are already available at rubyonrails+glassfish+stories.

Technorati: glassfish rubyonrails apache httpd mod_proxy_balancer loadbalancing clustering
On behalf of the Jython development team, I'm pleased to announce that Jython 2.5.0 final is available for download. See the installation instructions.

Jython 2.5.0 brings us up to language level compatibility with the 2.5 version of CPython. This release has had a strong focus on CPython compatibility, and so this release of Jython can run more pure Python apps then any previous release. Please see the NEWS file for detailed release notes.

I want to thank all of the amazing people who have contributed in large and small ways to this important Jython release. For the first time in a long time we have Jython that is a modern version of Python. Enjoy!

Please report any bugs that you find. Thanks!

OPIN Systems has chosen JRuby, Rails, and GlassFish for a customer-facing financial application. Why ? "Easy to setup Rails application and add more intense logic in JRuby calls"

Learn more about it in this video:



Thanks to Ben Leadholm for the quick story! Check out other GlassFish Production Stories.

Check out Ben's "Dude, Where's my pass ?" entry:


Read several other Rails/GlassFish success stories.

Technorati: conf javaone sanfrancisco glassfish rubyonrails stories
On behalf of the Jython development team, I'm pleased to announce that Jython 2.5rc4 is available for download. See the installation instructions.

This is the fourth and probably final release candidate of the 2.5.0 version of Jython. If no major bugs are found in rc4, we will relabel it as 2.5.0 final. With this version, all of the regression that we follow on Windows now pass.

Please try this out and report any bugs that you find.
GlassFish v3 Preview is available and I am excited to announce support for Django applications. Django is a Python based framework, probably anyone hosting their applications on GAE might know Django is a Python based web framework. GlassFish-Scripting project provides implementation of Jython Container. Just like JRuby Container, Jython Container does the following:
  1. Provides GlassFish v3 connector and deployer as OSGi module. Which means that deployment of a Python application will trigger Jython Container code. 
  2. Wire up the HTTP request and response at very low level by implementing a GrizzlyAdapter, hence resulting in better runtime performance and scalability using grizzly scalable NIO framework.
  3. WSGI (Web Services Gateway Interface) is a Python standard to wire a Web Server to Python web frameworks such as Django or  TurboGears etc. Jython Container implements WSGI interface and so it would be pretty easy to add support for various Python web frameworks. Currently, we have Django and we will have others such as TuroboGears, Pylons etc.
  4. Currently Jython Container is available thru GlassFish v3 Update Tool. In the future it may appear with GlassFish v3 core distribution.

Here is how you can run a Jython application on GlassFish v3 Preview.

Install GlassFish v3 Preview

Follow the directions from here to install GlassFish v3 Preview.

Install Jython Container on GlassFish v3 Preview

$ cd /tools/glassfishv3
$ ./bin/updatetool

Below is the GlassFish v3 Update Tool image. Select GlassFish v3 Jython Container and click Install. This would install Jython Container OSGi module and Grizzly adapter jars inside glassfish/modules directory.

jython.png

Install Jython

To run your Django application, at the minimum you need Jython 2.5 release. Get Jython 2.5 RC3 from here. Once downloaded, run the installer by doing the following:

$ java -jar jython_installer-2.5rc3.jar

Follow the installer direction and after it is installed, add jython command to your path

$ export JYTHON_HOME=/path/to/jython-install
$ export PATH=$JYTHON_HOME/bin:$PATH
$ jython

should display jython interpreter shell

jython-cli.png

Install Django

Get Django 1.0.2 from here.

$ tar xzvf Django-1.0.2-final.tar.gz
$ cd Django-1.0.2-final
$ jython setup.py install

Follow the detailed instruction here.

Create a sample Django application

$ django-admin.py startproject mysite
$ cd mysite

django-ls.png

Start GlassFish v3 Preview

$ asadmin start-domain -v
$ asadmin create-jvm-options -Djython.home=/tools/jython

Above /tools/jython is where I installed jython. jython.home system property tells Jython container in GlassFish v3, where to find jython container.

Deploy the application

$ cd mysite
$ asadmin deploy .

Now access http://localhost:8080/mysite and it should show the first Django page:

django-app.png

Enable Django admin

Django framework comes with a built in administration utility and we will enable it in this sample. The admin application needs a database. So first we would need to setup the database.

Install PostgreSQL

Download and install PostgreSQL database server. Make sure it is running.

Once installed, make sure it is running. You would also need to download and install PostgreSQL JDBC driver inside jython installation.

$ cd /tools/jython
$ wget http://jdbc.postgresql.org/download/postgresql-8.3-604.jdbc4.jar

Above /tools/jython is where I have installed jython.

Install django-jython

You would need JDBC drivers for the database backends. So you would need to install django-jython on your Jython installation. django-jython is a Google Code project that has JDBC database backend for PostgreSQL only. There is SQLite3 experimantal version and no MySQL as of now.

Download the latest django-jython from here.

Then install it on Jython.

$ tar zxvf django-jython-1.0b1.tar.gz
$ cd django-jython-1.0b1
$ jython setup.py install

See further details on setting up backend here.

Create database

$ createdb mysite

Refer to details on how to go about creating database in PostgreSQL incase you encounter problem. We will use mysite database in our Django application.

Configure Database

Edit settings.py and change the database configuration. For example these are my entries:

DATABASE_ENGINE = 'doj.backends.zxjdbc.postgresql'
DATABASE_NAME = 'mysite'             # Or path to database file if using sqlite3.
DATABASE_USER = 'vivek'             # Not used with sqlite3.
DATABASE_PASSWORD = 'vivek'         # Not used with sqlite3.
DATABASE_HOST = ''             # Set to empty string for localhost. Not used with sqlite3.
DATABASE_PORT = ''             # Set to empty string for default. Not used with sqlite3.

Above, doj.backends.zxjdbc.postgresql is the name of the PostgreSQL JDBC driver, the database name is mysite and then the username and password. HOST and PORT values are empty meaning the default will be taken.

It is time to sync the DB. So first make the PostgreSQL JDBC driver available to jython classpath so that the following syncdb command works:

$ export CLASSPATH=/tools/jython/postgresql-8.3-604.jdbc4.jar:$CLASSPATH
$ jython manage.py syncdb

NOTE: postgresql-8.3-604.jdbc4.jar was downloaded and installed inside jython directory installation earlier.

Enable admin app

Edit settings.py and add 'django.contrib.admin' under INSTALLED_APPS.

INSTALLED_APPS = (
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.sites',
)


Edit urls.py and uncomment all the lines that contains admin. Make sure you do not have space at the beginning on a line.

urls.png

Now you would need to redeploy the application so that you can access the admin application. But before we do that, we need to make the stylesheets available to the Jython container. 


$ asadmin deploy --property jython.mediaRoot=/tools/jython/Lib/site-packages/django/contrib/admin/ --force=true .

There is a bug in Jython container and this would not work so as a work around copy the admin media files to the GlassFish v3 domain1 docroot.

$ cp -r /tools/jython/Lib/site-packages/django/contrib/admin/media   /tools/glassfishv3/glassfish/domains/domain1/docroot/

Note: If you deploy your application at context root '/' then the above work around to make admin media files will not be needed.

Now access : http://localhost:8080/mysite/admin, it should show:

admin1.png

After you login,  the admin console would let you manage users/groups.

admin2.png

To summarise
  • Install GlassFish v3
  • Install Jython
  • Install Django
  • To use admin application or any DB backed application
    • Install django-jython
    • Install PostgreSQL
    • Make PostgreSQL JDBC driver available to jython installation
    • Enable admin application by editing settings.py and urls.py
    • Make admin media files available to GlassFish v3 Jython container by using jython.mediaRoot deploy time property or simply putting the admin static content inside GlassFish v3 docroot
The above sample creation is completely based on the Django tutorial.

A note about the Ruby application support, GlassFish v3 Preview release improves on the Ruby application support. Now the Ruby application deployment support is Rack based. Just like WSGI, Rack is a Ruby standard to wire a web server to a Ruby web framework. The Ruby web frameworks supported out of the box are: Rails, Merb and Sinatra. 

Try it out by running on GlassFish v3 Preview and send us feedback at GlassFish mailing list.
The slides for my JavaOne technical session: Dynamic Languages Powered by Glassfish are available here.
I guess was too busy with JavaOne to announce the release of GlassFish gem 0.9.5. This release is based on GlassFish v3 Kernel from GlassFish v3 Preview.

This release also has few critical fixes related to logging and an improvement in logging where you would be able to log the message on cosole using -l option without a log file name.

See the changes in this release and follow the documentation for installation and getting started. As always send your feedback to the forum and report any issues here.
Here's a list of talks about Ruby or that mention/relate to Ruby at CommunityOne and JavaOne 2009. Some of these are about other languages, since I just did a dumb search for any mention of "ruby".

Add your suggestions in comments to help narrow down which talks people should go see.

CommunityOne:

S304128

Developing on the OpenSolaris™Operating System

David Miner, Sun Microsystems; Nicholas Solter, Sun Microsystems

Monday

June 01

10:50 AM - 11:40 AM

Esplanade 305

S307894

Sun GlassFish™ Portfolio: Where Sun's Application Platform Is Going

Eduardo Pelegri-Llopart, Sun Microsystems, Inc.

Monday

June 01

10:50 AM - 11:40 AM

Hall E 135

S304001

Pragmatic Identity 2.0: Invoking Identity Services with a Simplified REST/ROA Architecture

Pat Patterson, Sun Microsystems, Inc.; Daniel Raskin, Sun Microsystems, Inc.; Ron Ten-Hove, Sun Microsystems, Inc.

Monday

June 01

11:50 AM - 12:40 PM

Gateway 102-103

S304141

Programming Languages and the Cloud

Ted Leung, Sun Microsystems, Inc.

Monday

June 01

11:50 AM - 12:40 PM

Gateway 104

S304267

Beyond Impossible: How JRuby Has Evolved the Java™ Platform

Charles Nutter, Sun Microsystems, Inc.

Monday

June 01

1:40 PM - 2:30 PM

Hall E 134

S304040

Social-Enable Your Web Apps with OpenSocial

Dave Johnson, IBM

Monday

June 01

4:00 PM - 4:50 PM

Esplanade 300

S311290

JRuby Rails Workshop

Arun Gupta, Sun Microsystems, Inc.; Jacob Kessler, Sun Microsystems; Vivek Pandey, Sun Microsystems, Inc.; Nick Sieger, Sun Microsystems, Inc

Tuesday

June 02

9:00 AM - 5:00 PM

Breakout Room 7

S311294

Cloud Computing and Storage in Practice

Tim Bray, Sun Microsystems, Inc.; Chris Kutler, Sun Microsystems, Inc.

Wednesday

June 03

1:30 PM - 5:00 PM

Breakout Room 2


JavaOne:

PAN-5348
Script Bowl 2009: A Scripting Languages ShootoutPanel SessionRoberto Chinnici, Sun Microsystems, Inc.; Thomas Enebo, Sun Microsystems, Inc. ; Rich Hickey, Clojure;Guillaume Laforge, SpringSource; Raghavan Srinivas, Self; Dick Wall , Google; Frank Wierzbicki, Sun Microsystems, Inc.Tuesday
June 02
10:50 AM - 11:50 AM
Gateway 104
TS-4164
Clojure: Dynamic Functional Programming for the JVM™ MachineTechnical SessionRich Hickey, ClojureTuesday
June 02
12:10 PM - 1:10 PM
Hall E 133
TS-4487
The Feel of ScalaTechnical SessionBill Venners, Artima, Inc.Tuesday
June 02
3:20 PM - 4:20 PM
Gateway 104
TS-5015
Welcome to RubyTechnical SessionYehuda Katz, Engine YardTuesday
June 02
4:40 PM - 5:40 PM
Gateway 104
TS-5216
Toward a Renaissance VMTechnical SessionBrian Goetz, Sun Microsystems, Inc.; John Rose, Sun MicrosystemsTuesday
June 02
6:00 PM - 7:00 PM
Hall E 133
BOF-4434
Hacking JRubyBOFOla Bini, ThoughtWorksTuesday
June 02
8:30 PM - 9:20 PM
Gateway 104
BOF-5058
JRuby Experiences in the Real WorldBOFLogan Barnett, Happy Camper Studios; David Koontz, JumpBoxTuesday
June 02
9:30 PM - 10:20 PM
Gateway 104
TS-5413
JRuby on Rails in Production: Lessons Learned from Operating a Live, Real-World SiteTechnical SessionNick Sieger, Sun Microsystems, IncWednesday
June 03
11:05 AM - 12:05 PM
Gateway 104
TS-4921
Dynamic Languages Powered by GlassFish™ Application Server v3Technical SessionJacob Kessler, Sun Microsystems; Vivek Pandey, Sun Microsystems, Inc.Wednesday
June 03
11:05 AM - 12:05 PM
Hall E 133
TS-4955
Comparing Groovy and JRubyTechnical SessionNeal Ford, ThoughtWorks Inc.Wednesday
June 03
2:50 PM - 3:50 PM
Gateway 104
BOF-4682
Performance Comparisons of Dynamic Languages on the Java™ Virtual MachineBOFMichael Galpin, eBayWednesday
June 03
6:45 PM - 7:35 PM
Esplanade 300
TS-5385
Alternative Languages on the JVM™ MachineTechnical SessionCliff Click, Azul SystemsThursday
June 04
9:30 AM - 10:30 AM
Gateway 104
TS-4012
Pragmatic Identity 2.0: Simple, Open, Identity Services Using RESTTechnical SessionPat Patterson, Sun Microsystems, Inc.; Ron Ten-Hove, Sun Microsystems, Inc.Thursday
June 04
10:50 AM - 11:50 AM
Esplanade 307-310
TS-4961
"Design Patterns" for Dynamic Languages on the JVM™ MachineTechnical SessionNeal Ford, ThoughtWorks Inc.Thursday
June 04
10:50 AM - 11:50 AM
Gateway 104
TS-5354
Exploiting Concurrency with Dynamic LanguagesTechnical SessionTobias Ivarsson, Neo TechnologyThursday
June 04
1:30 PM - 2:30 PM
Gateway 104
TS-5033
Scripting Java™ Technology with JRubyTechnical SessionThomas Enebo, Sun Microsystems, Inc. ; Charles Nutter, Sun Microsystems, Inc.Thursday
June 04
2:50 PM - 3:50 PM
Gateway 104
TS-3955
Monkeybars: Tools-Enabled Swing Development with JRubyTechnical SessionLogan Barnett, Happy Camper Studios; David Koontz, JumpBoxFriday
June 05
12:10 PM - 1:10 PM
Esplanade 302
On behalf of the Jython development team, I'm pleased to announce that Jython 2.5rc3 is available for download. See the installation instructions.

This is the third release candidate of the 2.5 version of Jython. It fixes some threading issues and partially fixes JLine on Cygwin.

Almost every release in the past year has been followed shortly by another release to fix a windows bug. Today I finally got off of my butt and installed Windows on a VM and spent the day testing, so hopefully this one will not follow that pattern.

Please try this out and report any bugs that you find.

Published three new JRuby/GlassFish production deployment stories in as many days:

Who ? Recipe Why GlassFish ?
JRuby + Rails + GlassFish v2 + MySQL + Apache Web Server + memcached The GlassFish processes have been among the most stable of our deployment.

and

(The) GlassFish team has been extremely helpful along the way with tuning and diagnosing performance issues.
JRuby + Rails + GlassFish v2 + MySQL + Solaris Zones GlassFish works and provides useful error messages.
Recipe: JRuby + Ramaze + GlassFish v2 + MySQL/H2 What's essential for me is that I spend my time doing development, not sysadmin work, so I settle for a working solution.   I've had no trouble for a few months now, and redeploy using simple scripts.

Other similar JRuby stories are available at jruby+stories. Other GlassFish stories are available here.

Technorati: jruby rubyonrails ramaze stories glassfish
I just released BiteScript 0.0.2, which mainly fixes some issues defining packages and non-public classes.

BiteScript is basically just a simple DSL for generating JVM bytecode. I use it in Duby and now in the "ruby2java" compiler we'll be using to turn Ruby classes into Java classes.

I've blogged about BiteScript here before, but I realized today I never posted any simple "hello world" examples. So here's a few of them, all using the command-line "scripting" mode.

First, the simplest version:
main do
ldc "Hello, world!"
aprintln
returnvoid
end

Obviously this is using a predefined "aprintln" macro, since there's no "aprintln" opcode on the JVM. Here's a longer version that shows how a macro would be defined, and accepts one argument
import java.lang.System
import java.io.PrintStream

macro :aprintln do
getstatic System, :out, PrintStream
swap
invokevirtual PrintStream, println, [Object]
end

macro :aprint do
getstatic System, :out, PrintStream
swap
invokevirtual PrintStream, print, [Object]
end

main do
ldc "Hello, "
aprint
aload 0
aaload 0
aprintln
returnvoid
end

And of course this is just Ruby code, so you can just use Ruby to alter the generation of code:
main do
5.times do
ldc "Wow!"
aprintln
end
returnvoid
end

These "BiteScripts" can all be either run with the "bite" command or compiled with the "bitec" command:
$ bite examples/using_ruby.bs 
Wow!
Wow!
Wow!
Wow!
Wow!

$ bitec examples/using_ruby.bs

$ javap -c examples/using_ruby
Compiled from "examples.using_ruby.bs"
public class examples.using_ruby extends java.lang.Object{
public static void main(java.lang.String[]);
Code:
0: ldc #9; //String Wow!
2: getstatic #15; //Field java/lang/System.out:Ljava/io/PrintStream;
5: swap
6: invokevirtual #21; //Method java/io/PrintStream.println:(Ljava/lang/Object;)V
9: ldc #9; //String Wow!
11: getstatic #15; //Field java/lang/System.out:Ljava/io/PrintStream;
14: swap
15: invokevirtual #21; //Method java/io/PrintStream.println:(Ljava/lang/Object;)V
18: ldc #9; //String Wow!
20: getstatic #15; //Field java/lang/System.out:Ljava/io/PrintStream;
23: swap
24: invokevirtual #21; //Method java/io/PrintStream.println:(Ljava/lang/Object;)V
27: ldc #9; //String Wow!
29: getstatic #15; //Field java/lang/System.out:Ljava/io/PrintStream;
32: swap
33: invokevirtual #21; //Method java/io/PrintStream.println:(Ljava/lang/Object;)V
36: ldc #9; //String Wow!
38: getstatic #15; //Field java/lang/System.out:Ljava/io/PrintStream;
41: swap
42: invokevirtual #21; //Method java/io/PrintStream.println:(Ljava/lang/Object;)V
45: return

}

This last example shows the resulting JVM bytecode as well.

Future plans for BiteScript include making it have better error detection (right now it just falls back on the JVM bytecode verifier, which is not the most descriptive thing in the world) and improving the API to more easily handle all the various combinations of class, field, and method modifiers. I'd also like to make it detect if you're doing bad things to the stack to save you the hassle of interpreting verification errors that may not happen until runtime.

Anyway, give it a try and feel free to contribute; the code is all Ruby, wrapping the ASM bytecode library, so anyone that knows Ruby can tweak it. The project page and wiki are hosted at Kenai.com: http://kenai.com/projects/jvmscript

And if you're not up on the JVM or JVM bytecodes, the JVM Specification is an easy-to-read complete reference for code targeting the JVM, and here is my favorite JVM opcode quickref.
A number of you have asked how you can help JRuby development. Well there's actually an easy way: fix RubySpec failures.

You may have noticed we periodically update our RubySpec "stable" revision number, and usually have to file a few bugs. This isn't because we don't want to fix those issues...on the contrary, we would love to fix them. We just don't have enough manpower, and there's usually harder issues we need to tackle first.

But most of the failures are easy to fix, and a lot of JRuby newcomers have gotten their feet wet fixing them. So here's a short guide on how to run the specs and fix them quickly.

1. Get a JRuby working copy and build it

This is as simple as 'git clone git://github.com/jruby/jruby.git', then 'cd jruby' and 'ant'. You'll need Apache Ant 1.7 and Java 5/1.5 or higher (grab "Java SE Development Kit" from the Java SE downloads page.

2. Run the CI spec run

We have a clean spec run that should be clean for you before you start. Just run "ant spec-short" and it will pull the mspec and rubyspec repositories, roll them to the stable versions, and run all known good specs. Now you're ready to investigate specific failures.

3. Run specific spec files with bugs reported

You can look in Jira under the "RubySpec" category, or look under spec/tags/ruby for "tag" files listing failing specs and Jira bug numbers. Once you find something you'd like to investigate, run that spec file using "bin/jruby spec/mspec/bin/mspec <path/to/spec/file>". For example to set the Range#initialize failures I just reported, run "bin/jruby spec/mspec/bin/mspec spec/ruby/core/range/initialize_spec.rb".

Now you can proceed to fixing it.

4. Identify where the problem is.

Most of the core classes are pretty easy to locate. Any classes in the "core" specs will have a Java class named Ruby, like RubyArray, RubyRange, and so on. They're generally located in src/org/jruby. If you know any Java, these files are pretty easy to follow, and we're standing by on IRC or on the mailing list to hold your hand at the start.

5. Create a patch and submit it to the bug

Once you have a working fix, you can go ahead create a patch, either with "git diff > somefile.patch" or by committing it to your local repository and using "git format-patch -1" to create a formatted patch for the topmost commit. Some git-fu may be necessary, so I usually just use "git diff".

That's all there is to it! You'll be a JRuby contributor in no time!
Today David R. MacIver pinged me in #scala and asked "headius: Presumably you guys have spent quite a lot of time trying to make things like system("vim") work correctly in JRuby and failing? i.e. I'm probably wasting my time to attempt similar?"

My first answer was "yes", since there's no direct way to exec a program like vim (which wants a real terminal) and have it work on the JVM. The JVM's process launching gives the newly-spawned processes the child side of piped streams, which you then have to manually pump (which is what we do in JRuby's system, backtick, and exec methods). Under these circumstances, vim may start up, but it's certainly not functional.

But then I got to thinking...if you were doing this in C, you'd fork+exec and all would be happy. But we can't fork+exec on the JVM..OR CAN WE?

As you should know by now, JRuby ships with FFI, a library that allows you to bind any arbitrary C function in Ruby code. So getting fork+exec to work was a simple matter of writing a little Ruby code:
require 'ffi'

module Exec
extend FFI::Library

attach_function :my_exec, :execl, [:string, :string, :varargs], :int
attach_function :fork, [], :int
end

vim1 = '/usr/bin/vim'
vim2 = 'vim'
if Exec.fork == 0
Exec.my_exec vim1, vim2, :pointer, nil
end

Process.waitall

Running that with JRuby (I tried master, David tried 1.3.0RC1, and 1.2.0 works too) brings up a full-screen vim session, just like you'd expect, and it all just works. No other JVM language can do this so quickly and easily.

We'll probably try to generalize this into an optional library JRubyists can load (require 'jruby/real_exec' or similar) and perhaps add fork and exec to jna-posix so that the other JVM languages can have sweet, sweet process launching too.

JRuby rocks.
I've merged changes into master (to be 1.3 soon) that should make Nailgun easier to use. And 1.3 will be the first release to include all NG stuff in the binary dist.
  • jruby --ng-server starts up a server. You can manage it however you like
  • jruby --ng uses the Nailgun client instead of launching a new JVM for the command you run. You'll just need to run make in tool/nailgun to build the ng executable (already built for you on Windows).
Future improvements will include having --ng start up the server for you if you haven't started it, passing signals through (don't expect signals to work at all right now), better management of threads and commands, and so on. But it's a good start, and people can try to play with it and report issues more easily now.

Heres a sample session:
~/projects/jruby ➔ cd tool/nailgun/ ; make ; cd -
Building ng client. To build a Windows binary, type 'make ng.exe'
gcc -Wall -pedantic -s -O3 -o ng src/c/ng.c
ld warning: option -s is obsolete and being ignored
/Users/headius/projects/jruby

~/projects/jruby ➔ jruby --ng-server
NGServer started on all interfaces, port 2113.
^Z
[1]+ Stopped jruby --ng-server

~/projects/jruby ➔ bg
[1]+ jruby --ng-server &

~/projects/jruby ➔ jruby --ng -e "puts 1"
1

~/projects/jruby ➔ time jruby -e "puts 1"
1

real 0m0.609s
user 0m0.482s
sys 0m0.119s

~/projects/jruby ➔ time jruby --ng -e "puts 1"
1

real 0m0.073s
user 0m0.010s
sys 0m0.018s

Update: For those not familiar, "NailGun is a client, protocol, and server for running Java programs from the command line without incurring the JVM startup overhead. Programs run in the server (implemented in java), triggered by the client (written in C), which handles all I/O."
[Update:A bug in our build process and jline support caused windows failures, so we have released an rc2 and changed the download link below to point there]

On behalf of the Jython development team, I'm pleased to announce that Jython 2.5rc2 is available for download. See the installation instructions.

It contains bug fixes and polish since the last beta. One especially nice bit of polish is that JLine is enabled by default now, and so using up and down arrows should work out of the box. If no major bugs are found this release will get re-labeled and released as the production version of 2.5.0.

Please try this out and report any bugs that you find.
Attended a great talk by Michael Bleigh on Twitter on Rails. He has built a gem, TwitterAuth, that uses Twitter as authentication provider (OAuth or HTTP Basic) which allows to to quickly and easily create Twitter applications in Rails. In Michael's words "TwitterAuth makes Twitter Rails apps stupid simple".

The talk built Twistener - a Twitter application in Rails that shows how many tweepl are having a conversation about you. A hosted version of the application is available at twisteners.heroku.com. The slides and  end result of the code are always helpful.

In a post-talk conversation he mentioned that all the gems are pure Ruby. Any body willing to re-build the application and trying using JRuby and GlassFish ?

And then attended Rails 3: Stepping off of the Golden Path by a "morally loose, cheese eating surrender monkey", aka Matt Aimonetti :)

What are you going to get in Rails 3 ?
  • Improved performance
  • Increased modularity 
  • Agnosticism (choose any JS framework)
  • Public API
  • Mountable Apps
Rails 3: ORM => ActiveRecord, JavaScript => Prototype, Templating => ERB, Testing => Test::Unit. The defaults are exactly same as Rails 2.x.x and the idea is to provide the least amount of changes from user perspective, yet provide a choice.

Some other key points highlighted in the talk were:
  • More flexible, Less opinionated
  • Templating engines: HAML and ERB are the big ones.
  • ORM: ActiveRecord, DataMapper
  • Adapaters: RDBMS, File Ssytem, IMAP, YAML, REST APIs, Custom APIs
  • Sequel: Prepared statement, Sharding, highly customization SQL
  • Use Hibernate with either ActionORM, JRuby
  • Ability to talk to Non RDBMS Systems; GAE, CouchDB, Redis, Tokyo Cabinet, etc
Had a so-so lunch buffet at a local Indian restaurant with a limited variety and average food. It was hot in Vegas today (98 degrees high and 72 degrees low) and we walked to the restaurant. An Indian buffet and a long walk in the high temperatures is a perfect recipe for a sleepy afternoon ;-) But it was apparently contagious cause the person sitting next to me dozed off while I kept awake!

Anyway here are some pictures captured earlier today:


And then the complete album:



See ya next year!

Technorati: conf railsconf lasvegas rubyonrails

An informal survey this morning at Rails Conf 2009 keynote showed:

40% Rails developers in startup
30% Rails developers in consulting
30% Rails developers work in internal projects

Engine Yard got a sponsor keynote slot and announced Flex - a cloud computing platform on EC2 to host Rails applications. They also showed one-button self-healing clusters. One of the speakers was particularly scared (reminds me of my early days) and IRC#railsconf had a pool of $125 to hug him on the stage :) Anyway, read more details here.

The highlight of the morning was keynote by Chris Wanstrath (@defunkt). I took multiple notes during the keynote but the  transcript has all the details. The first part of the talk has good tips on how to create a successful blog such as blog personality, template, killer name and a sleek design, sparse side bar, consistency and structure, and many others.

There was a good bashing of SourceForge towards 60% in the talk. Chris gave a suggestion to SourceForge:

They should cut their registration process down to a single page, remove the 200 character "please host my project" plea, be more lenient on the categorization, suggest an open source license for you, then allow you to change any of these things after your project has been created.

And there are other interesting ones too. Read the full text and enjoy!

Here were some of the Q&As at the end of keynote ...

Q. What was your inspiration for github ?
A. Inspiration is coding, doing open source software all the time, downloading patches, relative paths were getitng in the way to coding. Source forge registration page is too long. github is very simple for anything code related that you want to put there.

Q. You develop these interesting pieces of software, have a job, play guitar, create pieces of music, drum music, how you do this in one lifetime ?
A. I outsource most of it. All of us do it, I post my music and code on the Internet, watch a lot of TV. I'm into sharing on github. Don't have a better work ethic, just talk a lot.

Q. What do you watch on the TV  ?
A. Legend of the secret

Q. Built a bunch of tools used by non-Rubyists, how can we promote Ruby there ?
A. RubyGems is one reason I fell in love with Ruby. Remove the friction and lower the barrier to entry.

Q. Focus github around the code/community, it helps around the people, opposite of Source Forge/Ruby Forge
A. Github is like facebook for code development.

Technorati: conf railsconf git github lasvegas rubyonrails
This is a follow up post from David's keynote.

Attended Women in Rails panel discussion. The panel, Sarah Mei, Lori Olson, and Desi McAdam (from L to R), had a very interesting discussion around the genuine problems and possible solutions of involving more women in Rails community.

Sarah is trying to involve more women in the San Francisco Ruby meetup. She plans to invite non-traditional audience like those who never programmed before, other language programmers, and similar. The details will be shared after performing the exercise for a year. Lori started Calgary Ruby Group. She do lot of self promotion so that younger women feel inspired. Desi is a co-founder of devChix with the purpose of "build a community of women developers". All the panelists were very vocal about being visible, having a blog and twitter presence is a good start.

Here are some random notes captured ...

Women drop out because of kids, try to get a job and then come back with a gap in the resume. It's difficult to get a new job at that time. Sarah is trying to reach out to that group who have that gap in their resume.

Visbility is important "She did that, I can do too!".

Data point: Women % in Rails community is much less than in other development community, e.g. Java or .NET world.
Another data point: % of women is more in larger companies, not in smaller companies. The reason is facilities like maternity leave, training (don't have evening hours to train themselves, can't sacrifice family time), etc.

Real stats from 2006: Women participation in open source community is 2-3%, 20-25% in "enterprise"

Appeal from the panelist "Guys, help us, tell .NET developer that Rails is not all guys, spread the word.".

Here are some Q&As captured:

Q. Should women be given free/discounted tickets to RailsConf ?
A. If women can't pay for it, then devChix can help them. RailsConf have helped before. It'll help if childcare is available.

Q. Why are we only looking at CS ? Why not other areas who have the development skills ?
A. Panel do reach out to multiple audience and seeks help from everybody in spreading the word. Women will be working on JavaScript and thinks she is designer. A guy will read 3 blog entries and thinks he is developer. There is a market salary differential between designer and developer. Women need to be more public about their programming status.

Q. Women won't present themselves as something they are not confident because they'll be called upon. How do you fix it ?
A. Everybody is learning. David's comment "I don't know everything in Rails" was commended. Girls need to know if it's important then they can figure it out. They are scared of messing the impression of their gender.

And of course there was a discussion on "Pr0ngate scandal":

Sarah: Matt is not a bad guy, he made a mistake that lot of people make in software development. If 1 out of 100 does not match the pattern of software developer, then that "1" may not be a software developer. The organizers of the conference did not do anything wrong. I voted for the talk and trust the judgement of the people. A negative feeling started developing but don't want to see that honestly. We learned something from it. As a relatively young community, this was bound to happen.

Lori: Not from the presentation itself but form the community reaction to this event. Blown out of proportion because of the same reasons when there is a conflict with developers in same company. You can't argue with somebody regarding how they feel. Can have a discussion, but argument is never going to be a win for anyone. That's where the community reaction devolved.

Desi: If Matt would've said "Oh Crap, I offended and wouldnt mean to offend you.", everything would've been fine. To David: "Next time, do us a favor and keep your mouth shut. It didn't help."

I was certainly expecting many more women to show up in the room but there were very few. Anyway, read Desi's blog entry about the panel. And I reached out to all three of them for helping in any manner :)

I presented on Develop with Pleasure, Deploy with Fun: GlassFish and NetBeans for a better Rails experience, slides here. The several concepts in the talk are explained in the following bullets:

The next talk of the day was JRuby: State of the Art

Why JRuby on VM ?
  • Best memory management
  • Dynamic optimizations
  • Reliable native threads: run threads across multiple cores
  • Vast number of libraries
  • Interop with Java, Scala, Rhino, Jython, ...
  • Ubiquitous
Performance
  • Fastest production-ready Ruby implementation
  • Definitely faster than 1.8.6
  • JRuby -> Bytecode -> Native code -> Optimizations
Future JVM Work
  • "invokedynamic": Build fast dynamic invocation in JVM, JRuby support by June, allow Hotspot to do all optimizations across Ruby calls
  • Multi-language VM "Da Vinci Machine", Optimized tail calls, continuations, fixnums, value types
Threading
  • Only production-ready impl with real threads
  • Ruby thread is a normal thread that can run on multiple cores
Simple Rails App
  • 1 Controller/Mode/View, send 1000 reqs
  • 80% less memory in 10 instance example, 96% for 20 instances
GlassFish
  • Gem, WAR-based
  • nginx, Apache: mod_proxy
Ruby 1.9 is 80-90% complete, IRB works, RubyGems works

FFI
  • Call C functions directly from Ruby
  • Portable unlike extensions
Who uses JRuby ?
  • Kenai
  • Gravitor
  • King Pong (JRuby wrapping MonkeyEngine)
  • Oslo's Gardermoen Airport to refuel planes
  • ThoughtWorks Mingle
    • No cross-platform SVN libraryfor Ruby
    • Bundling of installation
    • Security (ecnrypting source code)
    • Memory profile
    • Avoiding process proliferation
  • mix.oracle.com
    • 5 developers, 6 weeks for all development, 2887 LOC
  • Trisano: Open source infectious disease reporting system
    • Ease of deployment
    • Every enterprise on the planet run Java
    • Extensive project roadmap
Check out interactive Q & A from the session in the following video fragment:



Later in the evening, Brian Helmkamp, Aman Gupta, Luis Navena, Pat Allan, Dan Kubb, and John Nunemaker were awarded Ruby Heroes Award!


And the keynote by Tim Ferris, lets not talk about it ;-) I edited pictures, authored my blog, caught up on email/RSS during the keynote. #railconf on IRC and twitter were way more fun! Check the live ratings.

"1" was the lowest rating that could be given anyway!

Watch the interview on why Sea Change Affinity picked JRuby/GlassFish.

Finally watch some of the snapshots captured today:


And then the evolving album:



Technorati: conf railsconf lasvegas jruby rubyonrails glassfish netbeans

At Rails Conf 2009, Jay McGaffigan from Sea Change talked about why they choose JRuby/GlassFish for their product Affinity. Here are some of the reasons he quoted:
  • Performance characterisitics (of GlassFish) have been excellent
  • Picked GlassFish based upon the recommendations from the people in industry
  • Dramatically more throughput on our GlassFish installation, 400 requests/sec instead of 100 requests/sec comapred to Tomcat
Watch the interview recorded earlier today:


Read other simiar stories at glassfish+rubyonrails+stories.

Technorati: jruby rubyonrails glassfish stories

Rails Conf 2009 started this morning. The first day consists of morning and afternoon tutorials.

I attended Nick Sieger's JRuby on Rails tutorial, the slides are available. A survey in the room showed:
  • 95% comfortable with Ruby/Rails
  • 80% have used JRuby
  • 10% use JRuby actively
Here are some of the key points highlighted in the tutorial:

Why JRuby ?
  • JRuby is "Less Bitter Java", after all Java is a great platform.
  • Concurrency (Native threading)
  • Reliability (well-behaved because of Hotspot compiler, no process monitoring, etc)
  • Encapsulation (take a Rails application, bundle it as a single deployable artifact that is fully contained)
  • Choice (Any Java application server, huge breadth of Java libraries, and can write thin Ruby wrappers around Java libraries)
Download JDK 5 minimum, JDK 6 preferred, MySQL 5.x, JRuby 1.2 (1.3.0 RC1 OK too), GlassFish v2.1 b60e

Common options
  • --server: Run with server VM, better performance
  • --headless: No UI
  • --properties: Show tweaks for compiler, JIT compiling,  thread pooling etc
  • -J<java-opt>: Pass any Java properties
  • -J-Xmx1G: Increase memory to 1G
Drawbacks: No fork(), No native extensions (for example ParseTree, EventMachine, RMagic cannot be used), No tty for subprocesses, Startup time slow for short scripts

Advantages: Improved versions of some Ruby APIs (tempfile, mutex, thread, timeout), 1.8 and 1.9 in a single install (jruby --1.9), Wrap Java libraries and APIs in Ruby

The slides have much more details in terms of deployment options (WAR-based, GlassFish Gem), and many other interesting details Scroll to slide #68 to understand all the guts of kenai.com - a real life application running using JRuby, Rails, and GlassFish.

The afternoon tutorial for me was A Hat Full of Tricks with Sinatra. The tutorial was completely code driven with no slides, just love that format!

The tutorial started with a brief introduction to Rack. A basic Rack application can be "config.ru" or "app.rb", lets start with "config.ru" Hello World:

run lambda { |env|
  [
    200,
    {
    'Content-Length' => '2',
    'Content-Type' => 'text/html',
    },
    ["hi"]
  ]
}

Run it as ...

~/samples/railsconf/sinatra/basic-rack >~/tools/jruby/bin/jruby -S rackup
[2009-05-04 13:40:18] INFO  WEBrick 1.3.1
[2009-05-04 13:40:18] INFO  ruby 1.8.6 (2009-03-16) [java]
[2009-05-04 13:40:18] INFO  WEBrick::HTTPServer#start: pid=90964 port=9292
127.0.0.1 - - [04/May/2009 13:40:27] "GET / HTTP/1.1" 200 2 0.0160
127.0.0.1 - - [04/May/2009 13:40:27] "GET /favicon.ico HTTP/1.1" 200 2 0.0060
127.0.0.1 - - [04/May/2009 13:40:30] "GET /favicon.ico HTTP/1.1" 200 2 0.0100

"config.ru" is the default Rackup script, otherwise need to specify the name. And now "app.rb" ..

App = lambda { |env|
  [
    200,
    {
    'Content-Length' => '2',
    'Content-Type' => 'text/html',
    },
    ["hi"]
  ]
}

And run it as ...

~/samples/railsconf/sinatra/basic-rack >~/tools/jruby/bin/jruby -S rackup app.rb
[2009-05-04 13:43:57] INFO  WEBrick 1.3.1
[2009-05-04 13:43:57] INFO  ruby 1.8.6 (2009-03-16) [java]
[2009-05-04 13:43:57] INFO  WEBrick::HTTPServer#start: pid=90990 port=9292
127.0.0.1 - - [04/May/2009 13:44:09] "GET / HTTP/1.1" 200 2 0.0110

In both cases, the application is accessible at "http://localhost:9292".

Change the basic "config.ru" to convert into a class as ...

class BasicRack
     def call(env)
      body = "Hello from a class"
      [
        200,
        {
        'Content-Length' => body.size.to_s,
        'Content-Type' => 'text/html',
        },
        [body]
      ]
    end
end

run BasicRack.new

and run the same way as earlier.

Change body to "env.inspect" to see an output as:



Sinatra allows reloading of application but that "feature" will be removed soon. Instead install shotgun (which does not work with JRuby yet!). Anyway, install the gem:

~/samples/railsconf/sinatra/basic-rack >~/tools/jruby/bin/jruby -S gem install shotgun
JRuby limited openssl loaded. gem install jruby-openssl for full support.
http://wiki.jruby.org/wiki/JRuby_Builtin_OpenSSL
Successfully installed configuration-0.0.5
Successfully installed launchy-0.3.3
Successfully installed shotgun-0.2
3 gems installed
Installing ri documentation for launchy-0.3.3...
Installing RDoc documentation for launchy-0.3.3...

And run as:

~/samples/railsconf/sinatra/basic-rack >~/tools/jruby/bin/jruby -J-Djruby.fork.enabled=true -S shotgun
[2009-05-04 13:55:46] INFO  WEBrick 1.3.1
[2009-05-04 13:55:46] INFO  ruby 1.8.6 (2009-03-16) [java]
== Shotgun starting Rack::Handler::WEBrick on localhost:9393
[2009-05-04 13:55:46] INFO  WEBrick::HTTPServer#start: pid=91089 port=9393

Process separate bodies depending upon the info:

class BasicRack
     def call(env)
      body = if env["PATH_INFO"] == "/foo"
        "in foo"
      else
       "in other"
      end
      [
        200,
        {
        'Content-Length' => body.size.to_s,
        'Content-Type' => 'text/html',
        },
        [body]
      ]
    end
end

run BasicRack.new

Accessing "http://localhost:9292/foo" shows "in foo" and accessing "http://localhost:9393" shows "in other".

Target application is the last application specified by "run".

Rack supports middleware which are like filters, they can applied before/after a message is processed.

Rack will initialize middleware at load, so hold on to that application as shown:

class BasicRackApp
     def call(env)
      body = "hello from app"
      [
        200,
        {
        'Content-Length' => body.size.to_s,
        'Content-Type' => 'text/html',
        },
        [body]
      ]
    end
end

class MyMiddleware
    def initialize(app)
        @app = app
    end
   
    def call(env)
        @app.call(env)
    end
end

use MyMiddleware

run BasicRackApp.new

@app.call calls the next middleware in the chain.

Rack comes with couple of standard middleware, e.g.:

use Rack::CommonLogger

Example of an after filter:

    def call(env)
        status, headers, body = @app.call(env)
        body.map! { |part| part.upcase}
        [status, headers, body]
    end

Lots of other filters available.

With a basic Rack understanding, lets build a Sinatra app:

require 'sinatra'

is the simplest Sinatra application. Save it in a file "basic-sinatra.rb" and run it as:

~/samples/railsconf/sinatra/basic-sinatra >~/tools/jruby/bin/jruby -rubygems basic-sinatra.rb
== Sinatra/0.9.1.1 has taken the stage on 4567 for development with backup from WEBrick
[2009-05-04 14:40:14] INFO  WEBrick 1.3.1
[2009-05-04 14:40:14] INFO  ruby 1.8.6 (2009-03-16) [java]
[2009-05-04 14:40:14] INFO  WEBrick::HTTPServer#start: pid=91396 port=4567

The application is now available at "http://localhost:4567". BTW, this app can easily be run using GlassFish Gem as explained  in TOTD #79. Add a simple GET method and "not_found" handler as:

require 'rubygems'
require 'sinatra'

not_found do
  'hi from other'
end

get '/foo' do
    'hi from foo'
end

Every time a request comes in, it builds a request context, instance evals lambda and finds the one that hits.

Sinatra takes care of status and headers, the application needs to process the body.

Another one ...

require 'rubygems'
require 'sinatra'

get '/env' do
    env.inspect
end

And it shows Rack environment hash at 'http://localhost:4567".

Another one ...

require 'rubygems'
require 'sinatra'

get '/' do
end

post '/' do
end 

put '/' do
end

delete '/' do
end

This adds 4 HTTP methods with different routes.

No explicit render method, e.g.

require 'rubygems'
require 'sinatra'

get '/' do
  content_type "application/json"
  { "foo" => "goo" }.to_json
end

No ".rhtml.erb" or ".json.erb", instead it's just ".erb". Add "views/index.erb" as:

<html>
  <body>
  Hello form Sinatra + ERB
  </body>
  </html>

And change GET method to:

require 'rubygems'
require 'sinatra'

get '/' do
  erb :index
end

And the application now uses ERB templating.

Using HAML templates is simple, change to:

require 'rubygems'
require 'sinatra'
require 'haml'

get '/' do
  haml :index
end

And add "views/index.haml" as:

%html
  %body
    %h1 Hello from HAML

And now the application is using HAML templates.

__END__ is the end of Ruby, can be anything after that and it'll not barf. Sinatra uses it for in file templates:

require 'rubygems'
require 'sinatra'
require 'haml'

get '/' do
  erb :index
end

use_in_file_templates!

__END__

@@ index

<html>
  <body>
  Hello form Sinatra + ERB in file
  </body>
  </html>

Start with in-file templates, and then move out to separate directory ("views") once grows big. But no syntax highlighting etc.

Add your custom template as:

require 'rubygems'
require 'sinatra'
require 'haml'

get '/' do
  erb :index
end

get '/foo' do
  erb :foo
end

use_in_file_templates!

__END__

@@ index

<html>
  <body>
  Hello form Sinatra + ERB in file
  </body>
  </html>
 
@@ foo
<h1>FOO!</h1>

With this file "http://localhost:4567/" uses ERB template, and "http://localhost:4567/foo" uses "foo" template.

Sinatra defines on Main. The before filters work before every single request, executed in the same context as lambda. Can be used if every request needs to do some setup.

Helpers can be defined as:

require 'rubygems'
require 'sinatra'
require 'haml'

helpers do
 
end

without defining on Main. Or ...

require 'rubygems'
require 'sinatra'
require 'haml'

module helpers
    def self.dosomething(arg)
    end
end

get '/' do
    Helpers.dosomething
end

Don't define something on Main, it's a bad practice.

Extension is a nice package that can be shared for other Sinatra developers to use, like Rails plugins but does not have boilerplate, much easier to do.

Rest of tutorial was quite fast paced so the code samples could not be captured. But there is boatload of information available at sinatrarb.com.

Check out the pictures from Day 1:


The evening concluded with dinner at Burger Bar at Mandalay Bay along with Project Kenai team.

And check the evolving album at:



On to GlassFish talk tomorrow, and running with @railsConfRunner in the morning before that :)

Technorati: conf railsconf lasvegas jruby rubyonrails sinatra glassfish

In JRuby-on-Rails tutorial at Rails Conf 2009, Nick Sieger shared a nice little tip on how to configure "database.yml" to be usable with both JRuby and MRI:

<% jdbc = defined?(JRUBY_VERSION) ? 'jdbc' : '' %>
development:
  adapter: <%= jdbc %>mysql
  encoding: utf8
  reconnect: false
  database: myapp_development
  pool: 5
  username: root
  password:
  socket: /tmp/mysql.sock
# ...

"JRUBY_VERSION" is defined only if your using JRuby and so the right database adapter is picked up accordingly.

The complete slides for the tutorial are available here. Learn about other related talks here.

Please leave suggestions on other TOTD (Tip Of The Day) that you'd like to see. An archive of all the tips is available here.

Technorati: conf railsconf lasvegas tutorial jruby ruby rubyonrails
Would you like to power up your Rails applications using JRuby and GlassFish ? And learn that from the engineers who develop the technology.

If yes, then we have organized a bootcamp for you!

Day 1 (FREE) of this bootcamp provides an introduction to JRuby and GlassFish and how they serve as an excellent development and deployment environment for Rails applications. Starting with clean slate on your laptop, you'll be able to setup JRuby, Rails, GlassFish and learn about different options available for running your applications.

Day 2 (need $$$) takes a deep dive on each topic and convert you into a power user instantaneously. The topics range from Virtual Machine tuning for JRuby and GlassFish, Warbler tricks, Java EE integration, Deployment strategies, Monitoring applications to Running other Rack-based frameworks. Lunch and beverages will be served on Day 2.

On both days, you get an opportunity to practice everything on your laptop by following the experts along.

Complete details on venue, time, agenda, etc are available at railscamp.eventbrite.com.

Register now before the seats fill out. And get ready to be drenched!

Technorati: conf jruby rubyonrails glassfish netbeans bootcamp sanfrancisco
nginx (pronounced as "engine-ex") is an open-source and high-performance HTTP server. It provides the common features such as reverse proxying with caching, load balancing, modular architecture using filters (gzipping, chunked responses, etc), virtual servers, flexible configuration and much more.

nginx is known for it's high performance and low resource consumption. It's a fairly popular front-end HTTP server in the Rails community along with Apache, Lighttpd, and others. This TOTD (Tip Of The Day) will show how to install/configure nginx for load-balancing/front-ending a cluster of Rails application running on GlassFish Gem.
  1. Download, build, and install nginx using the simple script (borrowed from dzone):

    ~/tools > curl -L -O http://sysoev.ru/nginx/nginx-0.6.36.tar.gz
    ~/tools > tar -xzf nginx-0.6.36.tar.gz
    ~/tools > curl -L -O http://downloads.sourceforge.net/pcre/pcre-7.7.tar.gz
    ~/tools > tar -xzf pcre-7.7.tar.gz
    ~/tools/nginx-0.6.36 > ./configure --prefix=/usr/local/nginx --sbin-path=/usr/sbin --with-debug --with-http_ssl_module --with-pcre=../pcre-7.7
    ~/tools/nginx-0.6.36 > make
    ~/tools/nginx-0.6.36 > sudo make install
    ~/tools/nginx-0.6.36 > which nginx
    /usr/sbin/nginx

    OK, nginx is now roaring and can be verified by visiting "http://localhost" as shown below:


  2. Create a simple Rails scaffold as:

    ~/samples/jruby >~/tools/jruby/bin/jruby -S rails runner
    ~/samples/jruby/runner >~/tools/jruby/bin/jruby script/generate scaffold runlog miles:float minutes:integer
    ~/samples/jruby/runner >sed s/'adapter: sqlite3'/'adapter: jdbcsqlite3'/ <config/database.yml >config/database.yml.new
    ~/samples/jruby/runner >mv config/database.yml.new config/database.yml
    ~/samples/jruby/runner >~/tools/jruby/bin/jruby -S rake db:migrate
  3. Run this application using GlassFish Gem on 3 separate ports as:

    ~/samples/jruby/runner >~/tools/jruby/bin/jruby -S glassfish
    Starting GlassFish server at: 192.168.1.145:3000 in development environment...
    Writing log messages to: /Users/arungupta/samples/jruby/runner/log/development.log.
    Press Ctrl+C to stop.

    The default port is 3000. Start the seond one by explicitly specifying the port using "-p" option ..

    ~/samples/jruby/runner >~/tools/jruby/bin/jruby -S glassfish -p 3001
    Starting GlassFish server at: 192.168.1.145:3001 in development environment...
    Writing log messages to: /Users/arungupta/samples/jruby/runner/log/development.log.
    Press Ctrl+C to stop.

    and the last one on 3002 port ...

    ~/samples/jruby/runner >~/tools/jruby/bin/jruby -S glassfish -p 3002
    Starting GlassFish server at: 192.168.1.145:3002 in development environment...
    Writing log messages to: /Users/arungupta/samples/jruby/runner/log/development.log.
    Press Ctrl+C to stop.

    On Solaris and Linux, you can run GlassFish as a daemon as well.
  4. Nginx currently uses a simple round-robin algorithm. Other load balancers such as nginx-upstream-fair (fair proxy) and nginx-ey-balancer (maximum connections) are also available. The built-in algorithm will be used for this blog. Edit "/usr/local/nginx/conf/nginx.conf" to specify an upstream module which provides load balancing:
    1. Create a cluster definition by adding an upstream module (configuration details) right before the "server" module:

      upstream glassfish {
              server 127.0.0.1:3000;
              server 127.0.0.1:3001;
              server 127.0.0.1:3002;
          }

      The cluster specifies a bunch of GlassFish Gem instances running at the backend. Each server can be weighted differently as explained here. The port numbers must exactly match as those specified at the start up. The modified "nginx.conf" looks like:



      The changes are highlighted on lines #35 through #39.
    2. Configure load balancing by specifying this cluster using "proxy_pass" directive as shown below:

      proxy_pass http://glassfish;

      in the "location" module. The updated "nginx.conf" looks like:



      The change is highlighted on line #52.
  5. Restart nginx by using the following commands:

    sudo kill -15 `cat /usr/local/nginx/logs/nginx.pid`
    sudo nginx
Now "http://localhost" shows the default Rails page as shown below:



"http://localhost/runlogs" now serves the page from the deployed Rails application.

Now lets configure logging so that the upstream server IP address and port are printed in the log files. In "nginx.conf", uncomment "log_format" directive and add "$upstream_addr" variable as shown:

    log_format  main  '$remote_addr - [$upstream_addr] $remote_user [$time_local] $request '
                      '"$status" $body_bytes_sent "$http_referer" '
                      '"$http_user_agent" "$http_x_forwarded_for"';

    access_log  logs/access.log  main;

Also change the log format to "main" by uncommenting "access_log logs/access.log main;" line as shown above (default format is "combined"). Accessing "http://localhost/runlogs" shows the following lines in "logs/access.log":

127.0.0.1 - [127.0.0.1:3000] - [29/Apr/2009:15:27:57 -0700] GET /runlogs/ HTTP/1.1 "200" 3689 "-" "Mozilla/5.0 (Macintosh; U; Intel Mac OS X 10_5_6; en-us) AppleWebKit/525.27.1 (KHTML, like Gecko) Version/3.2.1 Safari/525.27.1" "-"
127.0.0.1 - [127.0.0.1:3001] - [29/Apr/2009:15:27:57 -0700] GET /favicon.ico HTTP/1.1 "200" 0 "http://localhost/runlogs/" "Mozilla/5.0 (Macintosh; U; Intel Mac OS X 10_5_6; en-us) AppleWebKit/525.27.1 (KHTML, like Gecko) Version/3.2.1 Safari/525.27.1" "-"
127.0.0.1 - [127.0.0.1:3002] - [29/Apr/2009:15:27:57 -0700] GET /stylesheets/scaffold.css?1240977992 HTTP/1.1 "200" 889 "http://localhost/runlogs/" "Mozilla/5.0 (Macintosh; U; Intel Mac OS X 10_5_6; en-us) AppleWebKit/525.27.1 (KHTML, like Gecko) Version/3.2.1 Safari/525.27.1" "-"

The browser makes multiple requests (3 in this case) to load resources on a page and they are nicely load-balanced on the cluster. If an instance running on port 3002 is killed, then the access log show the entries like:

127.0.0.1 - [127.0.0.1:3000] - [29/Apr/2009:15:28:53 -0700] GET /runlogs/ HTTP/1.1 "200" 3689 "-" "Mozilla/5.0 (Macintosh; U; Intel Mac OS X 10_5_6; en-us) AppleWebKit/525.27.1 (KHTML, like Gecko) Version/3.2.1 Safari/525.27.1" "-"
127.0.0.1 - [127.0.0.1:3002, 127.0.0.1:3000] - [29/Apr/2009:15:28:53 -0700] GET /favicon.ico HTTP/1.1 "200" 0 "http://localhost/runlogs/" "Mozilla/5.0 (Macintosh; U; Intel Mac OS X 10_5_6; en-us) AppleWebKit/525.27.1 (KHTML, like Gecko) Version/3.2.1 Safari/525.27.1" "-"
127.0.0.1 - [127.0.0.1:3001] - [29/Apr/2009:15:28:53 -0700] GET /stylesheets/scaffold.css?1240977992 HTTP/1.1 "200" 889 "http://localhost/runlogs/" "Mozilla/5.0 (Macintosh; U; Intel Mac OS X 10_5_6; en-us) AppleWebKit/525.27.1 (KHTML, like Gecko) Version/3.2.1 Safari/525.27.1" "-"

The second log line shows that server running on port 3002 did not respond and so it automatically fall back to 3000, this is nice!

But this is inefficient because a back-end trip is made even for serving a static file ("/favicon.ico" and "/stylesheets/scaffold.css?1240977992"). This can be easily solved by enabling Rails page caching as described here and here.

More options about logging are described in NginxHttpLogModule and upstream module variables are defined in NginxHttpUpstreamModule.

Here are some nginx resources:
Are you using nginx to front-end your GlassFish cluster ?

Apache + JRuby + Rails + GlassFish = Easy Deployment! shows similar steps if you want to front-end your Rails application running using JRuby/GlassFish with Apache.

Hear all about it in Develop with Pleasure, Deploy with Fun: GlassFish and NetBeans for a Better Rails Experience session at Rails Conf next week.

Please leave suggestions on other TOTD (Tip Of The Day) that you'd like to see. A complete archive of all tips is available here.

Technorati: rubyonrails glassfish v3 gem jruby nginx loadbalancing clustering
In the spirit of Nick Sieger's short statement on the recent uproar over Matt Aimonetti's "pr0n star" talk, I'm posting my one and only blog response to the whole thing.

Unlike Nick, I too often have used this blog as a soapbox. And too often I've ground my personal axe against projects that may or may not have deserved it. I'm human, I'm passionate and proud of my work, and I'm defensive of what we've accomplished, so I don't think this is surprising. I also see the same passion and pride in the Ruby community at large, and it's why I'm much more interested in attending Ruby conferences than Java conferences, where many attendees just seem to be going through the motions. And I know I've crossed a line at times, making or taking things too personal, and hopefully I've apologized or corrected myself whenever that's happened. If not, mea culpa.

But there's a disturbing trend in the Ruby community I haven't had to deal with since high school: in preference to open inclusion, more and more Rubyists seem to choose exclusivity.

This recent firestorm has continued in large part, I believe, because of the poor initial response by folks involved. Rather than recognize that there are people with different views, taking offense at different ideas and images, some decided to say "fuck you, this is who I am" and further alienate those people. I certainly don't expect we as passionate individuals won't commit occasional faux pas, especially when trying to be funny or provocative and especially when coming from different backgrounds that may be more or less accepting of certain behaviors. But to claim no responsibility for an obvious mistake, indeed to claim it's somehow the fault of the offended, or American sensibility, or political correctness...well that's just sophomoric.

I think to some extent we can understand (but not excuse) such behavior by realizing that the Ruby (or perhaps the Rails) community is largely a very *young* community. That's a large part of why this community is so passionate, why they're so committed to their ideals, why they're so opinionated, why they're so much more fun to hang out with than many 30-year programmers from other communities who've had the life sucked out of them. It's also a reason so many in the Ruby (or perhaps the Rails) community seem to act like they're in high school, forming cliques, sitting at their own tables, snubbing the new kids or the weird kids or anyone they perceive as "trying to be cool."

Have you been invited to any exclusive Ruby communities? I've been invited to a couple, and without exception I've found the idea offensive every time. In some cities, there are now multiple tiers of Ruby group: one for the proles, where anyone is welcome and everyone is either new to Ruby, a little weird, or both; and then perhaps one or two levels of more "exclusive" groups, usually more "advanced" and sometimes invite-only but generally exclusionary in some way.

There's also a technical "coolness" exclusivity many projects have had to cope with. Folks working on JRuby and IronRuby, for example, have had to deal with perceptions that they're either less "cool" because of their platform of choice or at least somehow less "Ruby" because they're not following the same golden path everyone else follows. Or perhaps their employers are out to take over Ruby, or they're going to infect Ruby with a bunch more "new" people who don't "get it". All the while the folks that use and work on these projects are working just as hard as anyone else to bring Ruby to the world, staying true to what makes Ruby special, and largely going against the grain in their original communities as well. Being snubbed, mocked, or attacked is often their reward.

You start to see a pattern here, yes?

So let's spell it out. I like the Ruby community because it's filled with people who love playing with new technology, without biases and prejudices getting in the way. My closest friends in the community are people like me, who find it repugnant that being opinionated has been too often equated with being rude and boorish, exclusionary and sophomoric, or simply mean. We are all here because of our love of technology, all here because we didn't feel like we fit in other places that weren't so passionate about beautiful code and fresh ideas. We are all here because we don't care if you're male or female, religious or irreligious, young or old, experienced or inexperienced, beautiful or plain, conservative or liberal, tall or short, fat or thin, foreign or domestic, gay or straight, black or white, or any grey areas in-between. We are all here because we love that more and more people like us join the community every day...the same people some of us immediately judge and box into their own subcool subgroups.

I don't want to join your damn clique. I don't think it's ok to set people aside or treat them like dirt because they don't believe what you believe or because they have their own way of thinking and acting or because they're not as worldly and mature and oh-so-smug as you are. I don't believe in "rock stars" and I don't believe that dubious title gives anyone the right to be an asshole to others or to have free reign to act any way they choose. I don't care what kind of car you drive, what house you live in, or what clothes you wear...and I sure as hell don't care how many people follow you on Twitter.

What I do care about is whether you're interested in sitting down and hacking out some code, looking at new projects with an open mind, helping someone new (maybe me) improve their skills, being part of something larger than yourself. If you promise not to treat me like a weirdo or a rock star, I promise to talk openly about your ideas, to show you the heart and soul of my code, and to freely share my thoughts...no matter who you are. I hope you'll attend my presentations and/or try out my projects, and in exchange I'll try to do the same the same for you. I hope you'll walk up to me at conferences and tell me about whatever "crazy" or "stupid" idea you have, and I guarantee to listen since it's probably not as crazy or stupid as you think. And I expect you to do the same for everyone else in the community and not treat me or anyone else any differently.

Now, let's move forward and get back to hacking and having fun!
Jython 2.5b4 is available for download. See the installation instructions.

While no new features have been added since Beta 3, we have fixed a number of bugs. One of the bugs prompted an almost total re-write of our Tuple and List implementations, which is the reason that this is another beta and not a release candidate. Expect a release candidate real soon now.

We also applied a patch that allows Jython to function in a system that does not have write access to the filesystem so that Jython can run unpatched in Google App Engine.

This is a beta release so be careful.

Did you know that ...

  • GlassFish Gem is already used in production
  • GlassFish Gem can be used to run Rails, Merb, Sinatra, and any other Rack-based framework
  • Capistrano recipes are available for starting/stopping/bouncing the server
  • With GlassFish, standard Java monitoring techniques like JMX can be used for monitoring Rails apps
  • NetBeans provide a complete development environment for Rails applications

There are many other similar nuggets that I'll be covering in my Rails Conf 2009 session. Details are given below:

Develop with pleasure, Deploy with Fun: GlassFish and NetBeans for a better Rails experience
Tuesday, May 5th, 2009, 1:50pm
Pavilion 1

Register Today and avail a 15% discount using the code: RC09FOS.

I plan to attend these sessions, lets see how many I can make :-) And of course, you'll see me in the Exhibit Hall.

And you'll get to meet Project Kenai team, they form the foundation for Sun's connected developer experience. Read about their participation here and meet them to learn about NetBeans and Kenai integration.

And if you are interested in running with fellow attendees, follow @railsConfRunner.

And it's Vegas baby!

JRuby and GlassFish is already used in production. Do you have a success story to share ? I'll be happy to promote at RailsConf.

Technorati: conf glassfish netbeans rubyonrails kenai railsconf lasvegas jruby
I figured I'd give Typo a try on JRuby today. It has been working for quite a while, but with the GlassFish gem improving so much I thought it would be good to write up an updated walkthrough. It's pretty simple.

BTW, is Typo still the preeminent blog package for Rails? I certainly don't want to be out of fashion.

Prerequisites:
  1. MySQL already set up and working, with TCP sockets enabled (or I guess you can use SQLite too)
  2. Java (sudo apt-get install sun-java6-jdk or whatever's appropriate for your platform)
  3. JRuby (download, unpack, put bin/ in PATH)
  4. Appropriate gems installed (rails, activerecord-jdbcmysql-adapter, glassfish or mongrel)
The process:
  1. Download Typo. The gem unfortunately tries to install native extensions like sqlite3 and mysql (I sure wish they wouldn't do that!)
  2. Unpack the Typo zip wherever you want your blog site to live and cd into that directory
  3. Edit config/database.yml.example to your liking, replacing "mysql" with "jdbcmysql" and save it as config/database.yml
  4. Create the database:
    jruby -S rake db:create RAILS_ENV=production
  5. Migrate the database:
    jruby -S rake db:migrate RAILS_ENV=production
  6. Run the server:
    glassfish -p <port> -e production [and whatever other options you want]
    or
    jruby script/server -p <port> -e production
  7. Set up Apache to point at your new Typo instance (optional)
That's all there is to it! You'll want to be the first one to hit your new blog, so you can set up your admin account and lock down the server.

Perhaps it's time I finally moved my blog off Blogger and on to a JRuby-hosted server, eh?

Suggestions, improvements to this process? Add to comments and I'll update the post.
It occurred to me today that a lot of people probably want a JRuby deployment option that works with a front-end web server. I present for you the trivial steps required to host a JRuby server behind Apache.

Update: It's worth mentioning that this works fine with JRuby + Mongrel too, though Mongrel doesn't automatically multithread without this patch and Rails' production.rb config.threadsafe! line uncommented. The GlassFish gem will automatically multithread with several JRuby instances (in the same server process) by default or a single JRuby instance with config.threadsafe! uncommented.

Prerequisites:
  1. Apache with mod_proxy_http enabled (sudo a2enmod proxy_http on Ubuntu)
  2. Java (sudo apt-get install sun-java6-jdk or the openjdk flavors if you like)
  3. JRuby (download, unpack, put bin/ in PATH)
  4. gems appropriate to run your app with JRuby (e.g. rails, activerecord-jdbcsqlite3-adapter, etc)
  5. production DB all set to go
Ok. I'm no Apache expert, so I'm sure there's some tweaking necessary for this. Please add your tweaks and suggestions in comments. But basically, all you need to do is run your app using the GlassFish gem and set up Apache to proxy to it. It's that simple.
  1. Install the glassfish gem
    gem install glassfish
  2. From your application directory, run glassfish with these options:
    glassfish -p <port> -e production -c <context> -d
  3. Add ProxyPass and ProxyPassReverse lines to Apache (whereever is appropriate on your system) for the GlassFish server instance. For example, if <port> is 9000 and <context> is foo:
    ProxyPass /foo http://localhost:9000/foo
    ProxyPassReverse /foo http://localhost:9000/foo
  4. Reload Apache config, however is appropriate for your system
You'll now be able to access your app via http://servername/foo, and requests will all proxy to the GlassFish server instance.

This doesn't do anything to manage the server instance, but since GlassFish can start up as a daemon now, it should be easy to wire into whatever you normally use for that.

A few caveats:
  • I had some trouble getting mod_proxy to allow requests to proxy through. There's probably a right way I'm not doing, so I won't say what I did. If you know the ideal mod_proxy config for this sort of thing, post it in comments
  • Although the GlassFish gem is really nice already, we're still working minor kinks out of it and it may still have minor bugs. If you run into something, let us know and we'll get it fixed (and of course, you can use this mod_proxy setup with Mongrel too, if you like).
  • I'd love to get some help putting together something that manages servers similar to Phusion Passenger, because that's really the only missing piece here.
I'll update this post as suggestions come in. Enjoy!

SeaChange Affinity uses Rails and GlassFish as their deployment platform. One of their core developers posted tips based upon their experience so far and they are available at:

Rails on GlassFish v2 using Windows

Here are some of the quotes:

Glassfish can really handle a heavy load

and

handling 400 simultaneous users under a supremely heavy load, the memory was holding great

All previous links in this series are archived at LOTD.

Technorati: lotd glassfish jruby rubyonrails windows