Export Object over RMI example
This is an example of how to Export Object over RMI. The Java Remote Method Invocation (RMI) system allows an object running in one Java virtual machine to invoke methods on an object running in another Java virtual machine. In this example, we have created a Class, the CalculatorServiceImpl
, that will be used as the Object to be exported over RMI. It consists of two methods, the add(int num1, int num2)
method and the div(double num1, double num2)
method. Exporting the CalculatorService
over RMI implies that you should:
- Define a remote object interface, the
CalculatorService
that must extend the Remote Interface. - Define the remote object implementation, the
CalculatorServiceImpl
that must extend the UnicastRemoteObject Class. - Compile the
CalculatorServiceImpl
and generate skeletons and stubs. - Start up the RMI registry so as to create and export remote objects.
- Export the remote object. Create a new
CalculatorService
instance and bind it to the RMI registry, using the java.rmi.Naming Class and specifically therebind(String name, Remote obj)
API method. TheCalculatorService
object is now associated with a specified name and it can be looked up by it, as demonstrated to theExportObjectToRMI
Class. - Then look up the remote object, with the
lookup(String name)
API method and invoke one of its exposed methods.
Note that in the example, the CalculatorServiceImpl
implements two methods that must throw RemoteException. Any other exception must be wrapped by the RemoteException.
Let’s take a look at the code snippets that follow:
Define the remote interface
package com.javacodegeeks.snippets.core; import java.rmi.*; // Method attributes and return types can be primitives or Objects implementing the Serializable interface public interface CalculatorService extends Remote { int add(int num1, int num2) throws RemoteException; double div(double num1, double num2) throws RemoteException; }
Define the remote object implementation
package com.javacodegeeks.snippets.core; import java.rmi.*; import java.rmi.server.UnicastRemoteObject; public class CalculatorServiceImpl extends UnicastRemoteObject implements CalculatorService { public CalculatorServiceImpl() throws RemoteException { super(); } // All remote methods must throw RemoteException public int add(int num1, int num2) throws RemoteException { return (num1 + num2); } public double div(double num1, double num2) throws RemoteException { if (num2 == 0) { // The actual exception must be wrapped in a RemoteException throw new RemoteException("Divided by zero!", new IllegalArgumentException("Divided by zero!")); } return (num1 / num2); } }
Compile the remote object implementation and generate skeletons and stubs
> javac CalculatorService.java CalculatorServiceImpl.java > rmic CalculatorServiceImpl
Start up the RMI registry that allows us to create and export remote objects
> rmiregistry
Export remote object, look it up and invoke its methods
package com.javacodegeeks.snippets.core; import java.net.MalformedURLException; import java.rmi.Naming; import java.rmi.NotBoundException; import java.rmi.RemoteException; import java.rmi.UnknownHostException; public class ExportObjectToRMI { public static void main(String[] args) { // Create an instance of the remote object and bind it to the RMI registry try { CalculatorService calculatorService = new CalculatorServiceImpl(); Naming.rebind("//localhost/CalculatorService", calculatorService); } catch (MalformedURLException e) { } catch (UnknownHostException e) { } catch (RemoteException e) { } // Look up the remote object and invoke the exposed methods try { CalculatorService calculatorService = (CalculatorService) Naming.lookup("//localhost/CalculatorService"); double result = calculatorService.div(10, 5); System.out.println("Division 10/5 result : " + result); } catch (MalformedURLException e) { } catch (UnknownHostException e) { } catch (NotBoundException e) { } catch (RemoteException e) { // Get the actual exception that was thrown. Throwable realException = e.detail; } } }
Output:
Division 10/5 result : 2.0
This was an example of how to export an Object over an RMI in Java.