Use rmi for remote method calling in java

Source: Internet
Author: User

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.

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.