StackStalk
  • Home
  • Java
    • Java Collection
    • Spring Boot Collection
  • Python
    • Python Collection
  • C++
    • C++ Collection
    • Progamming Problems
    • Algorithms
    • Data Structures
    • Design Patterns
  • General
    • Tips and Tricks

Saturday, October 25, 2014

Java RMI

 October 25, 2014     Java     No comments   

Java RMI Introduction

Java Remote Method Invocation (RMI) enables remote communication between programs running across different Java Virtual Machines (JVM). RMI essentially allows an object running in one JVM to invoke a method on another object running in a different JVM. Java RMI provides the ability to write distributed objects in a simple and secure manner with the power of passing complete objects as arguments and return values.

Java RMI applications typically have a client and server. A server program binds the remote objects via registry and makes it accessible by clients. The client program typically does a look-up for the remote objects in the registry, obtains a remote object reference and perform a method call on the remote object. RMI is the mechanism which the server and client use to exchange the information.

Java RMI Application Architecture

Some of the key Java interfaces and classes necessary to understand and build an RMI application are listed below.

  • java.rmi.Remote - Marker interface that needs to be extended directly or indirectly by a remote interface. Refer to Java RMI Interface Example below.
  • java.rmi.RemoteException - Superclass of all exceptions thrown by the RMI runtime during the remote method invocation.
  • java.rmi.server.UnicastRemoteObject - Provides the server function and provides a singleton remote object.
Note: To use this application invoke the rmiregistry application which is a simple bootstrap nameserver that comes with the Java distribution.

Java RMI Interface Example

First step in a RMI application is to design the remotely accessible part of the application from another Java virtual machine. This is achieved by extending the java.rmi.Remote interface. In this example we define a simple interface with a single method to be invoked over RMI.

package com.sourcetricks.intf;

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

public interface ComputeIntf extends Remote {
 int doubleThis(int input) throws RemoteException;
}

Java RMI Server Example

To implement a RMI server the remote object usually extends the UnicastRemoteObject and implements the remote interface we have defined earlier. Then we create a Registry instance and then bind the remote object reference to a specific name which can be accessed by client program over RMI.

package com.sourcetricks.server;

import java.rmi.RemoteException;
import java.rmi.registry.LocateRegistry;
import java.rmi.registry.Registry;
import java.rmi.server.UnicastRemoteObject;

import com.sourcetricks.intf.ComputeIntf;

public class RmiServer extends UnicastRemoteObject 
implements ComputeIntf {
  
  private static final long serialVersionUID = 1L;
  
  protected RmiServer() throws RemoteException {
    super();
  }

  @Override
  public int doubleThis(int input) throws RemoteException {     
    return (input * 2);   
  }

  public static void main(String[] args) {
    try {
      // Name to be used in the registry
      String name = "ComputeEngine";
      
      // The remote object
      RmiServer engine = new RmiServer();
      
      // Creates and exports a Registry instance on  
      // the local host that accepts requests on the 
      // specified port.
      Registry registry = LocateRegistry.createRegistry(3500);
      
      // Replaces the binding for the specified name 
      // in this registry with the supplied remote ref. 
      // If there is an existing binding for the specified 
      // name, it is discarded.
      registry.rebind(name, engine);
      
      System.out.println("RmiServer bound");
        
    } catch (Exception e) {
        System.err.println("RmiServer exception:");
        e.printStackTrace();
    }
  }
}

Java RMI Client Example

Writing an RMI client application to access a remote object is simple. In the example below we locate the remote object registry on a specified port. We look-up the registry with the specific name and get a reference to the remote object. Then we invoke the remote method similar to a local method and the invocation happens over RMI.

package com.sourcetricks.client;

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

import com.sourcetricks.intf.ComputeIntf;

public class RmiClient {

  public static void main(String args[]) {
    try {
      String name = "ComputeEngine";
      
      // Returns a reference to the the remote object
      // Registry for the local host on the specified
      // port.
      Registry registry = LocateRegistry.getRegistry(3500);
      
      // Returns the remote reference bound to the  
      // specified name in this registry.
      ComputeIntf comp = (ComputeIntf) registry.lookup(name);     
      
      System.out.println(comp.doubleThis(10));
      
    } catch (Exception e) {
      System.err.println("RmiClient exception:");
      e.printStackTrace();
    }
  }   
}

Java RMI References

  1. http://docs.oracle.com/javase/7/docs/platform/rmi/spec/rmiTOC.html
  2. http://docs.oracle.com/javase/tutorial/rmi/overview.html

Read more Java tutorials.

Email ThisBlogThis!Share to XShare to Facebook
Newer Post Older Post Home

0 comments:

Post a Comment

Follow @StackStalk
Get new posts by email:
Powered by follow.it

Popular Posts

  • Python FastAPI file upload and download
    In this article, we will look at an example of how to implement a file upload and download API in a Python FastAPI microservice. Example bel...
  • Avro Producer and Consumer with Python using Confluent Kafka
    In this article, we will understand Avro a popular data serialization format in streaming data applications and develop a simple Avro Produc...
  • Monitor Spring Boot App with Micrometer and Prometheus
    Modern distributed applications typically have multiple microservices working together. Ability to monitor and manage aspects like health, m...
  • Server-Sent Events with Spring WebFlux
    In this article we will review the concepts of server-sent events and work on an example using WebFlux. Before getting into this article it ...
  • Accessing the Kubernetes API
    In this article, we will explore the steps required to access the Kubernetes API and overcome common challenges. All operations and communic...
  • Python FastAPI microservice with Okta and OPA
    Authentication (AuthN) and Authorization (AuthZ) is a common challenge when developing microservices. In this article, we will explore how t...
  • Scheduling jobs in Python
    When developing applications and microservices we run into scenarios where there is a need to run scheduled tasks. Examples include performi...
  • Using Tekton to deploy KNative services
    Tekton is a popular open-source framework for building continuous delivery pipelines. Tekton provides a declarative way to define pipelines ...

Copyright © StackStalk