Q. what is rmi in java ?
Ans:RMI is an acronym for Remote Method Invocation. Before understanding what is RMI one should know what is distributed computing.
Distributed computing: suppose you have an Online shopping application(say Flipkart.com) implemented in java technology.Now, a user selects a book to buy and he has to pay through card.Now for this, Flipkart application has to interact with the Bank application of the customer (say SBI) which may be implemented in different technology (say .NET). Amount has to be deducted from the customer's account and should be deposited to Flipkart's Bank account (say ICICI) which is implemented in different technology (say php).Now, these three applications Flipkart.com,SBI bank and ICICI bank belonging to three different organisations has to communicate and share information.This kind of cases needs distributed environment. With distributed applications, one organization can expose remote business services to their business partners and other organizations acting as business partners can consume the remote business services.
To develop the distributed applications, you need distributed technologies like RMI,EJB,webservices etc.
Distributed applications can be of two types---
To develop the distributed applications, you need distributed technologies like RMI,EJB,webservices etc.
Distributed applications can be of two types---
1. Homogenous applications
2. Hetrogenous applications
1. Homogenous applications: When two applications which are implemented in same technology standards are communicating then those applications are called as Homogenous applications .
EX:Communication between two java based applications.
Communication between two .net based applications.
Note: Data will be passed from one application to another application as an Object.
2. Hetrogenous applications: When two applications which are implemented in different technology standards are communicating then those applications are called as Hetrogenous applications .
EX:Communication between java based application and .net based application.
Note: Data will be passed from one application to another application as an XML document.
RMI (Remote Method Invocation):
RMI is a distributed technology which allows you to do two things mainly …….
1) Developing and exposing remote business services.
2) Consuming Remote business services.
Software required: JDK
RMI Example:
A) Server Side Steps:
1. Create the java project with the name ServerApp.
2. Create the package called com.jlcindia.rmi.
3. Write the following ……
a. HelloService.java
b. HelloServiceImpl.java
c. ServiceRegistry.java
Files Required:
A. HelloService.java:
package com.manish.rmi;
import java.rmi.Remote;
import java.rmi.RemoteException;
import java.rmi.Remote;
import java.rmi.RemoteException;
public interface HelloService extends Remote
{
public String getMessage(String name)throws RemoteException;
}
B. HelloServiceImpl.java:
package com.manish.rmi;
import java.rmi.*;
import java.rmi.server.*
public class HelloServiceImpl extends UnicastRemoteObject implements HelloService
{
private static final long serialVersionUID = 1L;
import java.rmi.*;
import java.rmi.server.*
public class HelloServiceImpl extends UnicastRemoteObject implements HelloService
{
private static final long serialVersionUID = 1L;
public HelloServiceImpl() throws RemoteException
{
super();
}
@Override
public String getMessage(String name) throws RemoteException
{
return "Hello"+name+"! Welcome to RMI world";
}
}
C. ServiceRegistry.java:
package com.manish.rmi;
import java.rmi.registry.*;
public class ServiceRegistry
{
public static void main(String[] args)
{
try
{
Registry reg=LocateRegistry.createRegistry(432);
HelloService h=new HelloServiceImpl();
reg.bind("HELLO",h);
System.out.println("binded successfully");
}
catch(Exception e)
{
e.printStackTrace();
}
}
}
import java.rmi.registry.*;
public class ServiceRegistry
{
public static void main(String[] args)
{
try
{
Registry reg=LocateRegistry.createRegistry(432);
HelloService h=new HelloServiceImpl();
reg.bind("HELLO",h);
System.out.println("binded successfully");
}
catch(Exception e)
{
e.printStackTrace();
}
}
}
Client side Steps:
1. Create the java project with name ClientApp.
2. Create the package called com.jlcindia.rmi.
3. Write the following ………
a. HelloClient.java
b. Copy HelloService.java to com.jlcindia.rmi package or add
jlcindia. jar to project build path.
c. Run ServiceRegistry
d. Run HelloClient
c. Run ServiceRegistry
d. Run HelloClient
D. RmiClient.java:
package com.manish.rmi;
import java.rmi.registry.*;
public class RmiClient
{
public static void main(String[] args)
{
try
{
Registry reg=LocateRegistry.getRegistry("localhost",432);
Object obj=reg.lookup("HELLO");
HelloService hs=(HelloService)obj;
String msg=hs.getMessage("Manish");
System.out.println(msg);
}
catch(Exception e)
{
e.printStackTrace();
}
}
}
import java.rmi.registry.*;
public class RmiClient
{
public static void main(String[] args)
{
try
{
Registry reg=LocateRegistry.getRegistry("localhost",432);
Object obj=reg.lookup("HELLO");
HelloService hs=(HelloService)obj;
String msg=hs.getMessage("Manish");
System.out.println(msg);
}
catch(Exception e)
{
e.printStackTrace();
}
}
}
Explanation: Write any business interface (e.g HelloService.java) by extending Remote interface and in that interface write any method that you want to expose to the client.Provide the implementation class for the interface and override the methods of the interface (e.g HelloServiceImpl).Now create the Service class (ServiceRegistry.java) to bind our HelloServiceImpl object in the JNDI registry.JNDI Registry is a simple registry which holds the object with logical name.Create the Registry object ----
Registry reg=LocateRegistry.createRegistry(432);
Now, create the HelloServiceImpl object and bind the object in the RMI registry----
HelloService h=new HelloServiceImpl();
reg.bind("HELLO",h);
reg.bind("HELLO",h);
Note: HELLO is the logical name of HelloServiceImpl object.
Now,create a different client application and write a class (RmiClient.java) to look up the JNDI registry and access the getMessage() method exposed by the server.
Registry reg=LocateRegistry.getRegistry("localhost",432);
Object obj=reg.lookup("HELLO");
HelloService hs=(HelloService)obj;
String msg=hs.getMessage("Manish");
System.out.println(msg);
Object obj=reg.lookup("HELLO");
HelloService hs=(HelloService)obj;
String msg=hs.getMessage("Manish");
System.out.println(msg);
.....................................................................................................................................
No comments:
Post a Comment