Java/Ruby communication

I am planning to write a Java program and and a Ruby program and have
them intercommunicate, with a bidirectional flow of fairly small
amounts of data from one to to the other and back. I know that I could
achieve this by running the ruby program in JRuby, but it would not run
fast enough (a crude benchmark implies that JRuby executes Ruby about
an order of magnitude slower than I would get using Ruby itself).

The communication could, I suppose, be done with pipes or TCP or even
by reading and writing files. Does anyone have experience of doing
this, and tips or code snippets they could share?

Nigel

[shameless plug]

if you can afford to wait for some time you could use rjni, which
provides transparent, seamless Java/Ruby interaction via JNI. It allows
you to instantiate Java classes from Ruby, call Java methods, implement
Java interfaces with Ruby objects, etc…

I am currently re-implementing several parts of rjni in C for
performance reasons. I cannot give any guarantee whatsoever on the
release date (the fact that I’ve had no connectivity for nearly 3 weeks
hasn’t helped either).

···

On Thu, Aug 14, 2003 at 12:23:11AM +0900, Nigel Gilbert wrote:

I am planning to write a Java program and and a Ruby program and have
them intercommunicate, with a bidirectional flow of fairly small
amounts of data from one to to the other and back. I know that I could
achieve this by running the ruby program in JRuby, but it would not run
fast enough (a crude benchmark implies that JRuby executes Ruby about
an order of magnitude slower than I would get using Ruby itself).

The communication could, I suppose, be done with pipes or TCP or even
by reading and writing files. Does anyone have experience of doing
this, and tips or code snippets they could share?


_ _

