<?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>Engine Yard Blog &#187; Nick Sieger</title>
	<atom:link href="http://www.engineyard.com/blog/author/nicksieger/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.engineyard.com/blog</link>
	<description></description>
	<lastBuildDate>Tue, 07 Feb 2012 19:36:04 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.1</generator>
		<item>
		<title>Concurrency in JRuby</title>
		<link>http://www.engineyard.com/blog/2011/concurrency-in-jruby/</link>
		<comments>http://www.engineyard.com/blog/2011/concurrency-in-jruby/#comments</comments>
		<pubDate>Fri, 22 Jul 2011 21:22:05 +0000</pubDate>
		<dc:creator>Nick Sieger</dc:creator>
				<category><![CDATA[Open Source]]></category>
		<category><![CDATA[Ruby]]></category>
		<category><![CDATA[Technology]]></category>
		<category><![CDATA[concurrency]]></category>
		<category><![CDATA[JRuby]]></category>

		<guid isPermaLink="false">http://www.engineyard.com/blog/?p=9565</guid>
		<description><![CDATA[<p><em>This is a recap of my talk on the same subject at <a href="http://emrubyconf.com/">EventMachine RubyConf</a> in Baltimore on the final day of RailsConf 2011.</em></p>
<p>Concurrency is a hotly debated subject in the Ruby community. Shared state or shared nothing? Threads or Events? Sync or Async? The fact that the standard Ruby interpreter does not provide multiple-core saturation without resorting to process management clouds the issue, causing developers to constantly evaluate new approaches for using all available CPUs.</p>
<p>JRuby enters the discussion, sporting its use of native (kernel) threads, allowing single-process access to all of your cores. Is true concurrently-executing Ruby code obtained simply by switching to JRuby? Before you think that JRuby will make your threaded code run faster, we need to take a step back and explain.</p>
<h1>Mental Model</h1>
<p>[caption id="attachment_9567" align="alignleft" width="240" caption="model_team_canada by Dept of Energy Solar Decathlon, on Flickr"]<a href="http://www.flickr.com/photos/solar_decathlon/4524503892/"><img class="size-full wp-image-9567" title="model" src="http://www.engineyard.com/blog/wp-content/uploads/model.jpg" alt="model_team_canada by Dept of Energy Solar Decathlon, on Flickr" width="240" height="159" /></a>[/caption]</p>
<p>First, a new mental model is needed. Although JRuby is just another Ruby implementation, it's also a new tool running on a completely different VM, the Java Virtual Machine, which has performance characteristics much different than Ruby's VM. These characteristics vary due to the use of native threads compared to green threads, the JVM's sophisticated garbage collection facilities, and most importantly JRuby's own codebase. So your assumptions about how code works do not carry across Ruby implementations. Code that previously ran slow may now be fast and vice versa.</p>
<h2>Uncertainty</h2>
<p>Adding to the uncertainty of the situation is the unpredictability of native threads. Have you ever seen "should never happen" comments in code, where some programmer was convinced that a branch of code was completely unreachable? If the code branches based on a piece of shared state corrupted by multiple threads scheduled across multiple cores, the impossible code just might end up executing.</p>
<pre lang="c" escaped="true">/* ruby/struct.c */
static VALUE rb_struct_equal(VALUE s, VALUE s2) {
    /* ... */
    if (RSTRUCT_LEN(s) != RSTRUCT_LEN(s2)) {
        rb_bug("inconsistent struct"); /* should never happen */
    }
    /* ... */
}</pre>
<p>Here's a hypothetical example running on some fictitious native-threaded, optimizing Ruby VM. Say we have this singleton object that's expensive to create, so the programmer wrote it to be constructed lazily.</p>
<pre lang="ruby" escaped="true">class ExpensiveToCreate
  def self.instance
    @instance ||= ExpensiveToCreate.new
  end
end</pre>
<p>As we all know, the "or-equals" operator is really just sugar for the following code:</p>
<pre lang="ruby" escaped="true">class ExpensiveToCreate
  def self.instance
    unless defined?(@instance)
      @instance = ExpensiveToCreate.new
    end
    @instance
  end
end</pre>
<p>Now let's play the role of the optimizing VM. Let's say that this VM decides to inline the <tt>new</tt> method like so:</p>
<pre lang="ruby" escaped="true">class ExpensiveToCreate
  def self.instance
    unless defined?(@instance) # Line 3
      @instance = ExpensiveToCreate.allocate
      @instance.initialize     # Line 5
    end
    @instance
  end
end</pre>
<p>What if two threads try to initialize the instance at the same time? Trouble! We have the potential for a race: the first thread on line 5 is not finished performing the expensive initialization, but the <tt>@instance</tt> variable has already been defined, so the second thread will happily return the uninitialized instance and try to use it. (Some of you will recognize this as a variation on the <a title="Double-checked locking" href="http://en.wikipedia.org/wiki/Double-checked_locking">Double-checked locking</a> problem.)</p>
<p>So does this mean that we need to be extra vigilant with our code, sprinkling it with mutex blocks everywhere? Will it become an unreadable, unmaintainable mess? <span id="more-9565"></span>Certainly not, as long as we follow a simple rule:</p>
<h2>Avoid shared, mutable state.</h2>
<p>This includes lazy initialization, which is mutating shared state at the time it is first accessed.</p>
<p>The consequences of programming with real threads are difficult to conceptualize at first, especially if you're used to Ruby's green threads or Ruby 1.9's global interpreter lock (GIL). Consider this code:</p>
<pre lang="ruby" escaped="true">data = []
M.times do |m|
  threads &lt;&lt; Thread.new do
    N.times do |n|
      data &lt;&lt; m * n
    end
  end
end</pre>
<p>What happens to the <tt>data</tt> array after all threads have finished? Under Ruby 1.8 and Ruby 1.9, we always get an array of integers of size <tt>M * N</tt>. There may be a little randomness in the ordering of the entries, but otherwise the array is intact and well-behaved.</p>
<p>Under JRuby, arrays (as well as strings, hashes and other core library data structures) are not safe for mutation by multiple threads. So when we run the code above with JRuby, the array and its internals become corrupted. The array's size is frequently less than <tt>M * N</tt>, and what's more, we often observe some of the entries are <tt>nil</tt> rather than the integers we expect. Sometimes we'll encounter a <tt>ConcurrencyError</tt> raised as well.</p>
<p>This uncertainty can be the cause of some nasty, hard-to-pinpoint bugs in your code. So if your code works well with Ruby but blows up with unexpected <tt>nil</tt>s or otherwise unexplained behavior, you can at least start to point the blame at threaded code that mutates state.</p>
<p>What about metaprogramming in the presence of threads? Can we corrupt the interpreter by defining classes and/or methods from many threads at once? Fortunately the answer here is no. JRuby explicitly takes steps to ensure that class and method definition are properly synchronized internally. Also, since class variables are frequently used for sharing state between objects, they are synchronized as well.</p>
<h2>Using Native Threads</h2>
<p>As you might expect, using native threads in JRuby is as simple as working with the regular Ruby <tt>Thread</tt> class. (Note that there are <a title="Headius" href="http://blog.headius.com/2008/02/ruby-threadraise-threadkill-timeoutrb.html">some caveats</a>). For example, you can easily offload some computation to the background:</p>
<pre lang="ruby" escaped="true">require 'java'
@count = java.util.concurrent.atomic.AtomicInteger.new

def send_email(message)
  Thread.new do
    # send the message
    puts "sent #{@count.incrementAndGet} emails"
  end
end

send_email("hello")</pre>
<p>For systems with large volumes of email, this naive approach may not work well. Native threads carry a bigger initialization cost and memory overhead than green threads, so JRuby normally cannot support more than about 10,000 threads.</p>
<pre escaped="true">$ jruby -e '100_000.times { Thread.new { sleep 1 } }'
ThreadError: unable to create new native thread</pre>
<p>To work around this, we can use a thread pool. Using JRuby's Java integration, we can easily access the built-in <tt>Executor</tt> classes:</p>
<pre lang="ruby" escaped="true">java_import java.util.concurrent.Executors

def send_email(message, executor)
  executor.submit do
    # send the message
    puts "sent #{@count.incrementAndGet} emails"
  end
end

executor = Executors.newCachedThreadPool
send_email("hello", executor)

executor = Executors.newFixedThreadPool(2)
10.times do
  send_email("hello", executor)
end</pre>
<p>Here we're using two thread pools. The first, the "cached" thread pool, is a general-purpose pool that grows as needed by demand and frees up system resources by releasing threads after they have been idle. The second example uses a fixed pool of two threads for when you want a place hard limit on the amount of background processing.</p>
<p>Java's <tt>java.util.concurrent</tt> package has a number of useful utilities like these for concurrent programming including locks, semaphores, latches, queues, concurrent lists and maps, and atomic objects such as the <tt>AtomicInteger</tt> used above. And they're all trivially available to you via JRuby.</p>
<h2>Concurrency with Actors</h2>
<p>The shift in thinking around concurrent programming in recent years has been around the development of higher-level abstractions. This arose out of the realization that lower-level coding with fine-grained locks is hard: it's error-prone, makes code less readable and maintainable, and is difficult to troubleshoot. The upside of this is that we get to leave the hard stuff to the library programmers who create the implementations of these abstractions.</p>
<p>Of all the higher level ways of doing concurrent programming, the <a title="Actor model on Wikipedia" href="http://en.wikipedia.org/wiki/Actor_model">actor model</a> has become preferred in recent years coincident with the rise in popularity of Erlang where the actor model has been proven to work well.</p>
<p>Ruby has had a <a title="On Ruby: Ruby concurrency with Actors" href="http://on-ruby.blogspot.com/2008/01/ruby-concurrency-with-actors.html">number of Actor frameworks</a> for some time, including a recent entry, <a title="Celluloid, an actor framework for Ruby" href="http://celluloid.github.com/">Celluloid</a>. (Be sure to watch Celluloid's creator Tony Arcieri in a <a title="Crash course on Celluoid on Vimeo" href="http://vimeo.com/23974548">screencast for EMRubyConf</a>.) While these all work great on JRuby, again I'd like to focus on two Java libraries that are just as accessible from JRuby but go above and beyond what is currently possible with the pure Ruby libraries.</p>
<h3>Jetlang/Jretlang</h3>
<p><a title="Jetlang on Google code" href="http://code.google.com/p/jetlang/">Jetlang</a> isn't quite a full actor library, but instead claims to be "designed specifically for high performance in-memory messaging". (<a title="Jretlang on Github" href="https://github.com/reevesg/jretlang">Jretlang</a> is the JRuby wrapper around Jetlang). The main primitives in Jetlang are Fibers and Channels. Here's a "Hello World" example:</p>
<pre lang="ruby" escaped="true">require 'java'
require 'jretlang'

fiber = JRL::Fiber.new
channel = JRL::Channel.new
fiber.start

channel.subscribe_on_fiber(fiber) do |msg|
  puts msg
end

channel.publish "Hello"</pre>
<p>If you want a toolkit for building a message-passing framework in your application, give Jretlang a look.</p>
<h3>Akka</h3>
<p><a title="Akka home page" href="http://akka.io/">Akka</a> is a platform and toolkit for concurrent, scalable, and fault-tolerant systems. It has many features inspired by Erlang, including many flavors of actors and fault-tolerant supervisor hierarchies. Here's the simplest-possible <a title="Ruby on Akka on Github" href="https://github.com/danielribeiro/RubyOnAkka/">Akka-in-Ruby</a> example:</p>
<pre lang="ruby" escaped="true">require 'akka'

class HelloWorld
  def hi
    puts "hello actor world"
  end
end

Actors.actorOf(HelloWorld.new).hi
puts "initiating shutdown..."
Actors.delayedShutdown 1</pre>
<p>When run, this example prints:</p>
<pre escaped="true">$ ./runner.sh lib/everything_is_an_actor.rb
[...lots of akka logging...]
initiating shutdown...
hello actor world</pre>
<p>Of note here is that we're creating an actor reference to the <tt>HelloWorld</tt> object and sending it the <tt>#hi</tt> message, but that does call the method immediately. Instead, the message is routed through Akka and back to the object later.</p>
<h2>JRuby Makes Concurrency Easy</h2>
<p>Once again, the long and short of the concurrency story on JRuby is buttressed by the ease of which you can access both the best of Ruby and Java libraries. Go forth and glue together concurrency-heavy applications with JRuby, and please share them with us!
<p><a href="http://www.engineyard.com/blog"><img height="98" width="61" title="logo-engineyard" alt="" class="attachment-post-thumbnail wp-post-image" src="http://www.engineyard.com/blog/wp-content/uploads/logo-engineyard.png"/></a></p>
]]></description>
		<wfw:commentRss>http://www.engineyard.com/blog/2011/concurrency-in-jruby/feed/</wfw:commentRss>
		<slash:comments>11</slash:comments>
		</item>
		<item>
		<title>Taking Stock of JRuby Web Servers</title>
		<link>http://www.engineyard.com/blog/2011/taking-stock-jruby-web-servers/</link>
		<comments>http://www.engineyard.com/blog/2011/taking-stock-jruby-web-servers/#comments</comments>
		<pubDate>Thu, 24 Mar 2011 17:41:52 +0000</pubDate>
		<dc:creator>Nick Sieger</dc:creator>
				<category><![CDATA[Open Source]]></category>
		<category><![CDATA[Ruby]]></category>
		<category><![CDATA[aspen]]></category>
		<category><![CDATA[jboss netty]]></category>
		<category><![CDATA[JRuby]]></category>
		<category><![CDATA[jruby on appcloud]]></category>
		<category><![CDATA[jruby-rack]]></category>
		<category><![CDATA[kirk]]></category>
		<category><![CDATA[mizuno]]></category>
		<category><![CDATA[torquebox]]></category>
		<category><![CDATA[trinidad]]></category>
		<category><![CDATA[warbler]]></category>
		<category><![CDATA[web servers]]></category>

		<guid isPermaLink="false">http://www.engineyard.com/blog/?p=7696</guid>
		<description><![CDATA[<p><a href="http://www.engineyard.com/blog/wp-content/uploads/jruby-logo.png"><img class="alignright size-full wp-image-4623" title="jruby-logo" src="http://www.engineyard.com/blog/wp-content/uploads/jruby-logo.png" alt="" /></a>One of the more frequent questions that new JRubyists ask is "Which server should I use to build and deploy my application?" I'm glad you asked! But first, I need to know a little more about you, your environment, and your deployment needs. Do you have an existing Java environment? Do you prefer to make sure you're using the same components for both development and production? Do you like rolling your own servers? Do you like experimentation? Do you like running background services in the same process? How about zero-downtime redeployments? Read on, we have something for everybody.</p>
<h2>You have an existing Java web server</h2>
<p>Maybe your organization is already Java-heavy, or you have an operations team that's familiar with operating an existing Java server. That's fine! In that case, <a rel="nofollow" href="https://github.com/nicksieger/jruby-rack">JRuby-Rack</a> is what you need.</p>
<p>JRuby-Rack is the foundation that enables Ruby applications to run on virtually any existing Java web server. Put simply, JRuby-Rack is a bridge from the Java Servlet API to Ruby's Rack API. So if you have a Servlet container (and pretty much every widely-used Java server in existence has one), you can run any Rack application with JRuby-Rack. We regularly test JRuby-Rack with Tomcat 6/7, Jetty 6/7, JBoss 5/6, Resin 4, and GlassFish 3, so chances are good that your application will run fine on your server.</p>
<p>JRuby-Rack is also the hidden engine behind <a rel="nofollow" href="http://caldersphere.rubyforge.org/warbler">Warbler</a>, the tool that quickly assembles a full <tt>.war</tt> file from your Ruby application. When you run <tt>warble</tt>, Warbler includes a copy of JRuby-Rack in the final assembled archive and wires up the <tt>RackFilter</tt> Servlet filter that delegates requests to your Ruby application.</p>
<p>In addition to managing the lifecycle of your Ruby application inside the Java web server, JRuby-Rack has accumulated a number of useful extensions over its three-year life. Here are three.<span id="more-7696"></span></p>
<h3>Response streaming</h3>
<p>Say you'd like to do some early flushing of your response. With JRuby-Rack, simply set the <tt>Transfer-Encoding: chunked</tt> header in the response, and JRuby-Rack will flush each element of the response body individually. (A word of caution: <a rel="nofollow" href="http://tenderlovemaking.com/2011/03/03/rack-api-is-awkward/">Aaron Patterson reports</a> how the Rack API doesn't support this very well, so make sure you know what you're doing.) Here's a <a rel="nofollow" href="https://github.com/nicksieger/jruby-rack/blob/master/examples/sinatra/lib/stream.rb">Sinatra example</a> that perpetually reports the server time in a series of JSON chunks:</p>
<pre lang="ruby" escaped="true">class ServerTime
  def initialize(response)
    @boundary = 'MultipartBody'
    response['Content-Type'] = "multipart/mixed; boundary=\"#{@boundary}\""
    response['Transfer-Encoding'] = 'chunked'
  end

  def chunk(content_type, body)
    "--#{@boundary}\nContent-Type: #{content_type}\n\n#{body}\n"
  end

  def each
    loop do
      yield chunk("application/json", "{\"currentTime\":\"#{Time.now.strftime '%H:%M:%S'}\"}")
      sleep 2
    end
  end
end

get '/servertime' do
  ServerTime.new(response)
end</pre>
<h3>A more efficient X-Sendfile</h3>
<p>You can also use a Ruby File or Tempfile object as the response body, and JRuby-Rack will write it to the response using the <a rel="nofollow" href="http://download.oracle.com/javase/6/docs/api/java/nio/channels/FileChannel.html#transferTo(long,%20long,%20java.nio.channels.WritableByteChannel)">Java NIO <tt>FileChannel#transferTo</tt></a> method, which, depending on the OS, can transfer bytes directly from the filesystem cache to the target channel without actually copying them. JRuby-Rack will close the file after the request is finished so you don't have to worry about leaking the resource.</p>
<h3>Interaction with Servlets</h3>
<p>You can use the JRuby-Rack <tt>RackFilter</tt> alongside other servlets or JSPs in a Java web application. For example, you can internally redirect from Ruby to another servlet. JRuby-Rack adds a custom <tt>#forward_to</tt> method to Rails' <tt>ActionController</tt>:</p>
<pre lang="ruby" escaped="true">class JspController &lt; ApplicationController
  def index
    servlet_request['message'] = 'Hello from Rails!'
    forward_to '/jsp/index.jsp'
  end
end</pre>
<p>Inside <tt>public/jsps/index.jsp</tt>, we can consume the <tt>message</tt> attribute as follows:</p>
<pre lang="xml" escaped="true">&lt;html&gt;
  &lt;head&gt;&lt;title&gt;Rails with JSPs&lt;/title&gt;&lt;/head&gt;
  &lt;body&gt;
    &lt;h1&gt;&lt;%= request.getAttribute("message") %&gt;&lt;/h1&gt;
  &lt;/body&gt;
&lt;/html&gt;</pre>
<p>With this technique it's easy to introduce Rails as the routing and controller layer on top of an existing Java web application.</p>
<p>We'll be writing much more about integrating Ruby alongside Java web applications in the near future, so watch this space.</p>
<h2>You want to distribute your application</h2>
<p>Warbler comes with a feature dubbed <tt>executable war</tt>, which builds a <tt>.war</tt> file that can be deployed in a container or run as an self-contained executable. Warbler embeds Winstone, a small servlet container, in the archive that self-extracts and listens at <tt>localhost:8080</tt> when run:</p>
<pre escaped="true">$ warble executable war
rm -f sinatra.war
Creating sinatra.war
$ java -jar sinatra.war
[Winstone 2011/03/07 12:40:58] - Beginning extraction from war file
[Winstone 2011/03/07 12:40:59] - WARNING: The Servlet 2.4/2.5 spec XSD was unavailable inside the winstone classpath. Will be retrieved from the web if required (slow)
[Winstone 2011/03/07 12:40:59] - No webapp classes folder found - /private/var/folders/CA/CAMqlEcnEmSLCKPW5k22oE+++TI/-Tmp-/winstone5711860033631390551webroot/sinatra.war/WEB-INF/classes
[Winstone 2011/03/07 12:41:05] - HTTP Listener started: port=8080
[Winstone 2011/03/07 12:41:05] - Listener winstone.ajp13.Ajp13Listener not found / disabled - ignoring
[Winstone 2011/03/07 12:41:05] - Listener winstone.ssl.HttpsListener not found / disabled - ignoring
[Winstone 2011/03/07 12:41:05] - Winstone Servlet Engine v0.9.10 running: controlPort=disabled</pre>
<p>The executable archive is completely self-contained, so you can distribute it and run it anywhere a Java Virtual Machine is installed.</p>
<p>Up to this point, we've learned that Warbler and JRuby-Rack are great for deploying across a wide range of environments, but what about development? When you're coding a new application, it would be mighty painful  to generate a .war file and deploy it after every change. Rubyists are used to firing up a web server, writing code, and hitting the refresh button. JRuby certainly supports this mode, but with a different cast of characters than you know from the C Ruby world. Let's take a look at five JRuby servers that fill different needs.</p>
<h2>You want uniformity across environments</h2>
<p>Unlike WEBrick, the server included in Ruby's standard library, <a href="https://github.com/calavera/trinidad">Trinidad</a> by David Calavera is equally suited for development and production use. Written in pure Ruby, Trinidad is built by employing JRuby-Rack and <a href="http://tomcat.apache.org">Apache Tomcat</a> as embedded Java libraries. To try out Trinidad, simply <tt>gem install trinidad</tt> and start the server with the <tt>trinidad</tt> command.</p>
<p>Trinidad is the most mature of the JRuby web servers, and most familiar in feel to other Ruby servers. <tt>trinidad -e production -p 3001</tt> does what you expect. In addition, JRuby-specific options like <tt>--threadsafe</tt>, <tt>--classes</tt>, and <tt>--jars</tt> are easily accessible.</p>
<p>You can also customize Trinidad with extensions. Currently there are <a rel="nofollow" href="https://github.com/calavera/trinidad_lifecycle_extension">lifecycle</a>, <a rel="nofollow" href="https://github.com/calavera/trinidad_daemon_extension">daemon</a>, <a rel="nofollow" href="https://github.com/calavera/trinidad_hotdeploy_extension">hotdeploy</a>, and <a rel="nofollow" href="https://github.com/calavera/trinidad_sandbox_extension">sandbox</a> extensions available.  Also notable is the brand new <a rel="nofollow" href="https://github.com/bdewitt/trinidad_scheduler_extension">scheduler</a> extension, developed by Brandon DeWitt, and is the first extension developed by someone other than David, showing growth in the Trinidad community beyond its primary maintainer.</p>
<h2>You want a light, customizable server</h2>
<p><a rel="nofollow" href="https://github.com/matadon/mizuno">Mizuno</a>, by Don Werve, is a great example of how easy it is to leverage an existing Java library in a very small amount of code. Mizuno is advertised as "a set of Jetty-powered running shoes for JRuby and Rack", and clocks in at a svelte 335 lines of pure Ruby code. Size does not matter in this case though, as Mizuno manages to turn in good performance numbers as well. Try out the Mizuno gem and if you're thinking of writing a custom server, take a look at the Mizuno codebase as a starting point.</p>
<h2>You like playing with asynchronous APIs</h2>
<p>Kevin Williams has been hacking on his server, <a rel="nofollow" href="https://github.com/kevwil/aspen">Aspen</a>, for a while now, and has recently kicked it up a notch. Aspen's approach is to mimic the design of the Ruby web server Thin using the <a rel="nofollow" href="http://www.jboss.org/netty">JBoss Netty</a> asynchronous event-driven network application framework. If a JVM-based, Ruby-flavored Node.js is up your alley, get in touch with Kevin and take a look at Aspen.</p>
<h2>You want a full-stack solution</h2>
<p><a rel="nofollow" href="http://torquebox.org/">TorqueBox</a>, from the fine folks at RedHat, provides an all-in-one solution by building on top of the JBoss application server. According to the <a rel="nofollow" href="http://torquebox.org/faq/">FAQ</a>:</p>
<blockquote><p>[TorqueBox] attempts to go beyond providing web-centric services (supporting Rails, Rack, Sinatra, etc), to also expose other enterprise-grade services to Ruby applications.</p></blockquote>
<p>One of the niceties of TorqueBox's full-stack solution is integrated messaging and asynchronous tasks. Backgrounding is as simple as creating a task class in <tt>app/tasks/email_task.rb</tt>:</p>
<pre lang="ruby" escaped="true">class EmailTask &lt; TorqueBox::Messaging::Task
  def send_hello(payload)
    puts "EmailTask: sent hello email with #{payload.inspect}"
  end
end</pre>
<p>and invoking it:</p>
<pre lang="ruby" escaped="true">class HelloController &lt; ApplicationController
  def index
    EmailTask.async(:send_hello, params[:message])
  end
end</pre>
<p>This is much better than simply launching a background thread or even a subprocess, because behind the scenes TorqueBox creates a queue for your task on the internal HornetQ message bus and invokes an instance of the task class on the receiving end of the queue. If the server happens to crash, you're much less likely to lose the task.</p>
<p>So if you're familiar with JBoss or like the idea of having a wider platform to build messaging and other background infrastructure all in Ruby, TorqueBox is definitely worth a look.</p>
<h2>You want zero-downtime deploys</h2>
<p><a rel="nofollow" href="https://github.com/strobecorp/kirk">Kirk</a>, at two months old the youngest entry in the JRuby server category, is brought to us by Carl Lerche of Rails core and Bundler fame.</p>
<p>The key use case for Kirk is zero-downtime deploys in the same fashion as Passenger or Unicorn. Applications deployed in Kirk can be redeployed either by command-line or by updating files that Kirk is configured to watch.</p>
<p>Kirk deployments are configured with a <tt>Kirkfile</tt> (facepalm!). Here's a <a rel="nofollow" href="https://github.com/nicksieger/jruby-rack/blob/master/examples/Kirkfile">simple example</a>:</p>
<pre lang="ruby" escaped="true">rack "rails3/config.ru" do
  listen 9090
end

rack "sinatra/config.ru" do
  listen 9091
end

rack "camping/config.ru" do
  listen 9092
end</pre>
<p>As you surely guessed, this starts up three socket listeners on ports 9090-9092, all inside a single JVM. Each application is redeployable on its own, or all at once if you configure them to watch the same file.</p>
<p>If you redeploy while the application is servicing requests, Kirk warms up the new version of the application and swaps it in atomically. In a <a rel="nofollow" href="https://github.com/nicksieger/jruby-rack/blob/master/examples/sinatra/Kirkfile">stress test</a>, I compared response rates between a stable application and one that continuously redeploys every two seconds. The stable server performed at a rate of 285 replies per second:</p>
<pre escaped="true">$ httperf --port 9292 --uri /env --num-conns 500 --num-calls 20
httperf --client=0/1 --server=localhost --port=9292 --uri=/env --send-buffer=4096 --recv-buffer=16384 --num-conns=500 --num-calls=20
httperf: warning: open file limit &gt; FD_SETSIZE; limiting max. # of open files to FD_SETSIZE
Maximum connect burst length: 1

Total: connections 500 requests 10000 replies 10000 test-duration 34.972 s

Connection rate: 14.3 conn/s (69.9 ms/conn, &lt;=1 concurrent connections)
Connection time [ms]: min 61.1 avg 69.9 max 357.4 median 66.5 stddev 15.9
Connection time [ms]: connect 0.3
Connection length [replies/conn]: 20.000

Request rate: 285.9 req/s (3.5 ms/req)
Request size [B]: 65.0

Reply rate [replies/s]: min 220.8 avg 284.9 max 305.6 stddev 32.6 (6 samples)
Reply time [ms]: response 3.5 transfer 0.0
Reply size [B]: header 113.0 content 6893.0 footer 0.0 (total 7006.0)
Reply status: 1xx=0 2xx=10000 3xx=0 4xx=0 5xx=0</pre>
<p>The coutinuous-redeploy version responded to every request but dropped to a rate of 90 replies per second. Obviously redeploys aren't free, but as long as you don't deploy new code every two seconds (!), you won't see a noticeable slowdown.</p>
<pre escaped="true">$ httperf --port 9292 --uri /env --num-conns 500 --num-calls 20
httperf --client=0/1 --server=localhost --port=9292 --uri=/env --send-buffer=4096 --recv-buffer=16384 --num-conns=500 --num-calls=20
httperf: warning: open file limit &gt; FD_SETSIZE; limiting max. # of open files to FD_SETSIZE
Maximum connect burst length: 1

Total: connections 500 requests 10000 replies 10000 test-duration 111.636 s

Connection rate: 4.5 conn/s (223.3 ms/conn, &lt;=1 concurrent connections)
Connection time [ms]: min 63.7 avg 223.3 max 5839.1 median 161.5 stddev 337.7
Connection time [ms]: connect 1.6
Connection length [replies/conn]: 20.000

Request rate: 89.6 req/s (11.2 ms/req)
Request size [B]: 65.0

Reply rate [replies/s]: min 8.4 avg 90.2 max 169.8 stddev 34.7 (22 samples)
Reply time [ms]: response 11.1 transfer 0.0
Reply size [B]: header 113.0 content 6908.0 footer 0.0 (total 7021.0)
Reply status: 1xx=0 2xx=10000 3xx=0 4xx=0 5xx=0</pre>
<h2>Bright Future</h2>
<p>There's certainly a swarm of activity in JRuby web server land, and you can count on seeing a few of these options appear in future <a href="http://docs.engineyard.com/beta/signup-jruby">JRuby offerings on the Engine Yard AppCloud</a>.</p>
<p>If you haven't yet tried running your application with JRuby, stay tuned. My session at <a href="http://en.oreilly.com/rails2011/public/schedule/detail/18555">RailsConf 2011 covers porting to JRuby</a> and you can rest assured that we'll cover the details in a future article as well.</p>
<p>Let us know in the comments about your preferred way of deploying JRuby applications, or tell us frankly what's holding you back from deploying with JRuby so we can correct those issues!
<p><a href="http://www.engineyard.com/blog"><img height="98" width="61" title="logo-engineyard" alt="" class="attachment-post-thumbnail wp-post-image" src="http://www.engineyard.com/blog/wp-content/uploads/logo-engineyard.png"/></a></p>
]]></description>
		<wfw:commentRss>http://www.engineyard.com/blog/2011/taking-stock-jruby-web-servers/feed/</wfw:commentRss>
		<slash:comments>19</slash:comments>
		</item>
		<item>
		<title>Connecting the Worlds of Java and Ruby with JRuby Webinar</title>
		<link>http://www.engineyard.com/blog/2010/connecting-the-worlds-of-java-and-ruby-with-jruby-webinar/</link>
		<comments>http://www.engineyard.com/blog/2010/connecting-the-worlds-of-java-and-ruby-with-jruby-webinar/#comments</comments>
		<pubDate>Mon, 20 Dec 2010 20:13:50 +0000</pubDate>
		<dc:creator>Nick Sieger</dc:creator>
				<category><![CDATA[Open Source]]></category>
		<category><![CDATA[Ruby]]></category>
		<category><![CDATA[Technology]]></category>
		<category><![CDATA[JRuby]]></category>
		<category><![CDATA[Webinar]]></category>

		<guid isPermaLink="false">http://www.engineyard.com/blog/?p=5367</guid>
		<description><![CDATA[<p>Thanks to everyone who took the time to watch our latest webinar on Ruby, JRuby and Rails. The full video recording is now available for viewing, as are the presentation slides. We realize one hour is not sufficient time to do any of the three topics justice, so we're planning to do a series of upcoming webinars to delve deeper into specific topics. Please let us know in the comments section about specific topics you'd like addressed.</p>
<div class="video">
<div class="center"><object classid="clsid:d27cdb6e-ae6d-11cf-96b8-444553540000" width="600" height="370" codebase="http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=6,0,40,0"><param name="allowfullscreen" value="true" /><param name="allowscriptaccess" value="always" /><param name="wmode" value="opaque" /><param name="src" value="http://vimeo.com/moogaloop.swf?clip_id=17963325&amp;server=vimeo.com&amp;show_title=1&amp;show_byline=1&amp;show_portrait=0&amp;color=ffffff&amp;fullscreen=1" /><embed type="application/x-shockwave-flash" width="600" height="300" src="http://vimeo.com/moogaloop.swf?clip_id=17963325&amp;server=vimeo.com&amp;show_title=1&amp;show_byline=1&amp;show_portrait=0&amp;color=ffffff&amp;fullscreen=1" wmode="opaque" allowscriptaccess="always" allowfullscreen="true"></embed></object></div>
<p>The slides from the presentation <a href="http://assets.engineyard.com/pdfs/webinar-slides-connecting-java-ruby.pdf">can also be downloaded</a>.</p>
<p>We ran out of time before getting to all your questions. Here are my responses to the questions we weren't able to field during the live webinar.</p>
<p><em>What are the best communities or places to go to ask questions and learn more about JRuby on Rails?</em></p>
<p>Certainly JRuby.org and the JRuby mailing lists, and to some extent the Rails mailing lists if the questions are not too JRuby-specific. We on the JRuby team also monitor JRuby-related questions on StackOverflow.com, and there is a volume of questions there already.</p>
<p>The JRuby team also is usually present in the IRC room #jruby on irc.freenode.net, so feel free to stop by there to chat. You can also follow <a href="http://twitter.com/jruby">@jruby on Twitter</a> to stay on top of important updates.</p>
<p><em>Do you have any prediction or comments on Ruby/JRuby adoption in the enterprise in the next couple of years?</em></p>
<p>I'm personally bullish on JRuby adoption and consider it part of my mission on the JRuby team to make it happen. It's hard to say in exact terms how it will increase, but a number of factors, such as the growing acceptance and maturity of Ruby and Rails, the participation at JRuby-related events, the new Using JRuby book, and the availability of knowledge, training, and documentation around Ruby and JRuby should all play a role.</p>
<p>We also typically have a hard time knowing exactly how many people are using JRuby, because of course they're not required to tell us. We are usually surprised at conferences and meetups to hear of another organization successfully using JRuby in their own projects.</p>
<p>Any business looking to try to inject new life into their Java-based projects should look at JRuby to help accelerate development of their systems and take advantage of the array of innovations the Ruby and Rails communities have brought to web development and cloud computing.</p>
<p><em>How is the gem support in JRuby? For example, can I use Nokogiri?</em></p>
<p>Gem support is generally good. For Nokogiri in particular, check out the 1.5 beta releases which will have a pure-Java backend to replace the previous issues suffered with the FFI implementation.</p>
<p>We tend to lag a bit behind the community for releases of gems that contain C extensions. The new C extension API support in the upcoming JRuby 1.6 release will mitigate that a bit, but we also rely on the community to help us port the most popular gems when it's apparent that a Java-backed API would be more useful.</p>
<p><em>How is the refactoring support in Ruby dev tools?</em></p>
<p>It's not up to par with Java-based refactoring tools, but rarely do you need that level of control. Tools like RubyMine help with renaming, extraction and organization of code. Of course any refactoring exercises should be supported by a solid test suite, and fortunately Ruby has a strong culture of testing to reinforce that.</p>
<p><em>We are building iPad, iPhone and Android apps in HTML5, based on jQuery. Can we use JRuby with an Engine Yard background running?</em></p>
<p>Sounds like a great choice! We'd be happy to help you get up and running.</p>
<p><em>How to access a Spring context from a JRuby on Rails controller?</em></p>
<p>We actually have a <a href="http://wiki.jruby.org/JRubyOnRailsWithSpring">nice JRuby on Rails article on the JRuby wiki</a> on this topic. It should still basically work, let us know if you have any problems following it.</p>
<p><em>Any info on including JRuby as a scripting framework in a NetBeans platform application?</em></p>
<p>I haven't heard any specifics around NetBeans RCP development using JRuby, but it sounds like a good idea. Start with <a href="http://wiki.jruby.org/RedBridge"> documentation on the JRuby wiki on JRuby's embedding API</a> to get an idea of some possibilities.</p>
<p>As always, feel free to contact us for help or more ideas. We want to hear from you.</p>
</div>
<p><a href="http://www.engineyard.com/blog"><img height="98" width="61" title="logo-engineyard" alt="" class="attachment-post-thumbnail wp-post-image" src="http://www.engineyard.com/blog/wp-content/uploads/logo-engineyard.png"/></a></p>
]]></description>
		<wfw:commentRss>http://www.engineyard.com/blog/2010/connecting-the-worlds-of-java-and-ruby-with-jruby-webinar/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Webinar: Connecting the Worlds of Java and Ruby with JRuby</title>
		<link>http://www.engineyard.com/blog/2010/webinar-connecting-the-worlds-of-java-and-ruby-with-jruby/</link>
		<comments>http://www.engineyard.com/blog/2010/webinar-connecting-the-worlds-of-java-and-ruby-with-jruby/#comments</comments>
		<pubDate>Tue, 07 Dec 2010 20:25:49 +0000</pubDate>
		<dc:creator>Nick Sieger</dc:creator>
				<category><![CDATA[Events]]></category>
		<category><![CDATA[Ruby]]></category>
		<category><![CDATA[education]]></category>
		<category><![CDATA[JRuby]]></category>
		<category><![CDATA[Webinar]]></category>

		<guid isPermaLink="false">http://www.engineyard.com/blog/?p=5200</guid>
		<description><![CDATA[<p>Since my very first encounter with JRuby, I've always felt that JRuby is best viewed as a bridge. A bridge connecting Java and Ruby. A bridge introducing Ruby's innovations to the Java world while giving all that legacy Java code running out there a new lease on life. This view may seem obvious to all of you who already know (and love!) Ruby and JRuby, but there are many Java developers out there to whom it is not. We're making a new effort to engage with these folks. We'll start with a series of webinars focusing on bringing Ruby to the Java world.</p>
<p>For those of you who are Rubyists or regular readers of the Engine Yard blog, the content will most likely not be anything new. We're reaching out to you because we know you love Ruby and want to help Ruby continue to succeed and find new enthusiasts. So if you have friends or colleagues who are Java programmers that haven't heard the Ruby message, please pass this along and encourage them to check out the first webinar in the series that I'll be leading next week.</p>
<p>*Webinar:* <a href="http://pages.engineyard.com/JavatoJRubyWebinar.html">*JRuby - Connecting the Worlds of Java and Ruby*</a><br />
*Date: Tuesday, December 14th, 2010*<br />
*Time: 10:00 AM - 11:00 AM Pacific*</p>
<p>The content will be a very high-level overview of Ruby and JRuby for Java developers, including:</p>
<ul>
<li>An introduction to the Ruby language: Learn how Ruby can make you more productive and write more readable and maintainable code</li>
</ul>
<ul>
<li>An introduction to Rails: Learn how it speeds up web development</li>
</ul>
<ul>
<li>An introduction to JRuby: Learn how to integrate existing Java code with just a few of lines of Ruby and how to use JRuby to extend existing Java applications with Ruby on Rails</li>
</ul>
<p>If you're interested in attending, please <a href="http://pages.engineyard.com/JavatoJRubyWebinar.html">register here</a>.  If you'd like to attend, but can't make the live webinar, go ahead and register and we will email you a link to the full recording and slides once available. </p>
<p>Happy holidays, and thanks for helping us spread the Ruby cheer!
<p><a href="http://www.engineyard.com/blog"><img height="98" width="61" title="logo-engineyard" alt="" class="attachment-post-thumbnail wp-post-image" src="http://www.engineyard.com/blog/wp-content/uploads/logo-engineyard.png"/></a></p>
]]></description>
		<wfw:commentRss>http://www.engineyard.com/blog/2010/webinar-connecting-the-worlds-of-java-and-ruby-with-jruby/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>The State of XML Parsing in Ruby (Circa 2009)</title>
		<link>http://www.engineyard.com/blog/2009/xml-parsing-in-ruby/</link>
		<comments>http://www.engineyard.com/blog/2009/xml-parsing-in-ruby/#comments</comments>
		<pubDate>Fri, 20 Nov 2009 18:30:29 +0000</pubDate>
		<dc:creator>Nick Sieger</dc:creator>
				<category><![CDATA[Ruby]]></category>
		<category><![CDATA[Technology]]></category>
		<category><![CDATA[Nokogiri]]></category>
		<category><![CDATA[REXML]]></category>
		<category><![CDATA[XML]]></category>

		<guid isPermaLink="false">http://www.engineyard.com/blog/?p=2917</guid>
		<description><![CDATA[<p>It’s almost the end of 2009, and I have to ask: are we through dealing with XML yet?</p>
<p>Although many of us wish we could consume the web through a magic programmer portal that shields us and our code from all the pointy angle brackets, the reality that is the legacy of HTML, Atom and RSS on the web leaves us little choice but to soldier on. So let’s take a look at what Ruby-colored armor is available to us as we continue our quest to slay the XML dragons.</p>
<h2 id="background">Background</h2>
<p>Historically, Ruby has had a number of options for dealing with structured markup, though oddly none have reached a solid consensus among Ruby developers as the “go to” library. The earliest available library seems to be Yoshida Masato’s <a href="http://www.yoshidam.net/Ruby.html#xmlparser"><code>XMLParser</code></a>, which wraps <a href="http://expat.sourceforge.net/">Expat</a> and was first released around the time that Expat <em>itself</em> was released, back in 1998. A pure Ruby parser by Jim Menard called <a href="http://nqxml.sourceforge.net/">NQXML</a> appeared in 2001, though it never matured to the level of a robust XML parser.</p>
<p>In late 2001, <a href="http://blade.nagaokaut.ac.jp/cgi-bin/scat.rb/ruby/ruby-talk/24744">Matz expressed his desire for out of the box XML support</a>, but sadly, nothing appeared in Ruby’s standard library until 2003, when <a href="http://svn.ruby-lang.org/cgi-bin/viewvc.cgi?view=rev&amp;revision=3925">REXML was imported</a> for the <a href="http://svn.ruby-lang.org/repos/ruby/tags/v1_8_0/doc/NEWS">1.8.0 release</a>. After reading bike-shed discussions like <a href="http://blade.nagaokaut.ac.jp/cgi-bin/vframe.rb/ruby/ruby-talk/24931?24875-26245">this one on ruby-talk in November 2001</a>, or <a href="http://web.archive.org/web/20061023014435/wiki.rubygarden.org/Ruby/page/show/XMLinRuby">this wayback-machine page from the old RubyGarden wiki</a>, it’s not hard to see why. Meanwhile, other language runtimes, such as <a href="http://docs.python.org/library/markup.html">Python</a> and <a href="https://jaxp-sources.dev.java.net/nonav/docs/api/">Java</a>, moved along and built solid, acceptable foundations, making Ruby’s omission seem more glaring.</p>
<p>But all was not lost: Ruby has always had a quality without a name that made it a great language for distilling an API. All that was needed was an infusion of interest and talent in Ruby, and a few more experiments and iterations.</p>
<p>Fast forward to the present time, and all those chips have fallen. We’ve seen evolution from <a href="http://www.germane-software.com/software/rexml/">REXML</a> to <a href="http://libxml.rubyforge.org/">libxml-ruby</a> to <a href="http://wiki.github.com/hpricot/hpricot">Hpricot</a>, and finally to <a href="http://nokogiri.org/">Nokogiri</a>. So, is the XML landscape on Ruby so dire? Certainly not, as you’ll see by the end of this article! While the standard library support for XML hasn’t progressed beyond REXML yet, state-of-the-art solutions are a few keystrokes away.</p>
<h2 id="xml_apis">XML APIs</h2>
<p>A big part of what makes XML such a pain to work with is the APIs. We Rubyists tend to have an especially low tolerance for friction in API design, and we really feel it when we work with XML. If XML is just a tree structure, why isn’t navigating it as simple and elegant as traversing a Ruby <code>Enumerable</code>?</p>
<p>The canonical example of API craptasticism is undoubtedly the <a href="http://www.w3.org/TR/DOM-Level-2-Core/">W3C DOM API</a>. For proof, observe the meteoric rise of <a href="http://jquery.com">jQuery</a> in the JavaScript world. While it would be easy to fill an entire article with criticisms regarding the DOM, <a href="http://www.artima.com/intv/dom.html">it’s been done before</a>. (Incidentally, read the <a href="http://www.artima.com/intv/schema.html">whole series of interviews with Elliotte Rusty Harold</a> for a series of insights on API design, schema evolution, and more.)</p>
<p>Instead, we’ll take a brief exploratory tour of some Ruby XML APIs using code examples. Though some of the examples may seem trivially short, don’t underestimate their power. Conciseness and readability are Ruby’s gifts to the library authors and they’re being put to good use.</p>
<p>The libraries we’ll use for comparison are REXML, Nokogiri, and JAXP, <a href="http://en.wikipedia.org/wiki/Java_API_for_XML_Processing">Java’s XML parsing APIs</a> (via JRuby).</p>
<h2 id="parsing">Parsing</h2>
<p>The simplest possible thing to do in XML is to hand the library some XML and get back a document.</p>
<p><em>REXML</em></p>
<pre>require 'rexml/document'
document = REXML::Document.new(xml)</pre>
<p><em>Nokogiri</em></p>
<pre>require 'nokogiri'
document = Nokogiri::XML(xml)</pre>
<p>Both REXML and Nokogiri more or less get this right. What’s also nice is that they both transparently accept either an IO-like object or a string. Contrast this to Java:</p>
<p><em>JAXP/JRuby</em></p>
<pre>factory = javax.xml.parsers.DocumentBuilderFactory.newInstance
factory.namespace_aware = true # unfortunately, important!
parser = factory.newDocumentBuilder
# String
document = parser.parse(xml)
# IO
document = parser.parse(xml.to_inputstream)</pre>
<p>In that familiar Java style, the JAXP approach forces you to choose from many options and write more code for the happy path. JRuby helps you a little bit by converting a Ruby string into a Java string, but needs a little help with intent for converting an <code>IO</code> to a Java <code>InputStream</code>.</p>
<h2 id="xpath">XPath</h2>
<p>Now that we’ve got a document object, let’s query it via XPath, assuming the underlying format is an Atom feed. Here is the code to grab the entries’ titles and store them as an array of strings:</p>
<p><em>REXML</em></p>
<pre>elements = REXML::XPath.match(document.root, "//atom:entry/atom:title/text()",
                              "atom" =&gt; "http://www.w3.org/2005/Atom")
titles = elements.map {|el| el.value }</pre>
<p><em>Nokogiri</em></p>
<pre>elements = document.xpath("//atom:entry/atom:title/text()",
                          "atom" =&gt; "http://www.w3.org/2005/Atom")
titles = elements.map {|e| e.to_s}</pre>
<p>Again, both REXML and Nokogiri clock in at similar code sizes, but subtle differences begin to emerge. Nokogiri’s use of <code>#xpath</code> as an instance method on the document object feels more natural as a way of drilling down for further detail. Also, note that both APIs return DOM objects for the text, so we need to take one more step to convert them to pure Ruby strings. Here, Nokogiri’s use of the standard <code>String#to_s</code> method is more intuitive; <code>REXML::Text</code>’s version returns the raw text without the entities replaced.</p>
<p>Unfortunately, doing XPath in Java gets a bit more complicated. First we need to construct an <code>XPath</code> object. At least JRuby helps us a bit here–we can create an instance of the <code>NamespaceContext</code> interface completely in Ruby, and omit the methods we don’t care about.</p>
<p><em>JAXP/JRuby</em></p>
<pre>xpath = javax.xml.xpath.XPathFactory.newInstance.newXPath
ns_context = Object.new
def ns_context.getNamespaceURI(prefix)
  {"atom" =&gt; "http://www.w3.org/2005/Atom"}[prefix]
end
xpath.namespace_context = ns_context</pre>
<p>Next, we evaluate the expression and construct the array titles:</p>
<p><em>JAXP/JRuby</em></p>
<pre>nodes = xpath.evaluate("//atom:entry/atom:title/text()",
                       document, javax.xml.xpath.XPathConstants::NODESET)
titles = []
0.upto(nodes.length-1) do |i|
  titles &lt;&lt; nodes.item(i).node_value
end</pre>
<p>That last bit where we need to externally iterate the DOM API is particularly un-Ruby-like. With JRuby we can mix in some methods to the NodeList class:</p>
<p><em>JAXP/JRuby</em></p>
<pre>module org::w3c::dom::NodeList
  include Enumerable
  def each
    0.upto(length - 1) do |i|
      yield item(i)
    end
  end
end</pre>
<p>And replace the external iteration with a more natural internal one:</p>
<p><em>JAXP/JRuby</em></p>
<pre>titles = nodes.map {|e| e.node_value}</pre>
<p>This kind of technique tends to become a fairly common occurrence when coding Ruby to Java libraries in JRuby. Fortunately Ruby makes it simple to hide away the ugliness in the Java APIs!</p>
<h2 id="walking_the_dom">Walking the DOM</h2>
<p>Say we’d like to explore the DOM. Both REXML and Nokogiri provide multiple ways of doing this, with parent/child/sibling navigation methods. They also each sport a recursive descent method, which is quite convenient.</p>
<p><em>REXML</em></p>
<pre>titles = []
document.root.each_recursive do |elem|
  titles &lt;&lt; elem.text.to_s if elem.name == "title"
end</pre>
<p><em>Nokogiri</em></p>
<pre>titles = []
document.root.traverse do |elem|
  titles &lt;&lt; elem.content if elem.name == "title"
end</pre>
<p>Needless to say, Java’s DOM API has no such convenience method, so we have to write one. But again, JRuby makes it easy to Rubify the code. Note that our <code>#traverse</code> method makes use of our <code>Enumerable</code>-ization of NodeList above as well.</p>
<p><em>JAXP/JRuby</em></p>
<pre>module org::w3c::dom::Node
  def traverse(&amp;blk)
    blk.call(self)
    child_nodes.each do |e|
      e.traverse(&amp;blk)
    end
  end
end

titles = []
document.traverse do |elem|
  titles &lt;&lt; elem.text_content if elem.node_name == "title"
end</pre>
<h2 id="pull_parsing">Pull parsing</h2>
<p>All three libraries have a pull parser (also called a stream parser or reader) as well. Pull parsers are efficient because they behave like a cursor scrolling through the document, but usually result in more verbose code because of the need to implement a small state machine on top of lower-level XML events. They are best employed on very large documents where it’s impractical to store the entire DOM tree in memory at once.</p>
<p><em>REXML</em></p>
<pre>parser = REXML::Parsers::PullParser.new(xml_stream)
titles = []
text = ''
grab_text = false
parser.each do |event|
  case event.event_type
  when :start_element
    grab_text = true if event[0] == "title"
  when :text
    text &lt;&lt; event[1] if grab_text
  when :end_element
    if event[0] == "title"
      titles &lt;&lt; text
      text = ''
      grab_text = false
    end
  end
end</pre>
<p><em>Nokogiri</em></p>
<pre>reader = Nokogiri::XML::Reader(xml_stream)
titles = []
text = ''
grab_text = false
reader.each do |elem|
  if elem.name == "title"
    if elem.node_type == 1  # start element?
      grab_text = true
    else # elem.node_type == 15  # end element?
      titles &lt;&lt; text
      text = ''
      grab_text = false
    end
  elsif grab_text &amp;&amp; elem.node_type == 3 # text?
    text &lt;&lt; elem.value
  end
end</pre>
<p>(Aside to the Nokogiri team: where are the reader node type constants?)</p>
<p><em>JAXP/JRuby</em></p>
<pre>include javax.xml.stream.XMLStreamConstants
factory = javax.xml.stream.XMLInputFactory.newInstance
reader = factory.createXMLStreamReader(xml_stream.to_inputstream)
titles = []
text = ''
grab_text = false
while reader.has_next
  case reader.next
  when START_ELEMENT
    grab_text = true if reader.local_name == "title"
  when CHARACTERS
    text &lt;&lt; reader.text if grab_text
  when END_ELEMENT
    if reader.local_name == "title"
      titles &lt;&lt; text
      text = ''
      grab_text = false
    end
  end
end</pre>
<p>Not surprisingly, all three pull parser examples end up looking very similar. The subtleties of the pull parser APIs end up getting blurred in the loops and conditionals. Only write this code when you have to.</p>
<h2 id="performance">Performance</h2>
<p>At the end of the day, it comes down to performance, doesn’t it? Although the topic of Ruby XML parser performance <a href="http://www.rubyinside.com/ruby-xml-performance-benchmarks-1641.html">has been discussed before</a>, I thought it would be instructive to do another round of comparisons with JRuby and Ruby 1.9 thrown into the mix.</p>
<p><em>System Configuration</em></p>
<ul>
<li>Mac OS X 10.5 on a MacBook Pro 2.53 GHz Core 2 Duo</li>
<li>Ruby 1.8.6p287</li>
<li>Ruby 1.9.1p243</li>
<li>JRuby 1.5.0.dev (rev c7b3348) on Apple JDK 5 (32-bit)</li>
<li>Nokogiri 1.4.0</li>
<li>libxml2 2.7.3</li>
</ul>
<p>Here are results comparing Nokogiri and Hpricot on the three implementations  along with the JAXP version which only runs on JRuby (smaller is better).</p>
<p><img class="aligncenter" title="XML Benchmarks 1" src="http://eyweb-images.s3.amazonaws.com/xmlbench.001.png" alt="" width="520" height="396" /></p>
<p>The REXML results were over an order of magnitude slower, so it's easier to view them on a separate graph. Note the number of iterations here is 100 vs. 1000 for the results above.</p>
<p style="text-align: center;"><img class="aligncenter" title="XML Benchmarks 2" src="http://eyweb-images.s3.amazonaws.com/xmlbench.002.png" alt="" width="375" height="310" /></p>
<p>While these results don’t paint a <em>complete</em> picture of XML parser performance, they should give you enough of a guideline to make a decision on which parser to use once you take portability and readability into account. In summary:</p>
<ul>
<li>Use REXML when your parsing needs are minimal and want the widest portability (across all implementations) with the smallest install footprint.</li>
<li>Use JRuby with the JAXP APIs for portability across any operating system that supports the Java platform (including Google AppEngine).</li>
<li>Use Nokogiri for everything else. It’s the fastest implementation, and produces the most programmer-friendly code of all Ruby XML parsers to date.</li>
</ul>
<p>(As a footnote, we on the Nokogiri and JRuby teams are looking for community help to <a href="http://www.serabe.com/2009/08/26/final-status-update/">further develop the pure-Java backend for Nokogiri</a> so that AppEngine and other JVM deployment scenarios that don’t allow loading native code can benefit from Nokogiri’s awesomeness. Please leave a comment or <a href="http://xircles.codehaus.org/lists/user@jruby.codehaus.org">contact the JRuby team on the mailing list</a> if you’re interested.)</p>
<p>The <a href="http://github.com/nicksieger/xmlbench">source code for this article is available</a> if you’d like to examine the code or run the benchmarks yourself. Keep an eye on the Engine Yard blog for an upcoming post on Nokogiri, and as always, leave questions and thoughts in the comments!
<p><a href="http://www.engineyard.com/blog"><img height="98" width="61" title="logo-engineyard" alt="" class="attachment-post-thumbnail wp-post-image" src="http://www.engineyard.com/blog/wp-content/uploads/logo-engineyard.png"/></a></p>
]]></description>
		<wfw:commentRss>http://www.engineyard.com/blog/2009/xml-parsing-in-ruby/feed/</wfw:commentRss>
		<slash:comments>15</slash:comments>
		</item>
		<item>
		<title>5 Things to Look for in JRuby 1.4</title>
		<link>http://www.engineyard.com/blog/2009/5-things-to-look-for-in-jruby-1-4/</link>
		<comments>http://www.engineyard.com/blog/2009/5-things-to-look-for-in-jruby-1-4/#comments</comments>
		<pubDate>Mon, 21 Sep 2009 17:00:30 +0000</pubDate>
		<dc:creator>Nick Sieger</dc:creator>
				<category><![CDATA[Ruby]]></category>
		<category><![CDATA[Technology]]></category>
		<category><![CDATA[1.8.7]]></category>
		<category><![CDATA[1.9]]></category>
		<category><![CDATA[Java]]></category>
		<category><![CDATA[JRuby]]></category>
		<category><![CDATA[Rubyspec]]></category>
		<category><![CDATA[YAML]]></category>

		<guid isPermaLink="false">http://www.engineyard.com/blog/?p=2371</guid>
		<description><![CDATA[<p>The team has finally wrapped up the long summer of JRuby-related travel, and it’s time to set our sights on a new version of JRuby for the community to lovingly embrace.</p>
<p>The release itself is tentatively planned to enter a round of release candidates at the end of September. Please prepare yourselves, try the release candidates when they are available, and most importantly, <a href="http://jira.codehaus.org/browse/JRUBY">report bugs</a>!</p>
<p>So what can you expect in the next release, other than the usual array of bug, performance, and compatibility fixes? Here are the five areas where we’ve been focusing efforts.</p>
<h2 id="187_is_the_new_baseline">1. 1.8.7 is the New Baseline</h2>
<p>When Ruby 1.8.7 was first released, we had a false start where we eagerly started to add some of the new additions, only to find that Rails (version 2.0 at the time) was rendered useless by the changes. Now, finally, three minor releases of Rails 2.x later, Rails on Ruby 1.8.7 has finally stabilized, and is now the recommended version of Ruby to use with Rails. Additionally, 1.8.7 is showing up as the default version of Ruby in many operating systems, and we began to see more bug reports about missing 1.8.7 features.</p>
<p>So, in JRuby 1.4, we’ll finally report compatibility with Ruby 1.8.7p174:</p>
<pre>$ jruby -v
jruby 1.4.0dev (ruby 1.8.7p174) (2009-09-14 b09f382) (Java HotSpot(TM) Client VM 1.5.0_19) [i386-java]</pre>
<p>The patch-level (p174) is based on the version of Ruby where we pulled the updated standard library. We always maintain that patch-level is a “best effort” number, and as such, we cannot promise full feature-for-feature (or bug-for-bug!) compatibility. However, as the <a href="http://rubyspec.org/">RubySpec project</a> has matured, it's gotten much easier for us to feel confident about this claim. As always, if you spot an area where we differ from Ruby 1.8.7, please do <a href="http://jira.codehaus.org/browse/JRUBY">file a bug on the discrepancy</a>, or even better, make sure that RubySpec covers the issue by <a href="http://rubyspec.org/projects/rubyspec/issues/new">submitting a patch to RubySpec</a>.</p>
<h2 id="improving_19_compatibility">2. Improving 1.9 Compatibility</h2>
<p>Even though Ruby 1.9 is still a moving target, the RubySpec project has recently begun to cover many of the new 1.9 features, which gives JRuby an easier, more robust mechanism for tracking how far along the path to 1.9 we are.</p>
<p>We’ve also been following our tried-and-true approach of <em>just trying to get stuff working</em>. In particular, IRB, RubyGems, Rake, and even some simple Rails applications are working with JRuby in 1.9 mode.</p>
<p>Other than a couple of major new features remaining (Multilingualization/encoding, IO transcoding, Fibers), JRuby 1.9 mode should just work. To try out JRuby in 1.9 mode, just pass <code>--1.9</code> as a leading argument on the command-line:</p>
<pre>$ jruby --1.9 -v
jruby 1.4.0dev (ruby 1.9.1p0) (2009-09-14 b09f382) (Java HotSpot(TM) Client VM 1.5.0_19) [i386-java]</pre>
<p>If you’ve been writing 1.9 code, we’d love to hear if you can get it working on JRuby: do comment!</p>
<h2 id="new_yaml_parser">3. New YAML Parser</h2>
<p>Ok, so a new YAML parser is something you’d probably have to give more than a cursory glance to notice. But our good friend <a href="http://olabini.com">Ola Bini</a> has gone and done it again with <a href="http://olabini.com/blog/2009/07/porting-syck-a-story-of-c-parser-generators-and-general-ugliness/">Yecht, a new YAML engine</a>. Ola re-implemented YAML support in JRuby to be much closer to the idiosyncracies of Syck, the C YAML parser that MRI uses. With this change, a <a href="http://jira.codehaus.org/secure/IssueNavigator.jspa?reset=true&amp;&amp;query=yaml&amp;summary=true&amp;description=true&amp;fixfor=15285&amp;pid=11295&amp;resolution=1&amp;resolution=2&amp;resolution=3&amp;resolution=4&amp;resolution=5&amp;resolution=6">number of long-standing YAML compatibility issues are fixed</a>. Hopefully this is the last YAML engine that JRuby will ever need!<br />
<span id="more-2371"></span></p>
<h2 id="java_method_selection_and_coercion_api">4. Java Method Selection and Coercion API</h2>
<p>For a long time, JRuby Java integration features have been something of a black box, especially the way Java methods are selected and invoked and arguments are coerced. This black box has hindered understanding of how JRuby calls Java, and in the case of overloaded methods, has made some Java methods unreachable. Consider this Java class:</p>
<pre>public class Overloaded {
    public static void use(int i) { /* ... */ }
    public static void use(long l) { /* ... */ }
}</pre>
<p>And this JRuby code:</p>
<pre>Java::Overloaded.use 10</pre>
<p>Which Overloaded method is called? It turns out it’s <code>use(long)</code>. JRuby behavior is to coerce Ruby Fixnums into Java longs. Thus, with current versions of JRuby, it’s impossible to call the version that takes an int.</p>
<p>We’d like to make this part of the Java integration layer more transparent and accessible. Here are a few APIs we’re considering:</p>
<h3 id="objectjava_sendname_signature_args">a. <code>Object#java_send(name, signature, *args)</code></h3>
<p>Like <code>Object#send</code> except it would allow you to identify and invoke a specific Java method by its name and signature. A signature for these purposes is an array of classes, where the first element in the array specifies the return type.</p>
<h3 id="objectjava_methodname_signature">b. <code>Object#java_method(name, signature)</code></h3>
<p>Like <code>Object#method</code>, except it would take an array of Java classes representing a method signature as an additional argument and return a Method object that can be <code>#call</code>‘ed.</p>
<h3 id="coerce_totype">c. <code>#coerce_to?(type)</code></h3>
<p>Method convention to allow an object to decide whether it can be coerced to a Java type. JRuby will call this method if it’s available to get hints to aid in choosing a method in the presence of several overloaded methods.</p>
<h3 id="to_javatype">d. <code>#to_java(type)</code></h3>
<p>Method convention to allow arbitrary Ruby objects to control how they are coerced to Java. If a Ruby object responds to <code>#to_java</code>, JRuby will use it to convert the argument before passing to a Java method invocation.</p>
<h3 id="to_rubytype">e. <code>#to_ruby(type)</code></h3>
<p>Like <code>#to_java</code> except to be applied to return types and Java objects coming back to Ruby.</p>
<p>All of these APIs are still yet to be implemented, and we could use some good real-world examples of where JRuby falls down to help ensure that we solve those issues.</p>
<h2 id="generate_real_java_classes_from_ruby">5. Generate Real Java Classes from Ruby</h2>
<p>Since we started writing <a href="http://kenai.com/projects/ruby2java/pages/Home">Ruby2java</a> as an add-on library, we’ve been looking for a way to expose the functionality of creating a real Java class from Ruby in the JRuby core. JRuby 1.4 will have some new experimental APIs for doing this. The example below explains it in code:</p>
<pre>require 'java'
require 'jruby/core_ext'

class SimpleTest
  def equals
    raise "not equal" unless 1.0 == 1
  end
end

SimpleTest.add_method_signature("equals", [java.lang.Void::TYPE])
SimpleTest.add_method_annotation("equals", org.junit.Test =&gt; {})
SimpleTest.become_java!</pre>
<p>The effect of this code is the same as writing the following Java code:</p>
<pre>package ruby;
import org.junit.Test;
public class SimpleTest {
    @Test
    public void equals() {
        // dispatch back to Ruby code here
    }
}</pre>
<p>We are also providing <code>#add_class_annotation</code> and <code>#add_parameter_annotation</code> methods as well, rounding out the ability to shape a real Java class.</p>
<p>These APIs are admittedly verbose and low-level, but they <em>do</em> allow you to easily build higher level constructs using Ruby metaprogramming techniques.</p>
<p>Here’s a more realistic example of implementing a JAX-RS/Jersey resource using Ruby (<a href="http://github.com/nicksieger/ruby-jersey">full source available</a>):</p>
<pre>class Hello &lt; RubyJersey::Resource
  GET()
  Produces("text/plain")
  Returns(java.lang.String)
  def hello
    "Hello Ruby Jersey!"
  end
end</pre>
<h2 id="feedback">Feedback</h2>
<p>The release is still a couple of weeks away, so none of this is by any means final. Please send us feedback by commenting here or use the <a href="http://xircles.codehaus.org/lists/user@jruby.codehaus.org">JRuby mailing list</a> for longer discussions, and let us know how the 1.4 release candidates work for you!
<p><a href="http://www.engineyard.com/blog"><img height="98" width="61" title="logo-engineyard" alt="" class="attachment-post-thumbnail wp-post-image" src="http://www.engineyard.com/blog/wp-content/uploads/logo-engineyard.png"/></a></p>
]]></description>
		<wfw:commentRss>http://www.engineyard.com/blog/2009/5-things-to-look-for-in-jruby-1-4/feed/</wfw:commentRss>
		<slash:comments>14</slash:comments>
		</item>
	</channel>
</rss>

