Calling Enterprise Java Beans from Ruby Code

12.11.08

Filed Under: java, jruby, programming, ruby with 0 Comments

Is a fact that a great piece of corporate softwares has been built over Java platform, times are changing and as a software professional we must be prepared for the future and try all the stuffs around.

Recently i´ve tried JRuby, not just programming in Ruby but specially about Java/Ruby integration and about it i decided dealing with EJB integration.

Today i´ll show you how to call and consume a EJB service inside Ruby Code, i´m considering that you have early or at least a bit knowledge about Java, Ruby and Glasfish deployment to get this example.

To build this example i used the sctructure bellow :

- JRE 1.6
- Java EE 5
- JRuby v 1.1
- Glassfish Application Server v 2

Once you´ve installed the dependencies above let´s get started.

Let´s suppose we have a Calculator service which provides math operations and we want to consume that inside our Ruby stack. This component is composed by three classes :

A Business implementation class :


package com.tonyfabeen.jruby.service;

import javax.ejb.Stateless;

@Stateless
public class CalculusServiceBean
implements CalculusServiceRemote,
CalculusServiceLocal {

    public int sum(int a, int b) {
        return a+b;
    }

    public int multiply(int a, int b) {
        return a*b;
    }

    public int divide(int a, int b) {
        return a/b;
    }

    public int subtract(int a, int b) {
        return a-b;
    }

}

A Remote Interface :


package com.tonyfabeen.jruby.service;

import javax.ejb.Remote;

@Remote
public interface CalculusServiceRemote {

    int sum(int a, int b);

    int multiply(int a, int b);

    int divide(int a, int b);

    int subtract(int a, int b);

}

A Local Interface :


package com.tonyfabeen.jruby.service;

import javax.ejb.Local;

@Local
public interface CalculusServiceLocal {

    int sum(int a, int b);

    int multiply(int a, int b);

    int divide(int a, int b);

    int subtract(int a, int b);

}

Note : To access a EJB component as a stand alone application we must declare *.jar dependencies in the $CLASSPATH before starting coding the client. In this case you should declare appserv-rt.jar, javaee.jar from $GLASSFISH_INSTALL/lib and EJBCalculus.jar(Name that i gave to my project) from the EJB component.

Our Ruby code is gonna see like that :


require 'java'

include_class 'java.util.Properties'
include_class 'javax.naming.Context'
include_class 'javax.naming.InitialContext'

class EJBRubyClient

  def jndi_properties
    @jndi_properties = Properties.new
    @jndi_properties.put("java.naming.factory.initial",
      "com.sun.enterprise.naming.SerialInitContextFactory")
    @jndi_properties.put("java.naming.factory.url.pkgs",
      "com.sun.enterprise.naming")
    @jndi_properties.put("java.naming.factory.state",
      "com.sun.corba.ee.impl.presentation.rmi.JNDIStateFactoryImpl")
    @jndi_properties.put("org.omg.CORBA.ORBInitialHost",
      "localhost")
    @jndi_properties.put("org.omg.CORBA.ORBInitialPort",
      "3700")
  end

  def context
    @context = InitialContext.new(jndi_properties)
  end

  def invoke_business_bean(name)
    @context ||= context
    bean = context.lookup(name)
  end 

end

Analyzing the code above we have the first line declaring that we are using Java inside our Ruby class.

From line 03 to 05 include_class statement act as import declarations in java code.

Inside jndi_properties method we have all declarations we need to access a Java EE resource.

The context method obtains the context and the method invoke_business_bean obtains an instance of a business component passed by name parameter.

To test the code you may write a code such that bellow :


ejb_client = EJBRubyClient.new
business_bean = ejb_client.invoke_business_bean("com.tonyfabeen.jruby.service.CalculusServiceRemote")

puts business_bean.sum(4,2)
puts business_bean.multiply(4,2)
puts business_bean.divide(4,2)
puts business_bean.subtract(4,2)