__ __ | | ___ _ __ ___ __ _ _ __
'_ \ / | __/ __| '_ _ \ / ` | ’ \
) | (| | |
__ \ | | | | | (| | | | |
.__/ _,
|_|/| || ||_,|| |_|
Running Debian GNU/Linux Sid (unstable)
batsman dot geo at yahoo dot com

Windows without the X is like making love without a partner.
– MaDsen Wikholm, mwikholm@at8.abo.fi

Nigel Gilbert wrote:

I am planning to write a Java program and and a Ruby program and have
them intercommunicate, with a bidirectional flow of fairly small amounts
of data from one to to the other and back. I know that I could achieve
this by running the ruby program in JRuby, but it would not run fast
enough (a crude benchmark implies that JRuby executes Ruby about an
order of magnitude slower than I would get using Ruby itself).

The communication could, I suppose, be done with pipes or TCP or even by
reading and writing files. Does anyone have experience of doing this,
and tips or code snippets they could share?

XML-RPC was fairly easy to set up on both the Java and Ruby end when I
tried it a year ago. Check out xmlrpc4r on RAA.

···


([ Kent Dahl ]/)_ ~ [ http://www.pvv.org/~kentda/ ]/~
))_student_/(( _d L b_/ (pre-) Master of Science in Technology )
( __õ|õ// ) )Industrial economics and technological management(
_
/ö____/ (_engineering.discipline=Computer::Technology)

ruby opens TCPServer
Java opens TCPSocket

Java says “hello\r\n”
ruby says “hiya\r\n”

Voila, communication. Anything else is just protocol :wink: Java doesn’t
support unix sockets or shared memory so there isn’t much you can do
other than network-socket based communication (well, shared
random-access file…).

I guess you could make a JNI based shared-memory or unix socket tool.
ick. The overhead for the Ruby and Java VM’s is enough that I think TCP
based communication is probably okay.

-Brian

···

On Wednesday, August 13, 2003, at 11:36 AM, Mauricio Fernández wrote:

On Thu, Aug 14, 2003 at 12:23:11AM +0900, Nigel Gilbert wrote:

I am planning to write a Java program and and a Ruby program and have
them intercommunicate, with a bidirectional flow of fairly small
amounts of data from one to to the other and back. I know that I
could
achieve this by running the ruby program in JRuby, but it would not
run
fast enough (a crude benchmark implies that JRuby executes Ruby about
an order of magnitude slower than I would get using Ruby itself).

The communication could, I suppose, be done with pipes or TCP or even
by reading and writing files. Does anyone have experience of doing
this, and tips or code snippets they could share?

[shameless plug]

if you can afford to wait for some time you could use rjni, which
provides transparent, seamless Java/Ruby interaction via JNI. It allows
you to instantiate Java classes from Ruby, call Java methods, implement
Java interfaces with Ruby objects, etc…

I am currently re-implementing several parts of rjni in C for
performance reasons. I cannot give any guarantee whatsoever on the
release date (the fact that I’ve had no connectivity for nearly 3 weeks
hasn’t helped either).


_ _

__ __ | | ___ _ __ ___ __ _ _ __
'_ \ / | __/ __| '_ _ \ / ` | ’
) | (| | |
__ \ | | | | | (| | | | |
.__/ _,
|_|/| || ||_,|| |_|
Running Debian GNU/Linux Sid (unstable)
batsman dot geo at yahoo dot com

Windows without the X is like making love without a partner.
– MaDsen Wikholm, mwikholm@at8.abo.fi

“Kent Dahl” kentda+news@stud.ntnu.no wrote in message

Nigel Gilbert wrote:

I am planning to write a Java program and and a Ruby program and have
them intercommunicate, with a bidirectional flow of fairly small amounts
of data from one to to the other and back.
^^^^^
We don’t know if ‘data’ is binary or text only.

XML-RPC was fairly easy to set up on both the Java and Ruby end when I

Pardon my ignorance in this area … but does XML-RPC allow for exchange of
binary data also ?

– shanko

Kent Dahl wrote:

XML-RPC was fairly easy to set up on both the Java and Ruby end when I
tried it a year ago. Check out xmlrpc4r on RAA.

I believe this is noe part of the standard ditribution, no?

Anyways, I just added XML-RPC methods to some blogging software, and it
really is quite simple.

James Britt

Fine… now let’s see you write a Ruby program
that instantiates a Java object and invokes a
method on it…

But that’s a SMOP, right? :slight_smile:

Hal

···

----- Original Message -----
From: “Brian McCallister” mccallister@forthillcompany.com
To: “ruby-talk ML” ruby-talk@ruby-lang.org
Sent: Wednesday, August 13, 2003 12:07 PM
Subject: Re: Java/Ruby communication

ruby opens TCPServer
Java opens TCPSocket

Java says “hello\r\n”
ruby says “hiya\r\n”

Voila, communication. Anything else is just protocol :wink:

Shashank Date wrote:

Pardon my ignorance in this area … but does XML-RPC allow for exchange of
binary data also ?

Yes, sort of. All data must be XML-compliant, so arbitray binary stuff
(such as a jpeg image) must be base-64 (or the like) encoded to ensure
the data payload doesn’t contain troublesome character sequences.

James

···

– shanko

Pardon my ignorance in this area … but does XML-RPC allow for exchange of
binary data also ?

send(data.base64)

data = recv.unbase64

Ari

Hal E. Fulton wrote:

From: “Brian McCallister” mccallister@forthillcompany.com

Voila, communication. Anything else is just protocol :wink:

Fine… now let’s see you write a Ruby program
that instantiates a Java object and invokes a
method on it…

But that’s a SMOP, right? :slight_smile:

That wasn’t part of the requirements in the original post, but, once
sockets are used, couldn’t that happen through XML-RPC or SOAP?

(It’s a separate matter whether one should want one application to know
anything about objects in another application, rather than just passing
messages back and forth.)

James

···

----- Original Message -----

Hal

From: “Brian McCallister” mccallister@forthillcompany.com
To: “ruby-talk ML” ruby-talk@ruby-lang.org
Sent: Wednesday, August 13, 2003 12:07 PM
Subject: Re: Java/Ruby communication

ruby opens TCPServer
Java opens TCPSocket

Java says “hello\r\n”
ruby says “hiya\r\n”

Voila, communication. Anything else is just protocol :wink:

Fine… now let’s see you write a Ruby program
that instantiates a Java object and invokes a
method on it…

batsman@tux-chan:~/src/rjni$ irb --simple-prompt
/home/batsman/usr/lib/ruby/1.8/irb/context.rb:180: warning: method redefined; discarding old use_readline=

require ‘rjni/reflect’
=> true
include RJNI
=> Object
jvm = JVM.new # init jvm
=> #RJNI::JVM:0x407f6e58
JString = Reflect[“java.lang.String”]
=> #<RJNI::Reflect::Class:0x407f46f8 @getMethodsID=getMethods, @field_container=java.lang.String, @vm=#RJNI::JVM:0x407f6e58, @getConstructorsID=getConstructors, @handle=java.lang.String>
o = JString.new “String Passed From Ruby”; nil
=> nil
o.startsWith(“String”).to_b
=> true
o.startsWith(“Sds”).to_b
=> false

startsWith is public boolean java.lang.String.startsWith(java.lang.String).

···

On Thu, Aug 14, 2003 at 02:23:50AM +0900, Hal E. Fulton wrote:

----- Original Message -----


_ _

__ __ | | ___ _ __ ___ __ _ _ __
'_ \ / | __/ __| '_ _ \ / ` | ’ \
) | (| | |
__ \ | | | | | (| | | | |
.__/ _,
|_|/| || ||_,|| |_|
Running Debian GNU/Linux Sid (unstable)
batsman dot geo at yahoo dot com

Linux: Where Don’t We Want To Go Today?
– Submitted by Pancrazio De Mauro, paraphrasing some well-known sales talk

Pardon, I didn’t read carefully enough.

Hal

···

----- Original Message -----
From: “james_b” james_b@neurogami.com
To: “ruby-talk ML” ruby-talk@ruby-lang.org
Sent: Wednesday, August 13, 2003 12:38 PM
Subject: Re: Java/Ruby communication

Hal E. Fulton wrote:

----- Original Message -----
From: “Brian McCallister” mccallister@forthillcompany.com

Voila, communication. Anything else is just protocol :wink:

Fine… now let’s see you write a Ruby program
that instantiates a Java object and invokes a
method on it…

But that’s a SMOP, right? :slight_smile:

That wasn’t part of the requirements in the original post, but, once
sockets are used, couldn’t that happen through XML-RPC or SOAP?

(It’s a separate matter whether one should want one application to know
anything about objects in another application, rather than just passing
messages back and forth.)


Hal Fulton
hal9000@hypermetrics.com

Fine… now let’s see you write a Ruby program
that instantiates a Java object and invokes a
method on it…

batsman@tux-chan:~/src/rjni$ irb --simple-prompt
/home/batsman/usr/lib/ruby/1.8/irb/context.rb:180: warning: method
redefined; discarding old use_readline=

require ‘rjni/reflect’
=> true
include RJNI
=> Object
jvm = JVM.new # init jvm
=> #RJNI::JVM:0x407f6e58
JString = Reflect[“java.lang.String”]
=> #<RJNI::Reflect::Class:0x407f46f8 @getMethodsID=getMethods,
@field_container=java.lang.String, @vm=#RJNI::JVM:0x407f6e58,
@getConstructorsID=getConstructors, @handle=java.lang.String>
o = JString.new “String Passed From Ruby”; nil
=> nil
o.startsWith(“String”).to_b
=> true
o.startsWith(“Sds”).to_b
=> false

startsWith is public boolean
java.lang.String.startsWith(java.lang.String).

Yeah, I knew you could do that… :wink:

My Java is weak, but I’m enthusiastic about this
project (if for no other reason, because it’s a
bridge between Ruby and a well-known technology).

And I think that even if nothing else came out of
the European Ruby Conference, the conference has
probably justified itself.

I’m curious: How mature is this project? I know
you’ve made rapid progress, but how close is it
to being “finished” (whatever that means)?

Cheers,
Hal

···

----- Original Message -----
From: “Mauricio Fernández” batsman.geo@yahoo.com
To: “ruby-talk ML” ruby-talk@ruby-lang.org
Sent: Wednesday, August 13, 2003 12:50 PM
Subject: Re: Java/Ruby communication

http://rubyforge.org/

rjni is still available :slight_smile:

Tom

···

On Wed, 2003-08-13 at 13:57, Hal E. Fulton wrote:

My Java is weak, but I’m enthusiastic about this
project (if for no other reason, because it’s a
bridge between Ruby and a well-known technology).

And I think that even if nothing else came out of
the European Ruby Conference, the conference has
probably justified itself.

I’m curious: How mature is this project? I know
you’ve made rapid progress, but how close is it
to being “finished” (whatever that means)?

startsWith is public boolean
java.lang.String.startsWith(java.lang.String).

Yeah, I knew you could do that… :wink:

My Java is weak, but I’m enthusiastic about this

mine is weak too :slight_smile: I started this because I knew how to do it (and
had the first preliminary version running within the first 3 days),
not because I really wanted to use it :stuck_out_tongue:

Now I’ve switched my goal from providing a binding of JNI for Ruby
(which was mostly finished in the first week of coding) to allowing
full-featured, transparent usage of Java classes and objects from Ruby.
I have some code that makes the other way around work but there’s still
a problem related to ruby’s lack of thread safety (ie. the interpreter
not being reentrant).

project (if for no other reason, because it’s a
bridge between Ruby and a well-known technology).

And I think that even if nothing else came out of
the European Ruby Conference, the conference has
probably justified itself.

I’m curious: How mature is this project? I know
you’ve made rapid progress, but how close is it
to being “finished” (whatever that means)?

I was really close to releasing a beta version (mostly feature-complete)
some 3 weeks ago. After doing some benchmarks, I dropped by #ruby-lang to
see what performance would be expected from rjni (so as not to disappoint
possible users :slight_smile: and I realized I had better not release then, as
people would forever associate rjni with “dead-slow”: on my machine,
I was getting ~2e4 method calls/s whereas Ruby was doing some 2e6 (method
dispatching is very slow cause I have to take care of method overloading,
type conversions, etc).

As I was about to re-implement some parts in C, Real Life distracted me
and I lost my Internet connectivity, which I recovered today (you might
have noticed by the relative lack of postings from me in ruby-talk :-).

I will soon start porting the method dispatching routines to C. As I
have the Ruby code to guide me it should be fairly fast, but OTOH
writing stuff in C will be much more painful for sure :stuck_out_tongue:

···

On Thu, Aug 14, 2003 at 02:57:01AM +0900, Hal E. Fulton wrote:


_ _

__ __ | | ___ _ __ ___ __ _ _ __
'_ \ / | __/ __| '_ _ \ / ` | ’ \
) | (| | |
__ \ | | | | | (| | | | |
.__/ _,
|_|/| || ||_,|| |_|
Running Debian GNU/Linux Sid (unstable)
batsman dot geo at yahoo dot com

The good thing about standards is that there are so many to choose from.
– Andrew S. Tanenbaum