I have been very comfortable recently, and I feel very fulfilled every day. When I pay attention to some of my life and work skills, I will find that my life can also be so fascinating and satisfying, and I feel very happy. The reason for learning java RMI is that I recently used the dubbo framework to build a system. So I learned this java RMI and made some notes, basic articles, and selective reading. [Definition] Java RMI: Java Remote Method call, that is, Java RMI (Java Remote Method Invocation) is an application programming interface used to implement Remote Procedure Call in Java programming language. It allows programs running on the client to call objects on the remote server. The remote method call feature enables Java programmers to distribute operations in the network environment. The purpose of RMI is to simplify the use of remote interface objects as much as possible. We know that Remote Procedure Call (RPC) can be used by one process to Call another process (probably on another Remote host), thus providing process distribution. Java RMI is a step forward on the basis of RPC, that is, to provide communication between distributed objects. RMI (Remote Method Invocation) is a Remote Method call that allows an object running on a Java Virtual Machine to call an object running on another Java virtual machine. The two virtual machines can be running in different processes on the same computer or on different computers on the network. [JavaRMI] I. Working Principle RMI allows a Java program to call the Java object method of another computer in the network. The Calling effect is like calling on the local machine. Generally speaking, machine A has A class on it. Through Remote Call, machine B calls the method in this class. Remote Method Invocation (RMI) is the pillar of Enterprise JavaBeans and a convenient way to build distributed Java applications. RMI is very easy to use, but it is very powerful. The foundation of RMI is interfaces. the RMI architecture is based on an important principle: the specific implementation of defining interfaces and defining interfaces is separated. The following example shows how to create a simple Remote Computing Service and the customer program that uses it. 2. RMI includes: the remote service interface defines the specific implementation of the remote service interface (Stub) and the Framework (Skeleton) file, a server running the remote service, and an RMI Naming Service, it allows the client to discover the provider of this remote service file (an HTTP or FTP Server), a client program that requires this remote service. 3. What is the purpose of RMI? RMI is used to provide distributed services for remote communication between distributed Java applications. At present, the main applications are encapsulated in various J2EE project frameworks, such as Spring, EJB (both Spring and EJB encapsulate RMI technology) to implement RMI in Spring: ① define service interfaces on the server side and define specific classes to implement these interfaces; ② use org. springframework. remoting. rmi. rmiServiceExporter class to register the service; ③ use org. springframework. remoting. rmi. rmiProxyFactoryBean is used to implement the remote service proxy function; ④ define the same access class as the server service interface on the client. 4. Limitations of RMI? RMI currently uses Java Remote message exchange Protocol JRMP (Java Remote Messaging Protocol) for communication. JRMP is a protocol designed for Java remote objects. Because JRMP is designed for Java objects, RMI does not support application systems developed in non-Java languages. It cannot communicate with objects written in non-Java languages (meaning it only supports remote calls of Java program code on both the client and server ). 5. What are the limitations of RMI? Because both the client and the server are written in Java, the platform compatibility requirement of the two is that both parties are running on a Java virtual machine compatible with the version. Vi. RMI call parameters and return values of remote methods when calling methods on a remote object, the client can take the raw data as a parameter, objects can also be passed as parameters. The response is the return value, which can return the original type or object. These are implemented through Java object serialization (serialization) technology. (In other words, the parameter or return value must implement the Serializable interface if it is an object) 7. Basic Model of the RMI application 8. Stub/Skeleton layer: the framework of the client and the server; remote reference layer: transport: Connection establishment and management, and Remote Object Tracking 9. RMI class and interface (complete the class required for a simple RMI ). (1) Remote interface: The Public interface Remote {} interface that does not define a method. In RMI, the Remote interface declares a set of methods that can be called from a Remote Java virtual machine. The remote interface meets the following requirements: 1. The remote interface must directly or indirectly expand Java. rmi. the Remote interface must be declared as public, unless the client is in the same package as the Remote interface. 2. When the methods in the Remote interface are declared, in addition to throwing an application-related field, it must also include a RemoteException (or its superclass, IOExcepion or Exception) Exception. 3. In the remote method declaration, the remote object declared as a parameter or return value must be declared as a remote interface, rather than the implementation class of this interface. (2) The RemoteObject abstract class implements the Remote interface and Serializable interface. It and its subclass provide RMI server functions. (3) The LocateRegistry final () class is used to obtain reference to the remote object registration server program of a specific host (that is, create stub ), or create a remote object registration service program that can receive calls on a specific port. Server: Provide the remote object service SomeService servcie = to other clients ......; // Remote Object Service Registry = LocateRegisty. getRegistry (); // Registry is an interface that inherits Remote. This method returns a reference from the local host to the Remote object Registry on the default Registry port 1099. GetRegistry (int port) returns the local host's reference to the remote object Registry on the specified port; getRegistry (String host) returns the reference of the specified host on the default Registry port 1099 to the remote object Registry; getRegistry (String host, int port) returns the reference Registry to the remote object registry on the specified host and port. bind ("I serve", service); // bind (String name, Remote obj) is bound to the Remote reference of the name specified in this registry. Name: name obj related to the Remote Reference: removes the binding of the specified name in the registry from the reference unbind (String name) of the remote object (usually a stub. Rebind (String name, Remote obj) re-binding. If the name already exists, it will be replaced if it is different from Remote. If it is the same as Remote, the existing lookup (String name) bound will be discarded) return the remote reference bound to the specified name in the registry, return RemoteString [] list (), and return the array of names bound to the Registry. The array will contain a name snapshot bound when this method is called in the registry. Client: provide corresponding service requests to the server. Registry registry = LocateRegisty. getRegistry (); SomeService servcie = (SomeService) registry. lookup ("I serve"); Servcie. requestService (); (4) the Naming class is similar to the Registry class. Client: Naming. the format of lookup (String url) url is as follows: "rmi: // localhost/" + Remote Object Reference server: Registry registry = LocateRegistry. createRegistry (int port); Naming. rebind ("service", service); (5) the RMISecurityManager class is in the RMI reference program. If the security manager is not set, stub and class can only be loaded from the local class path, this ensures that the application is not affected by the code downloaded by remote method calls. before downloading code from the remote host, you must run the following code to install RMISecurityManager: System. setSecurityManager (new RMISecurityManager (); 10. to compile a demo, the demo is divided into two parts: the server code and the cli Client code is mainly used to use the server code. Of course, this code is very simple, just to illustrate the problem, practical use will be more complex. (1) Our goal is to create a server-side java project, including remote code, interface definition, interface implementation, and then create a client-side java project, use methods in remote services through RMI. (2) code structure (3) remote service code 1. The first step in defining remote service interfaces is to establish and compile the Java code of the service interfaces. This interface defines all functions that provide remote services. The following is the source code: UserManagerInterface. java copy code 1 package cn.com. tt. rmiserver. stub; 2 3 import java. rmi. remote; 4 import java. rmi. remoteException; 5 6 import cn.com. tt. rmiserver. bean. account; 7 8 public interface UserManagerInterface extends Remote {9 public String getUserName () throws RemoteException; 10 public Account getAdminAccount () throws RemoteException; 11} the copy code interface must inherit the Remote class, each definition All local methods must throw a RemoteException object. 2. the second step is to implement the above interface: UserManagerImp. java copy code 1 package cn.com. tt. rmiserver; 2 3 import java. rmi. remoteException; 4 5 import cn.com. tt. rmiserver. stub. userManagerInterface; 6 import cn.com. tt. rmiserver. bean. account; 7 8 public class UserManagerImp implements UserManagerInterface {9 public UserManagerImp () throws RemoteException {10 11} 12 private static final long serialVersionUID =-31114 92742628447261L; 13 14 public String getUserName () throws RemoteException {15 return "TT"; 16} 17 public Account getAdminAccount () throws RemoteException {18 Account account Account = new Account (); 19 account. setUsername ("TT"); 20 account. setPassword ("123456"); 21 return account; 22} 23} copy code 3. define a bean to implement the implements Serializable serialization interface. That is, the serializable object that can be transmitted on the client and server. Account. java copy code 1 package cn.com. tt. rmiserver. bean; 2 3 import java. io. serializable; 4 5 public class Account implements Serializable, Cloneable {6 private static final long serialVersionUID =-1858518213668584532l; 7 private String username; 8 private String password; 9 10 public String getUsername () {11 return username; 12} 13 public void setUsername (String username) {14 this. username = username; 15} 16 public String getPassword () {17 return password; 18} 19 public void setPassword (String password) {20 this. password = password; 21} 22} copy code 4. define the main program portal of the server. Entry. java copy code 1 package cn.com. tt. rmiserver. entry; 2 3 import java. rmi. alreadyBoundException; 4 import java. rmi. remoteException; 5 import java. rmi. registry. locateRegistry; 6 import java. rmi. registry. registry; 7 import java. rmi. server. unicastRemoteObject; 8 9 import cn.com. tt. rmiserver. userManagerImp; 10 import cn.com. tt. rmiserver. stub. userManagerInterface; 11 12 public class Entry {13 public stati C void main (String [] args) throws AlreadyBoundException, RemoteException {14 UserManagerImp userManager = new UserManagerImp (); 15 UserManagerInterface userManagerI = (UserManagerInterface) UnicastRemoteObject. exportObject (userManager, 0); 16 // Bind the remote object's stub in the registry17 Registry registry = LocateRegistry. createRegistry (2002); 18 19 registry. rebind ("userManager", userManagerI); 20 e M. out. println ("server is ready"); 21} 22} copy code (4) client code: Export the Account class and interface UserManagerInterface of the Server to an Export as a jar package named RmiServerInterface. jar. Import to client. Project -- Right-click -- Export -- java -- jar file -- next -- select Account class and interface UserManagerInterface -- name: RmiServerInterface. jar example: 3. create a java Project, import the jar package, and write the client code. 4. code ClientEntry. java copy code 1 package Weibo log. rmi; 2 3 import java. rmi. notBoundException; 4 import java. rmi. remoteException; 5 import java. rmi. registry. locateRegistry; 6 import java. rmi. registry. registry; 7 8 import cn.com. tt. rmiserver. stub. userManagerInterface; 9 10 public class ClientEntry {11 12 public static void main (String [] args) {13 14 try {15 Registry registry = LocateRegistry. getRegistry ("Localhost", 2004); 16 UserManagerInterface userManager = (UserManagerInterface) registry. lookup ("userManager"); 17 System. out. println ("username is" + userManager. getAdminAccount (). getUsername () 18 + "password" + userManager. getAdminAccount (). getPassword (); 19} catch (RemoteException e) {20 // TODO Auto-generated catch block21 e. printStackTrace (); 22} catch (NotBoundException e) {23 // TODO Auto-generated catch block24 E. printStackTrace (); 25} 26 27} 28 29} copy Code 5. run the server-side code first, and then run the client code. The running result is displayed. The client can run multiple times and get the server-side objects each time. If you want to run the client code again, you need to change the port number. If you do not change it, the port number is occupied.