It´s a simple example even though there are more ways and another reasons to mix Java with Ruby. It depends on what are the requirements exposed by your customers.

I hope you enjoy this simple example. Any doubt or question post it here.

Keep Rocking.

Merb 1.0 was released, so Enjoy it!!!!

10.11.08

Filed Under: Uncategorized with 0 Comments

To keep up-to-date just execute :

$ thor merb:stack:install

Note : Your rubygems system must be up-to-date, you should use >= 1.3.0 versions. If you come from < 1.3.0 you may face errors.  Trying commands bellow you gonna solve that :

$ sudo gem install rubygems-update
$ sudo update_rubygems

That´s it. Keep Rocking.

Installing Merb without pain

31.10.08

Filed Under: merb, programming, ruby with 0 Comments

Hi there,

Some of you may faced errors caused by unstable versions of gems or dependencies trying to install merb but this way could be easier once you try thor.

What´s is thor ? Thor is a script framework that replaces rake and sake in the merb environment getting better the way to manage gems, dependencies and merb applications.

I´ll show you on a step-by-step how to start with thor and create your first merb application:

Well, first you should obtain thor´s gem ?

$ gem install thor

Then get merb.thor file which are two options to do it.

Installing as global :

$ thor install http://merbivore.com/merb.thor

Or simply local :

$ curl -L http://merbivore.com/merb.thor > merb.thor

After that keep up-to-date thor merb tasks :

$ thor merb:tasks:update

Then install all the merb stack into your computer :

$ thor merb:stack:install

And install dependencies :

$ thor merb:dependencies:install stable

If you have an Indiana Jones spirit both commands above accept –edge parameter which permits you living on the merb edge.

After all you are able to create a merb application :

$ merb-gen app myfirstapp

More information about thor tasks and merb check them out on http://wiki.merbivore.com .

Keep rocking.

Merb 1.0 almost there

07.10.08

Filed Under: Uncategorized with 1 Comment

On next October, 11 at Merb Camp will be released Merb 1.0.
Who are living on the Merb Edge knows and may be excited about it.
For a while i´m trying the 0.9.7 version.

Keep rocking.

Agile Business Word

26.09.08

Filed Under: agile, thoughts with 0 Comments

Is a fact that one of the greatest buzzwords at the moment is “Agile”. Is amazing how many companies starts trainning services without any earlier experience and another ones adopt “Agile” term without know anything about. And it just to say “I´m Agile, we are the top on delivery software”.

Some companies believe that working with a Task Board plus teams having daily meeting will solve all the problems or becomes the company as a “Agile Company”. Agile is about culture, it isn´t just about taking some practices and becomes a Agile firm, more than this “Agile” is about values. The practices are changing and evoluting everytime, but the values still there.

Start a Agile initiative in smalls or new companies is easier comparing to conservative or big companies. I guess when you have less people to treat and less root culture involved it´ll become easy, but try to do it in a bank. Can you imagine how difficult is to implement Agile practices in this kind of company?

There aren´t anything wrong with methodologies or practices, they are there to support you in a better way. The greatest problem is called people. As who sell services as who are start a agile initiative just behind a buzzword.

Working with M$-Excel Data using Ruby with ParseExcel

05.09.08

Filed Under: programming, ruby with 0 Comments

Did you face anytime with a job that you must get information from M$-Excel or any WorkSheet files?

If your answer is yes, ParseExcel is an easy way to do it. ParseExcel is a translation of a Perl package with the same name for Ruby.

Let´s suppose you have a Contact Sheet which contains all your contacts. A day you wake up totally boring to manage your contacts in that worksheet and decide import it to a System. This worksheet has two columns, the first one contains the “Name” of contact and the second one has the “Phone Number”. To proceed the import we must read the Sheet data and put it on the new repository.

