Monday, June 3, 2013

How to use and advantages of Spring RMI

My current project is java RMI (remote method invocation) project and we are using spring as main framework. There are lots of advantages when you use spring framework for RMI. Before I go father I will show you RMI example without using spring framework.

Create an interface name Hello.java and declare sayHello method without body as per the requirement of a simple Hello RMI application.


import java.rmi.Remote;

public interface Hello extends Remote{
    public String sayHello();
}

The next step is to implement the interface so define a class.

import java.rmi.RemoteException;
import java.rmi.server.UnicastRemoteObject;

public class HelloImpl extends UnicastRemoteObject implements Hello {

    protected HelloImpl() throws RemoteException {
        super();
    }

    @Override
    public String sayHello() {
        System.out.println("Hello");
        return "Hello";
    }
}

Define the Server side class.

import java.rmi.Naming;
public class HelloServer {

    public HelloServer() {
        try {
            Hello h = new HelloImpl();
            Naming.rebind("rmi://localhost:1099/HelloService", h);
        } catch (Exception e) {
            System.out.println("Exception is : " + e);
        }
    }
    
    public static void main(String[] args) {
        new HelloServer();
    }
}

Now server side code is over. Define the Client side class to access sayHello method.
import java.rmi.Naming;

public class HelloClient {
    public static void main(String[] args) {
        try {
            Hello h = (Hello) Naming.lookup("//127.0.0.1:1099/HelloService");
            System.out.println("Say : " + h.sayHello());
        } catch (Exception ex) {
            System.out.println("Error " + ex);
        } 
            
    }
}

These are the steps of RMI example without spring. Now check how Spring framework can implement same example.

package com.sasika.hello;

public interface Hello{
    public String sayHello();
}

You don’t need to extend Hello java interface from Remote Interface. You don’t need to extend UnicastRemoteObject as well.

package com.sasika.hello;
public class HelloImpl implements Hello {

    @Override
    public String sayHello() {
        System.out.println("Hello");
        return "Hello";
    }
}
We can export the interface of our HelloImpl object as RMI object. To do that we can use spring RmiServiceExporter. Next code sample shows how to export our business logic(HelloImpl) to RMI service using spring.
For more detail visit: http://static.springsource.org/spring/docs/2.0.8/reference/remoting.html



   
        
   

   
   

   
   


That’s all for server side. Now look at the client side code. We need to write SpringConfiguration.xml for access Rmi service.





        
        




public class HelloClient {
    public void callHello() {
        try {
            ApplicationContext ctx =  new ClassPathXmlApplicationContext("classpath:com/sasika/hello/SpringConfiguration.xml");
            Hello h = (UserRolePermissionBo) ctx.getBean("helloBean");
            System.out.println("Say : " + h.sayHello());
        } catch (Exception ex) {
            System.out.println("Error " + ex);
        } 
            
    }
}


This is how we develop RMI project with spring. So what are the advantages of using spring RMI.

There are few advantages. One is you don’t need to extend any RMI related Interfaces. So you can concentrate your application business logic rather than RMI specific codes. When you don’t extend RMI related Interfaces your application can export any other services without changing your existing code.

For example if you need to write Hello web service, you can write another class (HelloWs) and call existing Hello java class method. You don’t need to QA for existing code.Same as you can write web client or any other service without changing existing code.

This will improve your system Flexibility, Maintainability, and Extensibility.

In here with spring, RMI specific codes move into xml file. This reduces complexity of codes. Hiding complexity is another advantage.