Java RMI refers to remote method invocation (invocation). It is a mechanism that allows a Java program on one machine to invoke a Java program on another machine, and any object called by this method must implement the remote interface. EJB is built on the basis of RMI.
This section explains the basic concepts and system principles of RMI, and explains the development steps and compilation run methods of RMI programs through some introductory examples.
RMI is an implementation of the Java version of RPC (remote Procedure Call), which is a computer distributed communication protocol, including the Client/server model and the Distributed object model.
650) this.width=650; "Src=" Http://s5.51cto.com/wyfs02/M00/80/ED/wKioL1dFEJfgkEE2AAAlI65Arzc252.png " title= "RPC implementation model. PNG" alt= "Wkiol1dfejfgkee2aaali65arzc252.png"/>
RPC (Remote Procedure Call) is a distributed communication protocol for computers. The protocol allows programs that run with one computer to call subroutines from another computer, and programmers do not need to program this interaction.
RPC always sends a request by the client to the server to perform a number of procedures, and takes advantage of the parameters provided by the client, and executes the result back to the client.
To allow different clients to access the server side, many RPC uses the interface Description language IDL to facilitate cross-platform remote procedure calls.
The client/server model Client/server is a form of distributed communication. In this new type, the client and the server communicate once again to exchange information. A typical client/server approach is to use the underlying sockets. Socket and NIO socket programming are two Java implementations of socket programming
Distributed object Model
A distributed object-based system is a combination of objects that separate the service's requests from the providers of the service in a well-defined, encapsulated interface. Java remote Method invocation RMI and the common Object Request broker System (CORBA) are examples of this model. RMI is only available in the Java language, and CORBA supports multiple languages with more complexity.
Java Remote Method Invocation RMI is an application programming interface in the Java programming language for implementing remote procedure calls that enables programs running on a client to invoke objects on a remote server. RMI relies heavily on interfaces.
RMI system principles and development steps
RMI communication mechanism: RMI typically consists of two separate programs: Server and client programs
650) this.width=650; "src=" Http://s5.51cto.com/wyfs02/M02/80/EE/wKioL1dFFfeSebDdAAIih1sAfuI877.png "title=" RMI communication mechanism. PNG "alt=" Wkiol1dfffesebddaaiih1safui877.png "/>
RMI uses standard mechanisms during communication with remote objects: stub (stub) and skeleton (framework)
In the RMI Distributed Application system, the Java object passed between the server and the client must be a serializable object, and the non-serializable object cannot be passed in the object stream.
Steps to implement the RMI program
Defines the functionality of the remote class as a Java interface. In Java, a remote object is an instance of a class that implements a remote interface.
The remote interface must be declared public.
Remote Object Extension Java.rmi.Remote interface
Each method must be thrown java.rim.RemoteException
Any data type of a remote object that is passed as a parameter or a return value must be declared as a remote interface type.
An explanation of RMI program development
Create a remote interface class
Creating an Interface implementation class
Develop server-side programs to start remote interface services
Developing clients, accessing remote interface services
The steps to compile and run are as follows:
We will practice these steps by developing a HelloWorld starter program.
Implementation classes include:
Remote Excuse Class Itestremote
Implementing Class Testremote
Server Rmiserver
Client Rmiclient
Call relationship
650) this.width=650; "Src=" Http://s1.51cto.com/wyfs02/M02/80/F1/wKiom1dFLhjwZO1IAAGJNYrXbWY859.png " Title= "RMI program call relationship. PNG" alt= "Wkiom1dflhjwzo1iaagjnyrxbwy859.png"/>
Create a remote interface class Itestremote.java
Package org.test.rmi;
Import Java.rmi.Remote;
Import java.rmi.RemoteException;
/**
* @author Kucs
* Defines the remote interface, provides a method of Dealdata
* Expansion of remote
* The remote interface class must be declared public
* Must inherit from Java.rmi.Remote
* Must throw an java.rmi.RemoteExcetion exception
*/
Public interface Itestremote extends Remote {
public string DealData (String world) throws RemoteException;
}
Remote pretext implementation class Testremote.java
package org.test.rmi;
Import java.rmi.remoteexception;
import java.rmi.server.unicastremoteobject;
/**
* @author kucs
*
* implementation class for remote interface Itestremote
* Expanded UnicastRemoteObject,
* indicates that the Testremote class will be used to create a separate, non-replicable remote object
* It communicates with the default TCP Transport channel for RMI
*/
Public class testremote extends unicastremoteobject implements itestremote {
/**
*
*/
Private static final long serialVersionUID = 1L;
Protected testremote () throws remoteexception {
Super ();
}
@Override
Public string dealdata (string world) throws remoteexception {
// todo auto-generated method stub
return "Hello," +world;
}
}
Server Rmiserver.java
Package org.test.rmi;
Import java.net.MalformedURLException;
Import java.rmi.Naming;
Import Java.rmi.RMISecurityManager;
Import java.rmi.RemoteException;
Import Java.rmi.registry.LocateRegistry;
/**
* @author Kucs
* 3 things the server needs to do
* 1. Create an instance of RMI Security Manager Rmisecuritymanager and install it
* 2, create an instance of the remote object, in this instance is Testremote
* 3. Register this created object in the RMI registry
*/
public class Rmiserver {
public static void Main (string[] args) {
TODO Create and install security Manager
if (system.getsecuritymanager () = = null) {
System.setsecuritymanager (New Rmisecuritymanager ());
}
try {
TODO Creating a remote object
Itestremote test = new Testremote ();
TODO Startup Registry
Locateregistry.createregistry (1099);
Bind a name to an object
Naming.rebind ("//localhost:1099/testremoteservice", test);
SYSTEM.OUT.PRINTLN ("RMI server is running ....");
} catch (RemoteException e) {
TODO auto-generated Catch block
E.printstacktrace ();
} catch (Malformedurlexception e) {
TODO auto-generated Catch block
E.printstacktrace ();
}
}
}
Client Rmiclient.java
Package org.test.rmi;
Import java.net.MalformedURLException;
Import java.rmi.Naming;
Import java.rmi.NotBoundException;
Import Java.rmi.RMISecurityManager;
Import java.rmi.RemoteException;
/**
* @author Kucs
* The client can remotely invoke any method specified in the remote interface.
* The client must obtain a reference to the remote object from the RMI registry
* Client needs to do 3 things
* 1. Create an instance of RMI Security Manager Rmisecuritymanager and install it
* 2, according to the IP, port number, service name to find the remote host services and obtain instances of the service
* Native interface method that invokes the instance
*
*/
public class Rmiclient {
public static void Main (string[] args) {
TODO Create and install security Manager
if (system.getsecuritymanager () = = null) {
System.setsecuritymanager (New Rmisecuritymanager ());;
}
try {
Itestremote test = (itestremote) naming.lookup ("Rmi://localhost:1099/testremoteservice");
System.out.println (Test.dealdata ("I am the Client"));
System.out.println (Test.dealdata ("China"));
} catch (Malformedurlexception e) {
TODO auto-generated Catch block
E.printstacktrace ();
} catch (RemoteException e) {
TODO auto-generated Catch block
E.printstacktrace ();
} catch (Notboundexception e) {
TODO auto-generated Catch block
E.printstacktrace ();
}
}
}
Compiling and running the application
In order to run the application, we need to generate stubs and skeleton, compile the server and client programs, start the RMI registry, and finally start the server and run the client program
(1) Compile and generate stubs and skeleton
to generate stubs and skeleton, we use the RMI compiler to compile the implementation class Testremote.class. In the project features directory, under the Bin directory, start cmd. Execute the following command and the result shown. This will generate Testremote_stub.class. Testremote_stub.class is a client agent, and Testremote_skel.class is a server backbone. 650) this.width=650; "src=" Http://s3.51cto.com/wyfs02/M00/80/F2/wKiom1dFPofxbsVjAADYgYXUy_U117.png "title=" rmic command. png "alt=" Wkiom1dfpofxbsvjaadygyxuy_u117.png "/> 650" this.width=650; "src=" http://s1.51cto.com/ Wyfs02/m01/80/f0/wkiol1dfqd7wwbmwaacopqp_bi4328.png "title=" generates Testremote_stub.png "alt=" WKioL1dFQD7wwBmwAACOpQp_ Bi4328.png "/> (2) Start the RMI registry because D we added the Locateregitry.createregistry (1099) statement in the code, so the RMI registry automatically automatically when you start the server
(3) Writing a security license file
Because RMI security administrators are being used in server applications, a security license file is required to match it. Security license File Policy.txt, we save it in a directory without packages.
grant{
Permission Java.security.AllPermission "*:1000-9999", "accept,connect,listen,resolve";
};
(4) Start server command Java-djava.security.policy=policy.txt Org.test.rmi.RMIServer
(5) Run the client command Java-djava.security.policy=policy.txt org.test.rmi.RMIClient
Run results
650) this.width=650; "src=" Http://s2.51cto.com/wyfs02/M01/80/F2/wKiom1dFQXqiFNR3AAEeib_II8I535.png "title=" Run the result. png "alt=" Wkiom1dfqxqifnr3aaeeib_ii8i535.png "/>
Security Manager
When no write policy file overwrites the Java.policy in C:\Program files\java\jre1.6.0_05\lib\security, the call
if (System.getsecuritymanager () ==null)
System.setsecuritymanager (New Rmisecuritymanager ());
The system throws an exception java.security.AccessControlException.
Cause: Each Java application can have its own security manager, which is the primary security guard against malicious attacks. The security Manager protects resources from malicious operations by performing run-time checks and access authorizations to enforce the security policies required by the application. In effect, the security manager determines which set of permissions to grant to the class based on the Java security policy file. However, when untrusted classes and third-party applications use the JVM, the Java Security Manager uses a JVM-related security policy to identify malicious actions. In many cases, the threat model does not contain malicious code that runs in the JVM, and Java Security Manager is not required at this time. When the security Manager detects an action that violates a security policy, the JVM throws Accesscontrolexception or SecurityException.
In a Java application, the security Manager is set by the method Setsecuritymanager in the System class. To obtain the current security manager, you can use the method getSecurityManager. The Java.lang.SecurityManager class contains a number of checkxxxx methods, such as the Checkread (String file) method used to determine access to a file. These check methods call the Securitymanager.checkpermission method, which determines whether the calling app has the requested operation permission to execute according to the security policy file. If not, SecurityException will be thrown. If you want your app to use Security Manager and security policy, you can set the-djava.security.manager option when you start the JVM and specify the security policy file at the same time. If Java Security Manager is enabled in the app and no security policy file is specified, the Java Security Manager uses the default security policy, which is defined by the Java.policy located in directory $java_home/jre/lib/security. Class Loaders help them decide what permissions they should give when importing a piece of code into a virtual machine. At any time, there is only one policy object for each application.
2. Doubts about Rmic Rmic is to generate the related stubs and skeletons for the client and server side (which is actually a proxy class), but we can call the remote method directly in the Javac Java process, as if there were no rmic calls.
Cause: In previous versions of jdk5.0, it was necessary to use the rmic command to generate static proxy classes (including stubs and skeleton classes) for remote objects, while in jdk5.0 the RMI framework automatically generated dynamic proxy classes (including stubs and skeleton classes) for remote objects during the run. The implementation details of the RMI framework are thus more thoroughly encapsulated. Simplifies how the RMI framework is used.
3.registry Local Registration Form
In fact Locateregistry.createregistry (Rmiport) is the creation of a local reference to a remote or local registry, creating and exporting an registry instance.
RMI implements the client-to-remote host invocation, but both the client and server must be based on the Java language. If you want to implement calls between non-Java languages, you must use CORBA.
This article is from the "Ah Cool blog source" blog, please make sure to keep this source http://aku28907.blog.51cto.com/5668513/1783003
Fundamentals of Java Network Programming (VII)-RMI distributed network programming