Remote method calls in java, supporting distributed computing. In addition, modifications on the server can be made to reflect various clients.
Assume that the ip address of the server is 192.168.11.2,
The server code is as follows:
[Java]
/**
*
*/
Package com. vs. rmi;
Import java. rmi. Remote;
Import java. rmi. RemoteException;
Public interface Product extends Remote {
Public String getDescription () throws RemoteException;
}
/**
*
*/
Package com. vs. rmi;
Import java. rmi. Remote;
Import java. rmi. RemoteException;
Public interface Product extends Remote {
Public String getDescription () throws RemoteException;
}
[Plain]
Package com. vs. rmi;
Import java. rmi. RemoteException;
Import java. rmi. server. UnicastRemoteObject;
Public class ProductImpl extends UnicastRemoteObject implements Product {
Private String name;
Public ProductImpl () throws RemoteException
{
Super ();
Name = "my rmi ";
}
@ Override
Public String getDescription () throws RemoteException {
Return "hello world," + name;
}
}
Package com. vs. rmi;
Import java. rmi. RemoteException;
Import java. rmi. server. UnicastRemoteObject;
Public class ProductImpl extends UnicastRemoteObject implements Product {
Private String name;
Public ProductImpl () throws RemoteException
{
Super ();
Name = "my rmi ";
}
@ Override
Public String getDescription () throws RemoteException {
Return "hello world," + name;
}
}
[Java]
/**
*
*/
Package com. vs. rmi;
Import java. rmi. Naming;
Import java. rmi. RemoteException;
Import java. rmi. registry. LocateRegistry;
/**
* @ Author hadoop
*
*/
Public class ProductServer {
/**
* @ Param args
*/
Public static void main (String [] args ){
/*
* Create and install a security manager that supports RMI. As part of the Java Development Kit
*
* Applicable to RMI, the only one is RMISecurityManager.
*
*
*
* If (System. getSecurityManager () = null ){
*
* System. setSecurityManager (new RMISecurityManager ());
*
*}
*/
Try {
LocateRegistry. createRegistry (8808 );
ProductImpl server = new ProductImpl ();
Naming. rebind ("// 192.168.11.2: 8808/SAMPLE-SERVER", server );
System. out. println ("Remote Object Registration successful, RMI service started, waiting for the client to call ....");
} Catch (java.net. MalformedURLException me ){
System. out. println ("Malformed URL:" + me. toString ());
} Catch (RemoteException re ){
System. out. println ("Remote exception:" + re. toString ());
}
}
}
/**
*
*/
Package com. vs. rmi;
Import java. rmi. Naming;
Import java. rmi. RemoteException;
Import java. rmi. registry. LocateRegistry;
/**
* @ Author hadoop
*
*/
Public class ProductServer {
/**
* @ Param args
*/
Public static void main (String [] args ){
/*
* Create and install a security manager that supports RMI. As part of the Java Development Kit
*
* Applicable to RMI, the only one is RMISecurityManager.
*
*
*
* If (System. getSecurityManager () = null ){
*
* System. setSecurityManager (new RMISecurityManager ());
*
*}
*/
Try {
LocateRegistry. createRegistry (8808 );
ProductImpl server = new ProductImpl ();
Naming. rebind ("// 192.168.11.2: 8808/SAMPLE-SERVER", server );
System. out. println ("Remote Object Registration successful, RMI service started, waiting for the client to call ....");
} Catch (java.net. MalformedURLException me ){
System. out. println ("Malformed URL:" + me. toString ());
} Catch (RemoteException re ){
System. out. println ("Remote exception:" + re. toString ());
}
}
}
The Project interface must be packaged into a jar package and introduced in the client package.
The client code is as follows:
[Java]
Package com. vs. myrmi;
Import java. rmi .*;
Import com. vs. rmi. Product;
Public class RmiSampleClient {
Public static void main (String [] args)
{
Try {
String url = "// 192.168.11.2: 8808/SAMPLE-SERVER ";
Product product = (Product) Naming. lookup (url );
System. out. println (product. getDescription ());
} Catch (RemoteException exc ){
System. out. println ("Error in lookup:" + exc. toString ());
} Catch (java.net. MalformedURLException exc ){
System. out. println ("Malformed URL:" + exc. toString ());
} Catch (java. rmi. NotBoundException exc ){
System. out. println ("NotBound:" + exc. toString ());
}
}
}
Package com. vs. myrmi;
Import java. rmi .*;
Import com. vs. rmi. Product;
Public class RmiSampleClient {
Public static void main (String [] args)
{
Try {
String url = "// 192.168.11.2: 8808/SAMPLE-SERVER ";
Product product = (Product) Naming. lookup (url );
System. out. println (product. getDescription ());
} Catch (RemoteException exc ){
System. out. println ("Error in lookup:" + exc. toString ());
} Catch (java.net. MalformedURLException exc ){
System. out. println ("Malformed URL:" + exc. toString ());
} Catch (java. rmi. NotBoundException exc ){
System. out. println ("NotBound:" + exc. toString ());
}
}
}
First, execute the server to listen, and then execute the client. The result of the client execution is as follows:
[Plain]
Hello world, my rmi
Hello world, my rmi
In Java 1.6, you do not need to manually use the rmic command to generate a skeleton.