Talks

JRuby: What Why How ... Do it Now!

JRuby: What Why How ... Do it Now!

by Thomas E Enebo and Charles Nutter

The video titled "JRuby: What Why How ... Do it Now!" presented by Thomas Enebo and Charles Nutter at RubyConf 2017 focuses on introducing JRuby, an implementation of Ruby built on the Java Virtual Machine (JVM). The speakers emphasize that JRuby functions as standard Ruby, allowing existing Ruby code to run seamlessly. They highlight several advantages of JRuby's infrastructure, which leverages the extensive features available on the JVM:

  • Compatibility and Deployment: JRuby is compatible with Java, and developers can run Ruby applications on various platforms due to JVM's broad reach, including mobile devices and mainframes.
  • Performance Enhancements: JRuby utilizes Java's advanced garbage collection and Just-In-Time (JIT) compilation, enabling more efficient memory usage and performance gains, particularly for applications with high concurrency needs.
  • Native Libraries and Tools: JRuby allows for the use of Java libraries as if they were Ruby gems, expanding functionality with resources like Maven's extensive library repository.
  • Cross-Language Integration: Developers can mix Ruby with other JVM languages, allowing a portion of the application to be written in Clojure for specialized tasks, enhancing the overall functionality of apps.
  • Ease of Development: With rich debugging tools available on the JVM and the ability to easily create Java components in Ruby (e.g., GUI applications using JavaFX), JRuby simplifies the development process.

During a live demo, the speakers illustrated how easily developers can integrate Java GUI features into Ruby applications. They further elaborated on the importance of community support in enhancing JRuby's capabilities, which allows for feedback, code contributions, and troubleshooting.

Concluding the presentation, the speakers encouraged developers to explore transitioning to JRuby, citing successful migrations of complex applications like Redmine and Mastodon, and highlighting cost savings in infrastructure as a major benefit. They also noted how their focus on improving compatibility with Active Record is paving the way for seamless Ruby on Rails deployment on JRuby.

The session ended on a supportive note, inviting the community to contribute to the JRuby project and utilize available resources for further learning about JRuby's benefits and functionalities.

