Java and JRuby tight integration

Hi

I have a requirement for integrating one of our existing applications
written in Ruby with another Java application. They need to appear
seamless
and thereby share the same information (e.g. session). At this stage,
most
of the information is available through a UI powered by Ruby.

My migration idea is to migrate Ruby to JRuby and then integrate this
with
the Java app.

Is migrating a Ruby application to JRuby a fairly trivial task?
How easy is it for Java to be able to access the JRuby session
information
and vice-versa? I assume Ruby/JRuby doesn’t encode the session
information
in a way that a Java app can’t read it straightforwardly? (e.g. doesn’t
do
what .NET does with the viewstate field)

Are there any gotchas/pitfalls that you would look out for?

Would it be possible to have an approach of dual running both in the
same
Java container, and then gradually rewriting one of the applications:
e.g. eliminating the Java code or the Ruby code in favour of the other.
Does one way cause more issues than the other?

I’m more of a Java guy myself, so any information JRuby would be
welcome.

Chris

On Thu, May 17, 2012 at 5:08 PM, Chris B. [email protected]
wrote:

Is migrating a Ruby application to JRuby a fairly trivial task?

For the most part, yes, aside from a few gotchas (more below).

You could look at this project as an example of blending Java and
Rails. GitHub - nicksieger/refactoring-to-rails: Example of refactoring a Spring/Hibernate application to Rails, and the
associated talk at
http://www.confreaks.com/videos/572-scotlandruby2011-refactoring-a-legacy-java-application-to-rails.

How easy is it for Java to be able to access the JRuby session information
and vice-versa? I assume Ruby/JRuby doesn’t encode the session information
in a way that a Java app can’t read it straightforwardly? (e.g. doesn’t do
what .NET does with the viewstate field)

There’s no viewstate in Rails, and (though I haven’t tried it myself)
I believe that jruby-rack (which you would be the glue between your
servlet environment and the Rails environment) I believe it includes a
backing store so that rails data is stored in the normal servlet
session. Now, accessing that data from Java may be slightly
cumbersome, since you’ll have to cast them to the appropriate
IRubyObject. But, accessing Java objects stored in the session from
Ruby would be considerably more convenient.

Are there any gotchas/pitfalls that you would look out for?

There are a few gotchas, but you could give jrlint
(GitHub - jruby/jruby-lint: See how ready your Ruby code is to run on JRuby) a pass at your current
application. It should warn you of some common cases.

Would it be possible to have an approach of dual running both in the same
Java container, and then gradually rewriting one of the applications:
e.g. eliminating the Java code or the Ruby code in favour of the other. Does
one way cause more issues than the other?

The refactoring-to-rails app shows exactly this. Taking an existing
Java app (the Pet Clinic sample spring app) and gradually migrating it
to Ruby/Rails.

Good luck!

On May 17, 2012, at 5:08 PM, Chris B. wrote:

Is migrating a Ruby application to JRuby a fairly trivial task?

I did a short screencast showing how to do this for an existing Rails
app:

I use Trinidad in that example, but you’ll probably want to use
Warbler+Tomcat/Jetty/etc if you’re already running with an existing Java
servlet container (although, you can deploy a WAR to Trinidad).

I cannot speak to moving from Ruby to JRuby… I think the issue can be
gem
support, but it’s getting better. I can talk about interop between
Java/JRuby, however. I embedded JRuby in Apache PIg so that users could
define UDF’s (think of this like a user defined COUNT or SUM in the
database world) using JRuby. I found the tools to be more than adequate,
though the documentation is thin, and there are definitely some gotchas
(and you will undoubtedly have to make your own conversion functions).
Still, I think that the power of Ruby is worth it… a little pain on
the
internals is worth the expressivity of Ruby :slight_smile:

2012/5/17 Marko A. [email protected]

On Fri, May 18, 2012 at 1:08 AM, Chris B. [email protected]
wrote:

and vice-versa? I assume Ruby/JRuby doesn’t encode the session information
in a way that a Java app can’t read it straightforwardly? (e.g. doesn’t do
what .NET does with the viewstate field)

Are there any gotchas/pitfalls that you would look out for?

I’ve been experimenting with embedding JRuby in a Java application a bit
lately and one gotcha I ran into is that in some cases you may need to
implement your own data conversion code. First I tried passing data from
Java code to Ruby code that’s invoked using JavaEmbedUtils.invokeMethod.
Initially I was using JavaEmbedUtils.javaToRuby for converting the
arguments but as things didn’t work properly this way, I had to
implement a
custom “deep convert” method. I’d guess the same could happen also in
some
cases when calling Java code from Ruby.

marko

Thank you everyone for all your responses! And so complete. Much
appreciated. It gives me a very good starting point :slight_smile:

Cheers
Chris