To start you should download the resource file on http://download.ywesee.com/parseexcel/parseexcel-0.5.1.1.tar.bz2. After that decompress the file on your File system and then install the package as follow :

  $ruby install.rb config
  $ruby install.rb setup
  $ruby install.rb install

Once you install the package without errors, create a class here i named it as GetExcel(get_excel.rb).

To use ParseExcel you must declare in the beginning the file require 'parseexcel'.

For the next step, we´ll start open the file.

  workbook = Spreadsheet::ParseExcel.parse(/path/of/file/Contacts.xls)

The chunk above start to read the file and return into an object related information data. It may be either one or more worksheets. In our case we´ll work with only one.

After we get the worksheet data, let´s choose a worksheet to work.

  worksheet = workbook.worksheet(0)

Now you can iterate over all rows and get the data we are expecting.


 worksheet.each do |line|
  column_name = line[0].to_s
  column_phone = line[1].to_s
 end

Our final code will seems some like that :


#Some details was hidden....
require 'parseexcel'
require 'my_model'

class GetExcel

 def initialize(path_file)
  @workbook = Spreadsheet::ParseExcel.parse(path_file)
  @worksheet = @workbook.worksheet(0)
 end

 def import_and_insert
  @worksheet.each do |line|
   column_name = line[0].to_s
   column_phone = line[1].to_s

   my_model = MyModel.create(
    :name => column_name,
    :phone => column_phone
   )
  end
 end
end

ParseExcel is an easy way to work with worksheets. As everything in the Ruby World, it´s so Sexy.

I hope it helps you.

Keep rocking.

Erlang as a Ruby Virtual Machine.

13.08.08

Filed Under: erlang, programming, ruby with 1 Comment

In the Antares Trader´s blog there is an interesting article about a supposed Ruby Virtual Machine implementation build in Erlang. Of course, it´s a thought from the author about this possibility and it still not a reality.

Erlang is a powerfull programming language focused in concurrent proccessing common used on telecommunications market. Nowadays it has been used like a middleware in web applications, some like Delicious, Twitter , Facebook and many more.


Particularly i liked pretty much this idea, once Ruby running inside the Java Virtual Machine has been a good purpose joining the best stuffs about that platforms.

Article´s link : http://blog.antarestrader.com/?p=77

Keep rocking, learning and coding.

Open your Mind

11.08.08

Filed Under: thoughts with 0 Comments

The behavior of Software profissional, not all but someone smells funny. All the time you may face to posts like that :

Dev1 : “Rails is better.”

Dev2 : “But Rails doesn´t scale, Java is better.”

Dev1 : “Your application design is stupid. There are a lot of DTO´s, VO´s, Service Locators, Why ?”



I guess that Software professionals must be up-to-date with good pratices, new technologies and how it can help themselves day-by-day. As techonology as proccess development evolutes each day, or better each moment. We like professionals must be able to decide what tool, language, practices is much appropriate for each situation.

Who can say for us if Rails, Seaside, Merb, anything else are the right answer for all the doubts.

The question is, are you enough pragmatic to open your eyes about your behavior??

Think about it!!! Keep rocking, learning and coding.

Package Management like apt-get for Mac OS

28.07.08

Filed Under: mac with 0 Comments

Hi fellas,

I’m a Linux user for a long time, mainly Debian/Ubuntu distros. One of things which i was missing is apt-get. But like a new Mac OS user, i must use to new funcionalities..

Surfing on the web, i found a package manager called Fink. It’s easier to install and start, you should download the package and follow the instructions in the same section.

Keep rocking and coding.

Java ME emulator plugin for Netbeans on Mac OS

20.07.08

Filed Under: mac, mobile with 0 Comments


Recently i’m developing some applications for mobile devices. I bought a Macbook Air and to configure all environement i suffered a litle bit… LOL…

I found out a excelent plugin called Microemulator. If you are looking for a plugin, you can find it on http://www.microemu.org .

That’s it. Keep coding and Rocking.

Categories