When I saw RMI, I first came up with this question: what is RMI?
Java RMI (Remote Method Invocation remote method call) is implemented in Java in jdk1.1, which greatly enhances Java's ability to develop distributed applications. Java, as a popular network development language, has great power in its powerful ability to develop distributed network applications, RMI is one of the core solutions for developing a network distributed application system with hundreds of thousands of pure Java. In fact, it can be regarded as the Java version of RPC. However, traditional RPC cannot be well applied to distributed object systems. Java RMI supports communication between Program-level objects stored in different address spaces to achieve seamless remote calls between remote objects. (Copy from the Internet, huh, huh)
What is the difference between it and WebService.
The client and server of RMI must be Java, and WebService does not have this restriction.
WebService transmits XML text files over HTTP, regardless of the language and platform.
RMI is a Java object that can be serialized over TCP. It can only be used on a Java virtual machine.
RMI is the basis for EJB remote calls. Remote calls can be implemented only using RMI technology. EJB is used to implement components, transactions, resource pools, clusters, and other functions.
WebService uses XML to transmit data. It can be transmitted between heterogeneous systems through http and other protocols, and can be remotely called on the public network through the firewall.
A small example is provided:
This article from the "brothers" blog, please be sure to keep this source http://llwbrothers.blog.51cto.com/2360705/537086
Perform the following steps:
1. Create a remote interface and declare a remote method (HelloInterface. java)
2. implement remote interfaces and remote methods (inherit UnicastRemoteObject) (Hello. java)
3. Start RMI Registration Service and register remote objects (HelloServer. java)
4. The client searches for remote objects and calls the remote method (HelloClient)
5. Execute the program: start the service HelloServer; run the client HelloClient to call
Package com. unmi;
Import java. rmi .*;
/**
* The Remote interface must be extended to java. rmi. Remote.
*/
Public interface HelloInterface extends Remote
{
/**
* The remote interface method must throw java. rmi. RemoteException.
*/
Public String say () throws RemoteException;
}
Package com. unmi;
Import java. rmi .*;
Import java. rmi. server .*;
/**
* Extends the UnicastRemoteObject class and implements the remote interface HelloInterface
*/
Public class Hello extends UnicastRemoteObject implements HelloInterface
{
Private String message;
/**
* The constructor must be defined. Even the default constructor must be explicitly written because it must throw a RemoteException.
*/
Public Hello (String msg) throws RemoteException
{
Message = msg;
}
/**
* Remote interface method implementation
*/
Public String say () throws RemoteException
{
System. out. println ("Called by HelloClient ");
Return message;
}
}
Package com. unmi;
Import java. rmi. Naming;
Public class HelloClient
{
/**
* Search for remote objects and call remote methods
*/
Public static void main (String [] argv)
{
Try
{
HelloInterface hello = (HelloInterface) Naming. lookup ("Hello ");
// If you want to find the hello instance from another machine that has started the RMI Registration Service
// HelloInterface hello = (HelloInterface) Naming. lookup ("// 192.168.1.105: 1099/Hello ");
// Call the Remote Method
System. out. println (hello. say ());
}
Catch (Exception e)
{
System. out. println ("HelloClient exception:" + e );
}
}
}
Package com. unmi;
Import java. rmi. Naming;
Import java. rmi. registry. LocateRegistry;
Public class HelloServer
{
/**
* Start RMI Registration Service and register objects
*/
Public static void main (String [] argv)
{
Try
{
// Start the RMI registration service and set the port to 1099 (1099 as the default port)
// You can also run the $ java_home/bin/rmiregistry 1099 command to start
// This method is used to avoid opening another DOS window.
// You must use the rmiregistry command to start the registration service. You must use RMIC to generate a stub class for this purpose.
LocateRegistry. createRegistry (1099 );
// Create one or more instances of the remote object. The following is the hello object.
// You can register different instances with different names.
HelloInterface hello = new Hello ("Hello, world! ");
// Register hello on the RMI registration server and name it Hello
Naming. rebind ("Hello", hello );
// If you want to register the hello instance to another machine that has started the RMI registration service
// Naming. rebind ("// 192.168.1.105: 1099/Hello", hello );
System. out. println ("Hello Server is ready .");
}
Catch (Exception e)
{
System. Out. println ("Hello server failed:" + E );
}
}
}