RMI Implementation in Java
This is the java program to implement RMI in java.the program implemented is a calculator
The Java Remote Method Invocation (Java RMI) is a Java API that performs the object-oriented equivalent of remote procedure calls (RPC), with support for direct transfer of serialized Java classes and distributed garbage collection.
The original implementation depends on Java Virtual Machine (JVM) class representation mechanisms and it thus only supports making calls from one JVM to another. The protocol underlying this Java-only implementation is known as Java Remote Method Protocol (JRMP).
In order to support code running in a non-JVM context, a CORBA version was later developed.
rmi implementation in java,remote method invocation in java,rmi java program,rmi code in java
Implementation:
calcimp.java
import java.io.*;
import java.rmi.*;
import java.rmi.server.*;
public class calcimp extends UnicastRemoteObject implements calcint
{
public calcimp() throws RemoteException{}
public int add(int a,int b) throws RemoteException{return (a+b);}
public int sub(int c,int d) throws RemoteException{return (c-d);}
}
Interface
calcint.java
import java.rmi.*;
public interface calcint extends Remote
{
int add(int a,int b) throws RemoteException;
int sub(int c,int d) throws RemoteException;
}
Calculator Client
calcclient.java
import java.io.*;
import java.rmi.*;
public class calcclient
{
public static void main(String[] args) throws Exception
{
try
{
int ch,a,b,c,d;
DataInputStream inp=new DataInputStream(System.in);
calcint cont=(calcint)Naming.lookup("rmi://localhost/calcserv");
do
{
System.out.println("1.ADD 2.SUB");
ch=Integer.parseInt(inp.readLine());
switch(ch)
{
case 1:a=Integer.parseInt(inp.readLine());
b=Integer.parseInt(inp.readLine());
System.out.println("Result:"+cont.add(a,b));
break;
case 2:c=Integer.parseInt(inp.readLine());
d=Integer.parseInt(inp.readLine());
System.out.println("Result:"+cont.sub(c,d));
break;
case 3:break;
}
}
while(ch!=3);
}
catch(Exception eee){}
}
}
Calc Server
calcserv.java
import java.io.*;
import java.rmi.*;
public class calcserv
{
public static void main(String[] args) throws Exception
{
try
{
calcimp ci=new calcimp();
Naming.rebind("calcserv",ci);
}
catch(Exception as){}
}
}
Recent Comments