Sitemap

Distributed Computing

Remote deployment with RMI

Method calls are always between two objects on the same heap.

In most applications, when one object calls a method on another, both objects are on the same heap. In other words, both are running within the same JVM.

JVM can know about references on only its own heap. a JVM running on one machine can’t know anything about a different JVM running on the same machine.

Object A, running on Little, wants to call a method on Object B, running on Big.

We can not do this. we can’t get a reference to something on another heap. That’s why the RMI usage is important here.

RMI — Remote Method Invocation

A design for remote method calls

Create four things: server, client, server helper, client helper

  1. Create client and server apps. The server app is the remote service that has an object with the method that the client wants to invoke.
  2. Create client and server ‘helpers’. They’ll handle all the low-level networking and I/O details so your client and service can pretend like they’re in the same heap.
Press enter or click to view image in full size

Helpers

Client object gets to act like it’s making remote method calls. But actually It is calling methods on a heap-local ‘proxy’ object that handles all the low-level details of Sockets and stream.

Press enter or click to view image in full size

How the method call happens

  • Client object calls doBigThing() on the client helper object.
  • Client helper packages up information about the call (arguments, method name, etc.) and ships it over the network to the service helper.
  • Service helper unpacks the information from the client helper, finds out which method to call (and on which object) and invokes the real method on the real service object.

In RMI, the client helper is a stub and the server helper is a skeleton.

Press enter or click to view image in full size

The Remote Service

  • Make a Remote Interface
  • Make a Remote Implementation
  • Generate the stubs and skeletons using rmic
  • Start the RMI registry (rmiregistry)
  • Start the remote service

Make a Remote Interface

  • Extend java.rmi.Remote

public interface MyRemote extends Remote {

  • Declare that all methods throw a RemoteException

import java.rmi.*;

  • Be sure arguments and return values are primitives or Serializable

public String sayHello() throws RemoteException;

Make a Remote Implementation

  • Implement the Remote interface

public class MyRemoteImpl extends UnicastRemoteObject implements MyRemote {

public String sayHello() {

return “Server says, ‘Hey’”;

} // more code in class

}

  • Extend UnicastRemoteObject

public class MyRemoteImpl extends UnicastRemoteObject implements MyRemote{

  • Write a no-arg constructor that declares a RemoteException

public MyRemoteImpl() throws RemoteException { }

Generate stubs and skeletons

  • Run rmic on the remote implementation class (not the remote interface)

Run rmiregistry

  • Bring up a terminal and start the rmiregistry.

Start the service

  • Bring up another terminal and start your service

Complete code for the server side

Press enter or click to view image in full size

How does the client get the stub object.

Get Vetharsana Thangarajah’s stories in your inbox

Join Medium for free to get updates from this writer.

The client has to get the stub object, since that’s the thing the client will call methods on.

MyRemote service = (MyRemote) Naming.lookup(“rmi://127.0.0.1/Remote Hello”);

  • Client does a lookup on the RMI registry.
  • RMI registry returns the stub object.
  • Client invokes a method on the stub, as though the stub IS the real service.

Complete client code

Press enter or click to view image in full size

Servlets

Servlets are Java programs that run on (and with) an HTTP web server.

Even servlets can use RMI.

The most common use of J2EE technology is to mix servlets and EJBs together, where servlets are the client of the EJB. And in that case, the servlet is using RMI to talk to the EJBs.

  1. Client fills out a registration form and clicks ‘submit’. The HTTP server (i.e. web server) gets the request, sees that it’s for a servlet, and sends the request to the servlet.
  2. Servlet (Java code) runs, adds data to the database, composes a web page (with custom info) and sends it back to the client where it displays in the browser.

Steps for making and running a servlet

  1. Find out where your servlets need to be placed.
  2. Get the servlets.jar and add it to your classpath.
  3. Write a servlet class by extending HttpServlet.
  4. Write an HTML page that invokes your servlet.
  5. Make your servlet and HTML page available to your server.

simple Servlet

Press enter or click to view image in full size

Enterprise JavaBeans: RMI on steroids

An EJB server adds a bunch of services that you don’t get with straight RMI. Things like transactions, security, concurrency, database management, and networking.

An EJB server steps into the middle of an RMI call and layers in all of the services.

Press enter or click to view image in full size

Written by Vetharsana Thangarajah

I am Computer Science graduate, worked as an Associate Software Engineer - Java at Virtusa for 3 years. looking for a job to continue my career in Canada.