Lightweight RMI Communication with plain Java SE (Source Sample), without generated stubs or additonal frameworks

It seems like the Plain Old RMI (it is perfect for a new acronym: PORI) is almost forgotten. From time to time I use RMI to establish communication between EJBs and legacy or EJB-container problematic processes like native resources. To establish communication between Java objects in different JVMs you have only to follow the following steps:

1. Definition of the remote (service) interface:

import java.rmi.Remote;
import java.rmi.RemoteException;

public interface HelloWorldService extends Remote{

    public String helloFromServer(String greeting) throws RemoteException;
}

2. Implementation of the Interface (the servant). Its implements the business logic. Of course the logic can be also factored out in an independent POJO:

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

public class HelloWorldServant extends UnicastRemoteObject implements HelloWorldService{

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

    public String helloFromServer(String greeting) {
        System.out.println("Request received: " + greeting);
        return greeting + " time: " + new Date();
    }

}
3. You need also a class with a main method...(the actual server)


import java.rmi.registry.LocateRegistry;
import java.rmi.registry.Registry;

public class RemoteServer {

    public static void main(String[] args) throws Exception {
        Registry registry = LocateRegistry.createRegistry(1200); // this line of code automatic creates a new RMI-Registry. Existing one can be also reused.
        System.out.println("Registry created !");
        registry.rebind("hugo",new HelloWorldServant());
        System.out.println("Server is up and running !");
    }
}


4. ...and finally the client:

import java.rmi.Naming;

import com.abien.rmi.server.HelloWorldService;

public class HelloWorldClient {

    public static void main(String[] args) throws Exception{
        HelloWorldService helloWorldService = (HelloWorldService) Naming.lookup("rmi://localhost:1200/hugo");
        System.out.println("Output" + helloWorldService.helloFromServer("hello "));
    }

}

And now its time for the homework. Just implement the same stuff with SOAP or other technology, and compare the performance, complexity, amount of classes and additional jars with this solution. So just PORI :-)


Web Apps, SPA, PWA with vanilla Java Script (ES 6+), CSS 3 and WebStandards only. As simple as possible, but not simpler. See you at: (Progressive) Web apps, Single Page Apps and WebStandards airhacks workshops at MUC airport, Winter Edition

airhacks.fm the podcast:

Stay in touch: airhacks.news.

Comments:

Hallo Adam,

Ausgezeichneten entry. I too find RMI is refreshingly simple to use.

This current madness with XML-RPC, SOAP, WSDL, .... it baffles me. I wonder if some people just enjoy making their lives very complicated.

I run a small project to further simplify the commonly encountered "rough edges" with RMI; such as dealing with NAT, UDP, HTTP proxies, asynchronous callbacks to firewalled clients. It also adds a few really nice features, like integrated scripting, and transparent remoting of user interfaces. Shared interfaces are not even required between clients and servers. All of this comes in a 40kB jar, with no external depencencies, it runs on all JREs ME/SE/EE 1.2 and higher.

I would greatly appreciate if you could come and give it a look:

https://cajo.dev.java.net/overview.html

Your commmments, suggestions, or questions, would be most welcome.

Tschuss,

John

Posted by John Catherino on November 23, 2006 at 05:58 AM CET #

> This current madness with XML-RPC, SOAP, WSDL,
> .... it baffles me. I wonder if some people just
> enjoy making their lives very complicated.

I agree that using RMI can be simple, but it's not a language neutral technology and it just doesn't scale very well.

The advantage of this madness, as you put it, is that it offers remote communications with more than just applications written in Java to participate in the remote calls. Web services can really be beneficial when doing integration work amongst apps written in many languages.

The overhead in using lots of RMI without a pooling mechanism can be costly. I've experienced lot of RMI without mitigation for the overhead and it was *painful*. In addition, applications written in other languages weren't able to participate.

Posted by Bruce Snyder on December 04, 2006 at 02:45 AM CET #

Post a Comment:
  • HTML Syntax: NOT allowed
Online Workshops
realworldpatterns.com
...the last 150 posts
...the last 10 comments
License