00:00:11.210 Hi, I'm Tom, and this is Charlie. We work at Red Hat, which is generous enough to allow us to work on JRuby.
00:00:18.109 We co-lead the JRuby project, and it will be no surprise that we're going to talk about JRuby. The first thing to note is that JRuby is Ruby—it's just an alternate implementation. If you've written any Ruby code that runs in CRuby, you should be able to run it with JRuby.
00:00:34.170 Sometimes, people get confused when I mention Java several times, but if you never use any of our extra features, just know that you can run your Ruby code, and it will work. You might think of Java as just an implementation detail, much like C is for regular Ruby.
00:00:45.210 We just released JRuby 9.2.0.0 last week, which is compatible with Ruby 2.3. You will need Java 7 or higher to run it. JRuby 9.2 is going to be Ruby 2.4 based, and it's actually sort of feature complete now, but we still want to add some things.
00:01:04.939 We're considering a release around Christmas. However, as you know, CRuby also releases around Christmas, and they are only putting out Ruby 2.5, making us one version behind.
00:01:20.909 Let's talk about why we implemented Ruby on the JVM and why that is useful. Both JRuby and CRuby have their unique advantages. Sometimes, the same people work on both projects, and we try to target where we want to run Python on platforms like macOS.
00:01:41.150 However, JRuby mostly targets running directly on Java itself. We have a little flexibility on the side for when we need more power to address certain limitations in Java. Meanwhile, MRI (Matz's Ruby Interpreter) has virtually no dependencies, so it has to go it alone. We're going to argue why we prefer running on top of Java.
00:02:15.860 This isn’t criticism of how CRuby is implemented; it’s just a different perspective. CRuby needs to have all the expertise to develop a fast, efficient version of Ruby, but we benefit from an army of experts working on Java who provide us with tools and features for free.
00:02:41.150 We have choices of multiple garbage collectors. There's a fantastic JIT (Just-In-Time) compiler that does a lot of crazy optimizations for our code. Java's native threading is one of the biggest differences between JRuby and CRuby, allowing us to utilize all available cores.
00:03:07.609 There's a lot of tooling available for us to debug issues, and when I say cross-platform, I mean that the JVM is literally everywhere—from normal platforms to IBM mainframes. We even have users running JRuby on IBM mainframes, Android microcontrollers—you name it. So JRuby enjoys a broad reach just from being built on the JVM.
00:03:32.209 Java compiles to an intermediary format of Java bytecode. After you compile something once, you can run it on Windows or a microcontroller, etc. Thus, it's only compiled once, which is a great advantage.
00:03:49.879 JRuby allows for Java native extensions, which include a bit of Java. The gem authors compile it once and publish the gem, so you just install it, and it works on Windows, which is often an issue with native C extensions. The equivalent of Ruby gems for Java is Maven, which has more than 47,000 libraries and tons of useful tools like Prawn for PDF generation.
00:04:21.950 However, if developers encounter issues that Prawn cannot solve, JRuby offers the feature to load Java libraries, allowing functionality as if they were Ruby libraries. If they need more capability, they can switch over to a mature PDF generation library like iText.
00:04:49.110 Additionally, you can call multiple languages that target the JVM. Thus, one can write 80-90% of the app in Ruby but switch out to Clojure if something is better suited for transactional processing or immutability. They can coexist in the same VM, utilizing the same garbage collector and even explore tools for memory usage.
00:05:19.530 Every language has its own libraries, offering additional options and opportunities. If you need to hook up to a commerce API, there might be something written in Scala that can be used seamlessly with JRuby.
00:05:54.810 Now, let’s turn it over to Charlie for a demo.
00:06:10.800 All the typical Ruby installers can install JRuby. The only prerequisite is having a JVM on your system. As expected, we have an IRB, which we call JIRB. As Tom mentioned, we can utilize any Java classes at any time as if they were regular Ruby code.
00:06:30.390 For example, we can create a new Java JFrame. Let's instantiate it: frame = Java::Swing::JFrame.new.
00:06:57.750 Now we have a window frame. Let’s make that visible.
00:07:03.300 There it is—it doesn't look like much right now, so let's set it to a size of 300 by 300.
00:07:19.920 Next, let’s add a button to it. Again, we set everything up so we can see it. Now we've got our button, and we can easily respond to events from the button using Ruby. Though we would usually have to implement the ActionListener interface in conventional Java, JRuby allows for a more 'Ruby-like' approach.
00:07:49.980 For instance, we can easily update the button text with just button.text = 'JRuby'. Then we return to our window, and it all works seamlessly.
00:08:21.120 This is an excellent way to explore different libraries on the JVM. As you can see, this doesn't really look like Java, except when we import the package.
00:08:46.850 We also have another GUI library available on the JVM called JavaFX that we have been using to build JavaFX applications with Ruby code. Charlie shows that he planned to run a demo.
00:09:19.120 There we go! The excitement of a live demo! It was a bit tricky, but I'm back.
00:09:38.600 Developing with these Java classes can be tricky. Sometimes after making minor changes, the demo may not show correctly because of how changes build upon each other in Java syntax.
00:10:06.970 Any changes here require attention, as Java classes can be complex. However, JRuby FX is a gem developed to wrap the JavaFX GUI library in a straightforward way.
00:10:49.700 It has two modes: a DSL builder for easier use, and a model-view-controller format that employs XML. Users often utilize Scene Builder, a canvas application that provides various controls for design.
00:11:01.500 It includes a palette on the left for custom controls, and once you have laid out your components, you will see properties on the right for customization. For instance, I named the left one 'Gauge' and the right one 'Slider'.
00:11:34.740 If working correctly, when the slider moves, you would see the gauge following it, showcasing simple interaction.
00:12:01.590 A more realistic application is called BreakMan Pro. It utilizes static analysis of Ruby on Rails applications, boasting a polished interface and heavily relying on JRuby FX.
00:12:27.100 JRuby benefits from advanced garbage collection algorithms and JIT optimizations on the JVM. There are parallel GCS, concurrent GC tools for live analysis, and tools for watching the heap to detect memory leaks.
00:12:56.720 VisualVM is one of the tools that provides graphical overviews of applications. You can connect it to a running JRuby application to monitor various aspects of Java's performance.
00:13:33.910 With VisualVM, you can monitor CPU usage, thread activity, and garbage collection processes, allowing developers to ascertain potential memory leaks or inefficiencies.
00:14:11.530 Furthermore, you can profile Ruby methods being interpreted in the JVM, providing insights into performance.
00:14:25.540 We've optimized Ruby's performance by implementing our own interpreter that runs Ruby code, letting us profile over time and compile directly into JVM bytecode.
00:14:49.560 By leveraging features like invoke dynamic, we've improved how Ruby and Java interact when compiling, leading to better optimization.
00:15:07.330 We’ve improved performance metrics dramatically, with benchmarks demonstrating JRuby's speed compared to CRuby, showcasing the benefits of dynamic compilation and JVM enhancements.
00:15:26.640 Using JRuby can result in considerable performance gains, especially in numerical computations where optimizing handling of objects on the JVM can reduce processing times significantly.
00:16:06.790 Additional benchmarks show JRuby's efficiency in managing and executing complex structures like red-black trees significantly more efficiently than CRuby.
00:16:29.290 Although CRuby provides concurrency, it operates under a global lock, while JRuby allows native threads to fully utilize available cores, which facilitates greater performance for concurrent requests.
00:16:54.850 Transitioning to JRuby can reduce memory needs significantly, as it generally requires only one process to handle multiple requests—leading to notable cost savings for large applications.
00:17:15.000 For instance, a friend transitioned a large application from 40 EC2 instances down to 10 and saved tens of thousands of dollars monthly simply by using JRuby.
00:17:55.190 As for JRuby on Rails, we’ve been running it since 2006, with many people successfully deploying Rails applications on JRuby.
00:18:07.680 While we were in the process of supporting Rails 5, we lagged behind yet managed to pass all tests apart from one concerning Active Record, which is crucial since running Rails without a database is futile.
00:18:58.700 We’ve since focused on improving compatibility with Active Record JDBC, an API allowing communication with various databases. Compatibility between Active Record and JRuby is a complex task indeed.
00:19:24.920 Active Record JDBC required support for various database systems with their unique features, creating compatibility complexities.
00:19:55.579 We realized addressing all compatibility dimensions proved challenging for past versions, and we’re addressing these concerns through JRuby now.
00:20:22.060 Moving forward, we're planning to support only a few essential databases like MySQL, SQLite, Postgres, and Microsoft SQL server, simplifying our prior models.
00:20:46.490 By streamlining compatibility with recent versions of Active Record, we improved our codebase's manageability and have seen great results in recent tests.
00:21:25.730 The famed Redmine project, a widely known pure Ruby project, was successfully migrated to JRuby without issues, showcasing stability and robust functionality.
00:21:57.100 Mastodon, a more complex application resembling a decentralized Twitter platform, posed more challenges but still had multiple dependencies with JRuby versions.
00:22:28.800 While some libraries presented conversion issues, the process was manageable due to active efforts from the community.
00:22:50.400 Various wrappers could fill the gaps of missing libraries, as JRuby's capabilities allow for flexible adaptation to Java systems.
00:23:05.720 Lastly, we've emphasized the necessity of community support for JRuby, aligning contributions toward closed issues, documentation improvements, and language support.
00:23:38.880 To make JRuby more effective, we invite engagement from developers familiar with JRuby and Ruby code, supporting our efforts to nurture a vibrant JRuby ecosystem, contributing code, troubleshooting, and sharing ideas.
00:24:49.679 If you’d like to learn more about deploying JRuby applications or migrating existing Ruby apps to it, look into literature from Joe Kutner and others that outline procedural approaches effectively.
00:25:13.620 The journey of transition is notable, and the community's reception of JRuby continues to evolve.
00:25:23.679 We are proud to have significant organizations using JRuby, like BBC News, proving its reliability and effectiveness for critical applications.
00:25:49.780 Charlie and I are delighted to encourage you to explore JRuby’s capabilities and hope this discussion has sparked your interest.
00:26:03.700 Let’s open the floor for questions and have an engaging discussion.
00:26:15.880 Thank you for joining us today! Feel free to discuss further around the conference if you have any additional questions.