<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>LolocoJr &#187; jruby ruby drb java</title>
	<atom:link href="http://www.railsguru.com/articles/tag/jruby-ruby-drb-java/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.railsguru.com</link>
	<description>Andy Lo-A-Foe&#039;s blog</description>
	<lastBuildDate>Fri, 09 Jul 2010 12:08:16 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0.1</generator>
		<item>
		<title>JRuby saves the day</title>
		<link>http://www.railsguru.com/articles/2007/11/29/jruby-saves-the-day/</link>
		<comments>http://www.railsguru.com/articles/2007/11/29/jruby-saves-the-day/#comments</comments>
		<pubDate>Thu, 29 Nov 2007 11:19:00 +0000</pubDate>
		<dc:creator>andy</dc:creator>
				<category><![CDATA[Home]]></category>
		<category><![CDATA[ruby]]></category>
		<category><![CDATA[jruby ruby drb java]]></category>

		<guid isPermaLink="false">http://www.railsguru.com/2008/09/24/jruby-saves-the-day</guid>
		<description><![CDATA[So I&#8217;m rewriting yet another subsystem which consists of a mismash of several languages and programmer ego&#8217;s (hardcore C being the largest one, aargh) to what else .. Ruby. Everythings going smoothly. Every line of Ruby code replaces about 10 lines of &#8220;put other language here&#8221; cruft. Life couldn&#8217;t be more beautiful. But then I [...]]]></description>
			<content:encoded><![CDATA[<p>
So I&#8217;m rewriting yet another subsystem which consists of a mismash of several languages and programmer ego&#8217;s (hardcore C being the largest one, aargh) to what else .. Ruby. Everythings going smoothly. Every line of Ruby code replaces about 10 lines of &#8220;put other language here&#8221; cruft. Life couldn&#8217;t be more beautiful. But then I hit the wall, the Java wall. Here I&#8217;m confronted with a full enterprisy Service Manager complete with dependencies on Java-only libs. Now what? I could rewrite the whole thing in Ruby. But then there would be 2 implementations of the same thing to maintain, not to mention reading through Java code, bad.</p>
<p>
Enter <strong>JRuby</strong>. Since the main code blob of this project is captured in a Mongrel plugin I thought about just deploying the whole of Mongrel on JRuby. Unfortunately JRuby Mongrel support was not there yet (<a href="http://mongrel.rubyforge.org/news.html" target="_blank">Mongrel 1.1</a> supports JRuby). So the next best thing was to build some kind bridge between JRuby and Ruby + Mongrel + Plugin. Distributed Ruby (DRb) is a perfect fit:
</p>
<table class="CodeRay">
<tr>
<td class="line_numbers" title="click to toggle" onclick="with (this.firstChild.style) { display = (display == '') ? 'none' : '' }">
<pre>1<tt>
</tt>2<tt>
</tt>3<tt>
</tt>4<tt>
</tt>5<tt>
</tt>6<tt>
</tt>7<tt>
</tt>8<tt>
</tt>9<tt>
</tt><strong>10</strong><tt>
</tt>11<tt>
</tt>12<tt>
</tt>13<tt>
</tt>14<tt>
</tt>15<tt>
</tt>16<tt>
</tt>17<tt>
</tt>18<tt>
</tt>19<tt>
</tt><strong>20</strong><tt>
</tt>21<tt>
</tt>22<tt>
</tt>23<tt>
</tt>24<tt>
</tt>25<tt>
</tt>26<tt>
</tt>27<tt>
</tt>28<tt>
</tt></pre>
</td>
<td class="code">
<pre ondblclick="with (this.style) { overflow = (overflow == 'auto' || overflow == '') ? 'visible' : 'auto' }"><span class="c">#</span><tt>
</tt><span class="c"># server.rb (this)</span><tt>
</tt><span class="c"># jars/big-bad-service.jar</span><tt>
</tt><span class="c">#</span><tt>
</tt><span class="co">APP_ROOT</span> = <span class="co">File</span>.join(<span class="co">File</span>.dirname(<span class="pc">__FILE__</span>), <span class="s"><span class="dl">'</span><span class="k">.</span><span class="dl">'</span></span>)<tt>
</tt><tt>
</tt>require <span class="s"><span class="dl">'</span><span class="k">java</span><span class="dl">'</span></span><tt>
</tt>require <span class="s"><span class="dl">'</span><span class="k">drb</span><span class="dl">'</span></span><tt>
</tt><tt>
</tt>require <span class="s"><span class="dl">&quot;</span><span class="il"><span class="dl">#{</span><span class="co">APP_ROOT</span><span class="dl">}</span></span><span class="k">/jars/big-bad-service.jar</span><span class="dl">&quot;</span></span><tt>
</tt><tt>
</tt><span class="co">BigBadService</span> = com.blah.<span class="co">BigBadService</span><tt>
</tt><tt>
</tt><span class="r">class</span> <span class="cl">JRubyServer</span><tt>
</tt>  <span class="r">def</span> <span class="fu">initialize</span><tt>
</tt>    <span class="iv">@bbs</span> = <span class="co">BigBadService</span>.new<tt>
</tt>    <span class="iv">@bbs</span>.initialize_service<tt>
</tt>  <span class="r">end</span><tt>
</tt><tt>
</tt>  <span class="r">def</span> <span class="fu">bbs_call</span>(param) <tt>
</tt>     <span class="iv">@bbs</span>.bbs_call(param)<tt>
</tt>  <span class="r">end</span><tt>
</tt><span class="r">end</span><tt>
</tt><tt>
</tt><span class="r">if</span> <span class="pc">__FILE__</span> == <span class="gv">$0</span><tt>
</tt>  <span class="co">DRb</span>.start_service <span class="s"><span class="dl">'</span><span class="k">druby://127.0.0.1:6666</span><span class="dl">'</span></span>, <span class="co">JRubyServer</span>.new<tt>
</tt>  <span class="co">DRb</span>.thread.join<tt>
</tt><span class="r">end</span></pre>
</td>
</tr>
</table>
<p>
Execute like <i>ruby server.rb</i>, and then you&#8217;ll have the server listening on port 6666 of localhost. Nice, we can now call our Java service from other Ruby code with this simple snippet:
</p>
<table class="CodeRay">
<tr>
<td class="line_numbers" title="click to toggle" onclick="with (this.firstChild.style) { display = (display == '') ? 'none' : '' }">
<pre>1<tt>
</tt>2<tt>
</tt>3<tt>
</tt>4<tt>
</tt>5<tt>
</tt>6<tt>
</tt></pre>
</td>
<td class="code">
<pre ondblclick="with (this.style) { overflow = (overflow == 'auto' || overflow == '') ? 'visible' : 'auto' }">require <span class="s"><span class="dl">'</span><span class="k">drb</span><span class="dl">'</span></span><tt>
</tt><span class="co">DRb</span>.start_service<tt>
</tt><tt>
</tt>java_bbs = <span class="co">DRbObject</span>.new(<span class="pc">nil</span>, <span class="s"><span class="dl">'</span><span class="k">druby://127.0.0.1:6666</span><span class="dl">'</span></span>)<tt>
</tt><tt>
</tt>puts  java_bbs.bbs_call(<span class="s"><span class="dl">&quot;</span><span class="k">Whack!</span><span class="dl">&quot;</span></span>) <span class="c">#=&gt; &quot;Whacked!&quot;</span></pre>
</td>
</tr>
</table>
<p>
Kewl! Except for one big caveat. As of JRuby 1.0.x Java objects cannot be marshalled correctly so passing them to your Ruby code will cause all sorts of interesting hangs and crashes when you access them concurrently (see <a href="http://jira.codehaus.org/browse/JRUBY-1235" target="_blank">JRUBY-1235</a>). Untill JRuby 1.1 is out you can synchronize all your calls to JRuby and making sure you convert any results to proper Ruby objects before using them elsewhere in your Ruby code.
</p>
<p>
This hack saved me loads of (Java hacking) time!</p>
]]></content:encoded>
			<wfw:commentRss>http://www.railsguru.com/articles/2007/11/29/jruby-saves-the-day/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
