Rails and a dose of JRuby

39 views
Skip to first unread message

Chris McCann

unread,
Jun 8, 2011, 10:36:25 PM6/8/11
to SD Ruby
As a follow-up to my previous post about manipulating spreadsheets
natively in Ruby I figured I'd spread the word on what I learned.

The ruby spreadsheet gem is nice but it lacks one key feature: it
doesn't support recalculating formulas in a spreadsheet after a
dependent value has changed. This is a show-stopper for my app as I
need to allow users to change values that drive numerous formulas and
immediately see the results of the recalculations.

I have been in touch with Zeno Davatz, who seems to manage the gem,
and it appears that this capability is slated for a release down the
road but not anytime soon.

I tried using the google_spreadsheet gem. This actually sort of
worked, with one key issue. I could interactively modify a Google
spreadsheet in my Google Docs account and see the values update
immediately.

The problem is the "local" ruby copy of the spreadsheet doesn't
reflect updates to formula values until you re-synchronize the in-
memory copy of the spreadsheet with the one on the server, and that
seems to take anywhere from 5-10 seconds, a non-starter for UX
reasons. Also, google_spreadsheets has a limit of 256 columns (and
some limitations to the functions you can use) but I could live with
that. The formula refresh delay -- not so much.

I also looked into Windows-based solutions, thinking I might be able
to write a web service my Rails app could hit but that seemed over the
top, not to mention prohibitively expensive when you add in Windows
and SharePoint costs, hosting, etc.

There is a SpreadScript excel calculation engine product out there,
but it only supports Python, Perl and Tcl, so again I'd have to write
a web service or wrapper to use it. Ouch. And it costs $995 to
deploy, so double Ouch.

Someone suggested JRuby wrapping a Java API. That sounded much more
like what I was after. I found the Apache POI project, which enables
interaction with MS Office documents like Excel spreadsheets. I built
a little Java test app today and voila, formulas that update
instantly!

Of course, now I have to figure out how to make use of this
capability, which brings me to a question: can anyone suggest a good
way to use JRuby in a Ruby-based Rails app to make use of the Apache
POI Java project? Am I back to trying to write a web service?

Any suggestions would be greatly appreciated!

Cheers,

Chris

John Lynch

unread,
Jun 8, 2011, 11:09:26 PM6/8/11
to sdr...@googlegroups.com
Chris, you can run a bog standard Rails app with the Trinidad gem,
which runs your app in a jruby/tomcat environment, and then you can
call any arbitrary java class directly from ruby. We have used this to
integrate to BIRT reports among other java-esque things. It's awesome.

For a more enterprise class deployment check out Torquebox, which
gives your rails app easy access to lots of java goodies like message
queues and async processing.

The biggest issue we have run into deploying rails apps in java
containers is making sure the jars are all in the right place.


- John
Jo...@rigelgroupllc.com

> --
> SD Ruby mailing list
> sdr...@googlegroups.com
> http://groups.google.com/group/sdruby

Chris McCann

unread,
Jun 9, 2011, 12:18:59 AM6/9/11
to SD Ruby
John,

Thanks as always for the sage advice. Looking at both Trinidad and
Torquebox they certainly seem mature enough for what I'm trying to do.

One question, though. Having never used JRuby are there any downsides
to using it for a Rails app? Performance, gems, learning curve, etc?

I will be researching JRuby extensively as the Apache POI project
seems superb and the spreadsheet functionality is a key feature of the
app I'm prototyping.

Oh, and for anyone else wondering about the phrase "bog standard",
have a gander: http://www.phrases.org.uk/meanings/bog-standard.html

Cheers,

Chris

On Jun 8, 8:09 pm, John Lynch <j...@rigelgroupllc.com> wrote:
> Chris, you can run a bog standard Rails app with the Trinidad gem,
> which runs your app in a jruby/tomcat environment, and then you can
> call any arbitrary java class directly from ruby. We have used this to
> integrate to BIRT reports among other java-esque things. It's awesome.
>
> For a more enterprise class deployment check out Torquebox, which
> gives your rails app easy access to lots of java goodies like message
> queues and async processing.
>
> The biggest issue we have run into deploying rails apps in java
> containers is making sure the jars are all in the right place.
>
> - John
> J...@rigelgroupllc.com

John Lynch

unread,
Jun 10, 2011, 4:17:41 PM6/10/11
to sdr...@googlegroups.com
Getting started with Jruby is easy:

Install jruby via rvm:  "rvm install jruby; rvm use jruby"

We have something like this in the Gemfile for our app:

platforms :jruby do
  gem 'activerecord-jdbc-adapter'
  gem 'jdbc-mysql'
  gem 'jruby-openssl'
end

platforms :ruby do
  gem 'mysql2'
end

then you just "bundle install" and "gem install trinidad", and run "trinidad -t" and its just like running "rails server".  (The -t param tells jruby to use multithreaded mode, with only one copy of the app handling requests via multiple threads. If some aspect of your app is not threadsafe (doubtful these days), leave off the -t and it will start up multiple instances of the app a la Mongrel). 

We have found that if you give it enough RAM (like anything related to Java) the performance is great, on par with Passenger.

As for downsides, we have bumped our knees on some rough edges, no doubt about that, but you trade that time and effort off against the time saved by not having to write your own spreadsheet editor and you will most likely come out way ahead in the end. 


Regards,

John Lynch
Rigel Group, LLC
Mobile: 760-515-2653
Skype: johnthethird 

Chris McCann

unread,
Jun 13, 2011, 11:34:24 AM6/13/11
to SD Ruby
John,

I'm almost there but I can't get the server started using Trinidad.
I'm getting an error related to a JAR not being found, and I suspect
that's what you were alluding to in your previous reply. Here's the
stacktrace:

[chris@chris (master)]$ jruby -S trinidad
Jun 12, 2011 11:43:14 PM org.apache.coyote.AbstractProtocolHandler
init
INFO: Initializing ProtocolHandler ["http-bio-3000"]
Jun 12, 2011 11:43:14 PM org.apache.catalina.core.StandardService
startInternal
INFO: Starting service Tomcat
Jun 12, 2011 11:43:14 PM org.apache.catalina.core.StandardEngine
startInternal
INFO: Starting Servlet Engine: Apache Tomcat/7.0.11
Jun 12, 2011 11:43:14 PM org.apache.catalina.startup.ContextConfig
webConfig
INFO: No global web.xml found
Jun 12, 2011 11:43:14 PM
org.apache.tomcat.util.scan.StandardJarScanner scan
WARNING: Failed to scan [file:/Users/chris/rails/protorocket/~/
development/tools/junit-4.5.jar] from classloader hierarchy
java.util.zip.ZipException: error in opening zip file

I didn't do any configuration besides installing jruby via rvm and
then installing trinidad. Searching for more detailed instructions on
getting a Rails app up and running on jruby yielded little.

If you have any hints I'll take them!

Chris
Reply all
Reply to author
Forward
0 new messages