简体   繁体   中英

How to make a Ruby program work with a Java program?

This is not a question as such, but more like a list of things I would like to know about how to make a Ruby program work with a Java program.

Here's the thing: We have two teams. One works on Java and implements their code and module in Java. We do the same in Ruby. We are supposed to have a set of common interfaces and use some code of theirs in our Ruby program and they are to use some of our Ruby code in their Java program.

What are some of the things to keep in mind while connecting these two modules to give one common module? I guess this would be more like a guide or a set of guidelines than a straight answer. So please, have some patience and not report or close this question. Thank you!

Update:

Yes, we are to use RESTful services. I do not want any sort of coding help. All I want is your contribution which you might have from your experience with coding modules in two different languages and interconnecting them with a common interface.

That's pretty much all I would like to know. While this is indeed a class project of mine, I thought it would be better if there were guidelines somewhere so it could help other people as well.

In the situation you describe, I would be inclined to use JRuby so your Ruby code can interoperate directly with Java. You could also make the Ruby/Java parts of your system talk over a socket using JSON or something, but there is a performance cost and a complexity cost to doing that. (Whether the performance hit really makes a difference depends on how "fine-grained" the interface between the Ruby/Java parts are -- how many Ruby-to-Java or Java-to-Ruby calls are required to process a single request.)

In JRuby, you can load JAR files and use the (Java) classes defined in them as if they are Ruby classes. You can also go the other way -- you can use JRuby like a "library" from within a Java application to evaluate Ruby code.

You should probably either 1) write the "main" application in Java, and write the Ruby parts as "libraries" which the main application calls into, or 2) write the main application in Ruby, and write the Java parts as libraries. If you have circular dependencies from Java code into Ruby and back from Ruby into Java, it will make your codebase difficult to understand.

This is a huge problem, so I am not going to give specifics as to how this is done. I can give you an idea how to go about doing this. Assuming you are asking about web application ...

  • Java-based web application on the database side
  • Java-based web application doesn't really have a "view" for client. It won't generate conventional css, html view.
  • Instead it created JSON objects based on URL
  • Rails uses JSON objects as if it is data coming from database. It can create models out of them.
  • Basically, Rails is making an API call to Java based web app every time it needs to get to database.
  • Big downside of this is that you no longer can use ActiveModel, ActiveRecords etc, all the goodies that come with Rails
  • Upside? Potentially faster for database queries that are complicated

This is basically what companies like Twitter and Amazon do when they say they use Java on the backend while they use Rails on the frontend. But again, this is a huge endeavor, and you definitely want to think about cost and benefit of this.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM