Going dynamic on JVM: jruby vs. Groovy

Source: Internet
Author: User
Tags grails
JVM is awesome!

Developers people that Bash Java make the mistake of mixing up the Java language and the Java platform. sure, the Java language is verbose; it lacks closures, mixins, etc. but the JVM is freaking awesome. it's blazing fast, it runs everywhere, has multiple garbage
Collectors, has native threads and great monitoring tools. as a result, you may still want to run your server-side applications on the JVM. the best part is that you don't have to use the Java language. if you are a fan of dynamic ages, there are lots
Of available options. the most mature ones are jruby and groovy. if you are asking yourself, "shocould I pick groovy or jruby," read this post and, hopefully, you will be able to make the describe.

Groovy

Let's start with groovy. groovy is a dynamic language designed for the Java Virtual Machine. it takes a lot of ideas from such versions as Ruby and Python (and adds a few its own) and wraps them in a Java-like syntax. being designed for the JVM groovy has
A few advantages over ported ages. one of them is seamless interoperability with the Java language (both groovy to Java and Java to groovy ). however, in order to make it closer to Java, the designers of the language had to make some compromises. for instance,
Groovy uses getters and setters to define properties.

Groovy Home Page

Jruby

It's an implementation of the Ruby language on top of the JVM. therefore, the coolest thing in jruby is the fact it's Ruby. this means that you can use all the goodness of the Ruby platform on the JVM. it's not only rails. rubyists have developed a lot
Really cool stuff, and the ability to use all these libraries rocks. However, there is a downside. jruby's InterOP with Java is not as smooth as groovy's.

Jruby Home Page

Groovy vs. jruby

Let's compare the following aspects of the ages: Maturity, language features, killer libs, InterOP with Java, performance, and tools.

Maturity

The jruby project celebrated its 10-year anniversary last year. it supports Ruby 1.8 and 1.9. most gems work with jruby without any problems. those that don't work have analogs written in Java (for example, nokogiri Java ). there are a few cloud Solutions
Supporting jruby and several robust application servers. it's by far the fastest Ruby implementation. I think jruby is gaining momentum right now. people are talking and writing about advantages of jruby over MRI everywhere. it's used in production in a lot
Of companies (e.g., LinkedIn and square ).

The Groovy project was started up in 2003. it became stable in 2008. in the very beginning it was used primarily as a scripting language for the Java platform, but over the last couple of years its ecosystem has really matured. A lot of Idiomatic groovy
Libraries (e.g ., spork, Geb, and gradle) have come out. there are cloud deployment options for groovy as well. groovy is used in your companies that have existing Java infrastructure (for example Netflix ).

Language Features

Since groovy was in every ways available red by ruby, both languages ages are very similar. the emphasis though is different. groovy has optional typing and interfaces and it's structured more like Java, which makes it more static than Ruby. one of these "more static"
Features differentiating groovy from other dynamically typed versions is compile-time metaprogramming. in groovy you can write extensions to the compiler that will change the semantics of the language. it opens up plenty of options for implementing cool domain
Specific languages. Spock, the testing framework, is a good example:

def "length of Spock's and his friends' names"() {  expect:  name.size() == length  where:  name     | length  "Spock"  | 5  "Kirk"   | 4  "Scotty" | 6}
View raw

Spock. Groovy this gist is brought to you using
Simple gist embed.

Similar JUnit test wowould look like:

def "length of Spock's and his friends' names"() {  assert "Spock".size() == 5  assert "Kirk".size() == 3  assert "Scotty".size() == 6}
View raw

JUnit. Groovy this gist is brought to you using
Simple gist embed.

It's important to point out that groovy does not support the 'Expect CT: 'and 'where: 'keywords out of the box. spock extends the groovy compiler to change the execution flow of your tests. awesome!

Jruby (or it's better to say Ruby) has lots of nice features for writing dsls as well. for example, the fact that class bodies are executable enables you want to write some really interesting dsls:

class Person  validates_presence_of :name  has_many :addressesend
View raw

Person. RB this gist is brought to you using
Simple gist embed.

Overall, I find myself writing more dynamic code in ruby that I wocould do in groovy. For instance, I can generate classes on the fly:

class Person < Struct.new(:name, :age)  def to_s    "#{name} is #{age} years old"  endend
View raw

Struct. RB this gist is brought to you using
Simple gist embed.

It's possible to write similar code in groovy, but it's not as elegant.

Killer libs

There are some really fantastic technologies built on top of groovy.

  • Grails. Web application framework.
  • Griffin on. A grails-like rich-application platform.
  • Spock. Testing Framework.
  • Gradle. An enterprise automation tool.
  • Geb. browser automation tool.

Being a ruby implementation, jruby gives you access to all the goodness created by the Ruby community.

  • Rails. Web application framework.
  • Sinatra. Web application framework.
  • Rspec. A state-of-the-art BDD framework.
  • Rake. A software task management tool.
  • Bundler. A dependency management tool.
InterOP with Java

Using Web Services and message queues is a right way to integrate a new system into existing infrastructure. however, sometimes it's impossible and you have to perform an object-level integration. being designed for the JVM, groovy does a better job here.
It seamlessly integrates with Java. groovy calljava, Java callgroovy. it just works. ruby, on the other hand, has a different object model, different naming conventions. there is no such thing as an interface in ruby. in addition, the libraries are different
As well. Even though the jruby team did an amazing job making this integration as smooth as possible, it's not even close to what groovy does.

Performance

I'm not going to show you charts. And no, I'm not going to tell you which language is faster. Why? It's not important. You shocould not make your demo-based on the fact that one of them is 10% faster than another one. jruby and groovy are slower than Java.
There is nothing you can do about it. but this is all right, because they are fast enough (and they are going to be much faster after they utilize invokedynamic ). if somehow you run into a situation when they are not, switching from jruby to groovy (and vice
Versa) won't make the situation better. Just rewrite that class in Java and integrate it with the rest of your application.

Tools (IDES and text editors)

Both versions are supported by all major text editors: vim, emacs, textmate, sublime. if you prefer ides, there are plugins for Eclipse, netbeans, and intellij idea (rubymine ). I personally prefer intellij idea and rubymine.

So groovy or jruby?

Hopefully, by now you have some idea of the pros and cons. If after weighing them up you are still not sure, here is some advice I can give you based on my own experience.

  • You are working with a large Java code base. the new module you're planning to write will extensively interact with the existing Java code. groovy is your choice. there is no other language on the JVM having such a seamless integration with Java.
  • You are working in a Java shop and your team consists of experienced Java developers, who are not really familiar with dynamically typed versions such as Smalltalk or Python. again, choose groovy. it has optional typing and interfaces. besides, your team
    Will be able to use existing tools and libraries. I wocould not say that it feels like Java, but it's definitely an easier switch.
  • You and your team can invest a little bit more time into learning new skills. existing infrastructure can be isolated from the new module you're going to build by an anti-attack uption layer. maybe you are building a web app or a rest web service. try jruby.
    The rails ecosystem is fantastic. I believe it's the best full-stack solution for Building Web apps right now.
  • Your team has experience with other dynamically typed versions such as PHP, Python or smalltalk. Go for jruby.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.