This is a series of notes for learning Java EE, and some may wonder-why should we choose RMI, which is basically not used in enterprise-level development today, instead of learning from Servlet and JSP, it is because the well-known and notorious EJB that has to be learned is built on RMI. Carrying the phrase "if we don't know the past, we don't have much hope to grasp the future .", I started to learn the historical Java ee versions, so I chose ejb2, so I chose RMI.
Distributed Computing involves RPC, Cobra, and COM + in windows. After the emergence of J2EE, RMI quickly joined the distributed family.
RMI implements such an Application Scenario: calling an object method in another JVM from an object in the JVM is a typical C/S architecture. First, register the server-side object to rmiregistry. The client finds the object and calls the method of the object. If the parameters of a method call are also objects, whether it is input or returned, such objects need to be serialized to implement the serializable interface.
On the server side, you must first define a remote interface that inherits the remote interface to indicate that it can be called remotely. The remote interface defines the methods for providing services to the client. The objects that actually provide services need to implement these methods. To enable the client to capture Possible exceptions, each method of the server object should throw a RemoteException.
After defining the service interfaces and corresponding objects on the server, use the rmic command to generate stub. Stub runs on the client, which is equivalent to the proxy provided by the client, the client calls the server through stub.
Next, run rmiregistry. Note that the path should be the top-level directory of the object package name on the server side. If you want to use codebase to automatically download stub from the server, Stub should not be placed under classpath when running rmiregistry, but should be stored in Java. RMI. server. the codebase option adds stub to codebase. For example, when the server is started:
Java-djava. RMI. server. codebase = http: // localhost: 2001/-djava. secutiry. Policy = securiy. Policy Server
Java provides a default codebase server, which is easy to start.
When binding an object on the server, you can use either naming. Bind or JNDI.